Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

php - Resize image in Laravel 5.2

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Laravel does not have a default resize of image. But most Laravel developers use 'Image intervention' in handling the image. It is 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:

InterventionImageImageServiceProvider::class

In the $aliases array,add the following:

'Image' => InterventionImageFacadesImage::class

If you have problems your GD library is missing, install 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 InterventionImageImageManagerStatic 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...