Resize image in Laravel 5.2

人走茶凉 提交于 2019-11-27 01:10:20

问题


Can anyone help me how to implement resizing image in Laravel?

I have this code only:

if($request->hasFile('image')){
    if (Input::file('image')->isValid()) {
        $file = Input::file('image');
        $destination = base_path() . '/public/images/ServiceImages';
        $extension = Input::file('image')->getClientOriginalExtension();
        $fileName = rand(111,99999).'.'.$extension;

        if(!empty($data['Image'])){
            unlink($destination.$data['Image']);
        }

        $file->move($destination, $fileName);
        $service->image=$fileName;
    }
}

回答1:


Laravel does not have a default resize of image. But most laravel developers use 'Image intervention' in handling the image. (Easy to use)

To install (Image intervention):

STEP 1 Run

composer require intervention/image

STEP 2 On your config/app.php:

In the $providers array, add the following:

Intervention\Image\ImageServiceProvider::class

In the $aliases array,add the following:

'Image' => Intervention\Image\Facades\Image::class

If you have problems your GD librabry is missing, intall it

  • PHP5: sudo apt-get install php5-gd
  • PHP7: sudo apt-get install php7.0-gd

~~ To use on your controller ~~

STEP 3 On top of your controller

use Intervention\Image\ImageManagerStatic as Image;

STEP 4 On your method (there are several ways but this will give you an idea)

if($request->hasFile('image')) {

    $image       = $request->file('image');
    $filename    = $image->getClientOriginalName();

    $image_resize = Image::make($image->getRealPath());              
    $image_resize->resize(300, 300);
    $image_resize->save(public_path('images/ServiceImages/' .$filename));

}

Reference here




回答2:


Try this, Image intervention, open source PHP image handling and manipulation library



来源:https://stackoverflow.com/questions/40358510/resize-image-in-laravel-5-2

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