Skip to content

fynduck/files-upload

Repository files navigation

FilesUpload

Software License Latest Version on Packagist Tests Total Downloads

Laravel files-upload Php version
< 5.7 ^1.8 >=5.6
>= 5.7 ^2.1 >=5.6
5.7 - 11.0 ^3.1 >=7.1
>= 9.0 ^4.0 >=8.1

Usage

Upload file or image

use Fynduck\FilesUpload\UploadFile;

UploadFile::file($request->file('file')) //or $request->get('base64'), required
    ->setDisk('storage') //default is public
    ->setFolder('Post') //optional
    ->setName('image_name') //optional, default use file name or random in case base64
    ->setOverwrite('old_name.jpg') //optional, remove file with old name
    ->setSizes(['xs' => ['width' => 100, 'height' => 100]]) //(optional) if need other sizes
    ->setBackground('#000000') //optional
    ->setBlur(0) //optional, use values between 0 and 100
    ->setBrightness(0) //optional, use values between -100 and +100. brightness 0 for no change
    ->setGreyscale(true) //optional true or false default is false
    ->setOptimize(true) //optional
    ->setEncodeFormat() //optional, ['jpeg', 'jpg', 'png', 'gif', 'webp', 'avif']
    ->setEncodeQuality() //optional, use values between 0 and 100
    ->queue() //optional, generate sizes in the background (see below)
    ->save('resize'); //save option resize, crop default is resize

Background (queued) resizing

When you request many sizes, generating them inline makes the client wait for every resize + optimize to finish. Call ->queue() to store the original immediately and generate the size variants on your queue workers instead:

UploadFile::file($request->file('file'))
    ->setFolder('Post')
    ->setName('image_name')
    ->setSizes(['xs' => ['width' => 100, 'height' => 100], 'md' => ['width' => 400, 'height' => 400]])
    ->queue() // or ->queue('redis', 'images') ; ->queue(perSize: false) for one job for all sizes
    ->save(); // returns the original filename right away

By default each size is dispatched as its own job inside a batch, so sizes are rendered in parallel across workers. When the batch finishes, a Fynduck\FilesUpload\Events\ImageSizesGenerated event is fired (disk, folder, name, size keys, source path) so you can react (e.g. mark a record as ready). Make sure a worker is running: php artisan queue:work.

Publish the config to change defaults (default disk, quality, and whether background mode is always on):

php artisan vendor:publish --tag=files-upload-config
// config/files-upload.php
'queue' => [
    'enabled'    => false, // set true to always resize in the background
    'connection' => null,
    'queue'      => null,
    'per_size'   => true,  // one job per size (parallel) vs a single job for all sizes
],

Make new sizes from image

use Fynduck\FilesUpload\ManipulationImage;

ManipulationImage::load($pathImage)
            ->setDisk('storage') //default is public
            ->setFolder('Post')
            ->setSizes(['xs' => ['width' => 100, 'height' => 100]])
            ->setName('image_name.jpg') //name with extension
            ->setOverwrite('old_name.jpg') //optional, remove file with old name
            ->setBackground('#000000') //optional
            ->setBlur(0) //optional, use values between 0 and 100
            ->setBrightness(0) //optional, use values between -100 and +100. brightness 0 for no change
            ->setGreyscale(true) //optional true or false default is true
            ->setOptimize(true) //optional
            ->setEncodeFormat() //optional, ['jpeg', 'jpg', 'png', 'gif', 'webp', 'avif']
            ->setEncodeQuality() //optional, use values between 0 and 100
            ->save('resize'); //save option resize, resize-crop, crop default is resize

Optimize exist image

use Fynduck\FilesUpload\ManipulationImage;

ManipulationImage::load('image_name.jpg')
            ->setOptimize(true)
            ->optimize('path_to_image');

resize: Resize the image by the maximum width or height

crop: Cut out by size part of the current image with given width and height

For laravel < 5.7 use version 1.8

Previous stable versions

Installation

You can install the package via composer:

composer require fynduck/files-upload

Contributing

Please see CONTRIBUTING for details.

JetBrains

License

The MIT License (MIT). Please see License File for more information.

About

Upload image or file, crop or resize image width

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Contributors

Languages