levaral-dev/upload-v2

This package is abandoned and no longer maintained. No replacement package was suggested.
There is no license information available for the latest version (dev-master) of this package.

Levaral file upload package for Laravel Framework

dev-master 2018-11-19 10:49 UTC

This package is not auto-updated.

Last update: 2021-01-30 03:01:11 UTC


README

composer require levaral-dev/upload-v2:dev-master

Run laravel publish, this will create config file upload.php and migrations

php artisan vendor:publish --provider="Levaral\Upload\UploadServiceProvider" 

Put route in routs/web.php or routs/api.php where ever you want, if you add route in web.php you must except url in VerifyCsrfToken middleware

Action::post('upload', \Levaral\Upload\Http\PostUpload::class, 'upload'); 

Configuration

return [
    // storage disk
    'disk' => env('UPLOAD_DISK', 'public'), 
     
     // file fields configration  
    'files' => [         
        'sample_logo' => [     //Required: field
            'resize' => [      //Optional: Resize options, use with images
                'thumb' => [    //possible resize options
                    'height' => 100,
                    'width' => 100,
                    'fit' => 'stretch', // ['stretch', 'crop', 'contain', 'max', 'fill', 'pad']
                    'crop' => [
                        'x' => 100,
                        'y' => 100
                    ],
                    'color' => '#ffffff' 
                    'create_on_upload' => true   //Optional: create resize image upfront 
                ]
            ],
            'validation' => 'required|mimes:jpeg,png,gif|size:2000', //Required: laravel validation string
            //Optional:
            //'optimize' => true, //optimize image on upload
            //'keep_original_file' => true //Note: this might take time if the image file is uploaded to cloud
        ],
        
        'sample_document' => [   //Required: field
            'validation' => 'required|size:2000', //Required: laravel validation string
        ]
    ],
];

Post request

post request accept two parameters (file_upload, file_field). In 'file_upload' pass the file and in 'file_field' pass field name for e.g company_logo, product_images etc...

Post response

{
    "success": true,
    "upload_id": 25,
    "url": "http://testl6.test/storage/uploads/bcb6a904-5a9f-415c-b1d1-0c35e7cda1a4/download.png",
    "urls": {
        "thumb": "http://testl6.test/storage/cache/bcb6a904-5a9f-415c-b1d1-0c35e7cda1a4/download-thumb.png"
    }
}

Get file and upload via url

To do that use FileUploadService class and inject it to your controller class or any other class for e.g

$upload = $this->fileUploadService->uploadFileByUrl('http://vkwins.com/wp-content/uploads/2017/10/Lorem-Ipsum.jpg', 'sample_logo');

Get original file url or resize image url using Upload Eloquent object

$upload->getPublicUrl();  // will return original uploaded file url
$upload->getPublicUrl('sample_logo.resize.thumb');  // will return resized image url

Remove all redundant files older then 24 hours

php artisan levaral:delete-files

Regenerate all resize images or for specific file_field

levaral:generate-images {fileField?}

Image optimize

Image optimize depends up on laravel package https://github.com/spatie/laravel-image-optimizer to install image optimize tools run following commands

sudo apt-get install jpegoptim
sudo apt-get install optipng
sudo apt-get install pngquant
sudo npm install -g svgo
sudo apt-get install gifsicle

optimize uploaded images in bulk, this will optmize only images

levaral:optimize-images {fileField?}

optimize images by giving specific path, this will optmize only images

levaral:optimize-images {path}  //give {path} like public, public/theme/images