1. Go to this page and download the library: Download adsoftteam/uploadimage library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
/**
* Upload file to disk.
*
* @param $file object file
* @param $contentName string content name (use for create and named folder)
*
* @return object image
* @throws UploadFileException
*/
UploadImage::uploadFile($file, $contentName)
$file = $request->file('image');
// Upload and save file.
try {
// Upload and save file.
$input['file'] = UploadImage::uploadFile($file, 'post')->getImageName();
} catch (UploadImageException $e) {
return back()->withInput()->withErrors(['image', $e->getMessage()]);
}
/**
* Delete image from disk.
*
* @param $imageName string image name or array with images
* @param $contentName string content name (use for folder and name)
*
*/
UploadImage::delete($imageName, $contentName);
// Delete old image.
UploadImage::delete($post->image, 'post');
/**
* Delete body images from disk.
*
* @param $textBody string with text where there images
*
*/
UploadImage::deleteBody($textBody);
// Delete all images from post body (added in editor).
UploadImage::deleteBody($post->body);
/**
* Create path to image.
*
* @param $contentName string content name (use for folder and name)
* @param null $size integer width for image (use one of thumbnail array)
*
* @return mixed
*/
UploadImage::load($contentName, $size = null);
// Give all data to template.
return view('posts.index', [
'posts' => $posts,
'path' => UploadImage::load('post')
]);
/**
* Preview image for form.
*
* @param $file object instance image
* @param $contentName string content name (use for folder and name)
*
* @return array new image stream Base64
*/
UploadImage::preview($file, $contentName);
// Get image preview.
$image_url = UploadImage::preview($file, 'collage_image');