[![Latest Version on Packagist][ico-version]][link-packagist] ![Software License][ico-license] [![Total Downloads][ico-downloads]][link-downloads]
mediauploader
is a simple file handling package of Laravel
that provides variety of options to deal with files. such as Image upload, file upload, webp upload,
base64 image, upload from URL, and upload any type of contents
Via Composer
$ composer require sudippalash/mediauploader
After updating composer, add the ServiceProvider to the providers array in config/app.php
Sudip\MediaUploader\MediaUploaderServiceProvider::class,
You can use the facade for shorter code. Add this to your aliases:
'MediaUploader' => Sudip\MediaUploader\Facades\MediaUploader::class,
You will need to publish config file to add mediauploader
global path.
php artisan vendor:publish --provider="Sudip\MediaUploader\MediaUploaderServiceProvider" --tag=config
In config/mediauploader.php
config file you should set mediauploader
global path.
return [
/*
|--------------------------------------------------------------------------
| Base Directory
|--------------------------------------------------------------------------
|
| base_dir stores all other directory inside storage folder of your laravel application by default
| if you specify any name. all storage will be done inside that directory or name that you specified
|
*/
'base_dir' => '',
/*
|--------------------------------------------------------------------------
| Thumb Directory
|--------------------------------------------------------------------------
|
| thumb_dir creates another folder inside the directory as a "thumb" by default
| you can change the name thumb to any other name you like.
*/
'thumb_dir' => 'thumb',
/*
|--------------------------------------------------------------------------
| Timestamp Prefix
|--------------------------------------------------------------------------
|
| If timestamp_prefix is true then create a file with a timestamp to ignore the same name image replacement. Example: image-1658562981.png.
| If timestamp_prefix is false then the script checks file exists or not if the file exists then add the time() prefix for the new file otherwise leave it as the file
| name.
*/
'timestamp_prefix' => false,
/*
|--------------------------------------------------------------------------
| Thumb Image Height Width
|--------------------------------------------------------------------------
|
| specify the thumb image ratio of height and weight by default it takes 300px X 300px
*/
'image_thumb_height' => 300,
'image_thumb_width' => 300,
/*
|--------------------------------------------------------------------------
| Fake Image Url
|--------------------------------------------------------------------------
|
| fake_image_url , if you specify a fake image path here. the entire package will use
| this image when there is no image found. or you can specify the fake image in the
| function parameter as well.
| Example: 'fake_image_url' => url('images/fake.png'),
*/
'fake_image_url' => null,
/*
|--------------------------------------------------------------------------
| Folder permission
|--------------------------------------------------------------------------
|
| path_permission , if you create a folder in your project then you can define your folder permission.
| Example: null, 0755, 0777
*/
'path_permission' => 0777,
];
Before jumping to use this, you must do the following:
- Please make sure to link your storage before using this package
php artisan storage:link
- Change your env filesystem drive to this
FILESYSTEM_DISK=public
- Image Upload
MediaUploader::imageUpload(<UploadedFile image>, <output path>, thumb=false, name=null, $imageResize = [], $thumbResize = [0, 0]);
Example:
$file = MediaUploader::imageUpload($request->file, 'images', 1, null, [600, 600]);
if ($file) {
// File is saved successfully
}
- Webp Image Upload
MediaUploader::webpUpload(<UploadedFile image>, <output path>, thumb=false, name=null, $imageResize = [], $thumbResize = [0, 0]);
Example:
$file = MediaUploader::webpUpload($request->file, 'webps', 1, null, [600, 600]);
if ($file) {
// File is saved successfully
}
- Any Upload (if you are not sure what type of file is, you can use this)
MediaUploader::anyUpload(<UploadedFile file>, <output path>, name=null);
Example:
$file = MediaUploader::anyUpload($request->file, 'files', 'filename');
if ($file) {
// File is saved successfully
}
- Thumb (to create a thumb from an existing image, you can use this)
MediaUploader::thumb(<output path>, <file>, $thumbPath = false, $thumbWidth = 0, $thumbHeight = 0);
Example:
$file = MediaUploader::thumb('thumbs', $file, 200, 200);
if ($file) {
// File is saved successfully
}
- Image Upload from URL
MediaUploader::imageUploadFromUrl(<Valid Image URL>, <output path>, thumb=false, name=null, $imageResize = [], $thumbResize = [0, 0]);
Example:
$file = MediaUploader::imageUploadFromUrl($url, 'images', 1, null, [600, 600]);
if ($file) {
// File is saved successfully
}
- Base64 image Upload
MediaUploader::base64ImageUpload(<base64 Content>, <output path>, thumb=false, name=null, $imageResize = [], $thumbResize = [0, 0]);
Example:
$file = MediaUploader::base64ImageUpload($content, 'images', 1, null, [600, 600]);
if ($file) {
// File is saved successfully
}
- Content Upload
MediaUploader::contentUpload(<Content>, <output path>, name=null);
Example:
$file = MediaUploader::contentUpload($content, 'contents', 'name');
if ($file) {
// File is saved successfully
}
Returns:
Response from every function looks like this
[
"name" => "example-image.jpg"
"originalName" => "example-image.jpg"
"size" => 148892
"width" => 600
"height" => 600
"mime_type" => "image/jpeg"
"ext" => "jpg"
"url" => "http://localhost/storage/test/example-image.jpg"
]
File Delete
MediaUploader::delete(<path>, <file_name>, $thumb = false)
Example:
$file = MediaUploader::delete('images', $file_name, true);
if ($file) {
// File is deleted successfully
}
Directory Delete
MediaUploader::removeDirectory(<path>)
Example:
$path = MediaUploader::removeDirectory('images');
if ($path) {
// Directory is deleted successfully
}
- FIle/Image Exists
MediaUploader::fileExists(<path>, <file_name>)
Example:
{!! MediaUploader::fileExists('images', $file_name) !!}
- FIle/Image Url
MediaUploader::showUrl(<path>, <file_name>)
Example:
{!! MediaUploader::showUrl('images', $file_name) !!}
- File Preview
MediaUploader::showFile(<path>, <file_name>)
Example:
{!! MediaUploader::showFile('images', $file_name) !!}
- Image Preview
MediaUploader::showImg(<path>, <file_name>, <array options>)
Example:
{!! MediaUploader::showImg('images', $file_name, [
'thumb' => true, // If thumb image store via upload function.
'popup' => true, // Currently support only jQuery fancybox.
'fakeImg' => 'images/avatar.png', // Pass fake image path without url or pass true (if pass true it will generate fake image from config file fake_image_url value).
'id' => 'image',
'class' => 'img-fluid',
'style' => '',
'alt' => 'Nice Image',
]) !!}