Laravel 5 intervention image upload multiple size

2019-06-10 23:53发布

问题:

I am using laravel 5 and intervention, and would like to store multiple sizes of a image when it is uploaded via a form. Can anybody guide me

回答1:

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...