Laravel 5 intervention image upload multiple size

独自空忆成欢 提交于 2019-12-08 10:52:05

问题


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



来源:https://stackoverflow.com/questions/29314189/laravel-5-intervention-image-upload-multiple-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!