Mcmilan
Mcmilan

Reputation: 131

Laravel multiple images upload and resize with intervention

Can someone give me an example how to upload and resize multiple images using intervention in laravel. I just need part in controller after

if($request->hasFile('images')){
  $images = $request->file('images');

I did search on google but i only get examples using one image.

Upvotes: 4

Views: 2398

Answers (3)

Zaid Bin Khalid
Zaid Bin Khalid

Reputation: 763

This class you can use with any framework or core PHP.

For complete detail visit the below link.

https://learncodeweb.com/web-development/laravel-9-upload-multiple-files-and-image-resizer/

There are many libs that you can use to upload & resize multiple images in the Laravel. I come up with a simple and easy solution, it is a GD base class.

All you need to install via composer with the help of the below command.

composer require learncodeweb/filesupload

After installation recreates the autoload file with the help of the below command. (optional)

composer dump-autoload

How to import and use in the Larvel 8/9 (Tested).

use anyFileUpload\FilesUploadAndImageResize as anyFilesUpload;
 
$files = new anyFilesUpload('array', ['jpg', 'jpeg', 'png'], public_path('uploads'), 0777);
$files->uploadFiles('files', 250, '', '', 100, '850', ['350']);
 
dd($files->uploadedData);

After uploading files to the server, it returns all uploaded file names. You can dump those in your DB.

Provided Functionalities

  1. Upload Single Or Multiple Files.
  2. Upload Any Type Of Files (Not Only Images).
  3. The image file can Resize.
  4. Create Image Thumbnails (With Keep The Image Aspect Ratio).
  5. You can add a watermark (Text, Image).
  6. Easy Integration With Forms.
  7. Create Any Number Of Thumbnails Under One Upload.
  8. Customizable Paths To Thumbnails Folders.
  9. Customizable Thumbnails Sizes And Dimensions.
  10. Files Extension Filters.
  11. File Size Limit for Uploading.

Upvotes: 0

Rahman Rezaee
Rahman Rezaee

Reputation: 2165

So i don't know what you already did. So let's start from the beginning.

First of all you need the Intervention Library. So switch to your main Folder (containing your composer.json file) And type

composer.phar require intervention/image

Or just add "intervention/image": "~2.1" to your require array in composer.json. ( And do a composer update after that )

"require": {
    "laravel/framework": "5.0.*",
    "intervention/image": "~2.1"
},

Now you have to add

'Intervention\Image\ImageServiceProvider',

to the providers array

and

'Image' => 'Intervention\Image\Facades\Image'

to your aliases Array. Both in config/app.php

Now you could create a "upload function" somewhere in a controller like

public function upload() {
    $image = \Image::make(\Input::file('image'));
    $path = storage_path('app')."/";

    // encode image to png
    $image->encode('png');
    // save original
    $image->save($path."original.png");
    //resize
    $image->resize(300,200);
    // save resized
    $image->save($path."resized.png");
}

This would save two images to the storage/app folder. One in the original size and one resized to 300x200.

This code is only an example, it does not contain any checks, for valid images or stuff like that. It just takes a file (assuming an image) and saves it two times. And of course you also don't need to encode to png...

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163948

Iterate over the images and handle them one by one:

foreach ($images as $image) {
    Image::make($image)->....
}

Upvotes: 0

Related Questions