Resize image in Laravel 5.2

前端 未结 3 1139
甜味超标
甜味超标 2020-12-08 11:52

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

I have this code only:

if($request->hasFile(\'image\')){
    if (Input::file(\'imag         


        
相关标签:
3条回答
  • 2020-12-08 11:53

    Although this is an old post but I consider posting a solution which is independent of installing any package.

    $image = $request->file('image');
    
    $image_name = rand(111111, 888999)*time() .'.'. $image->getClientOriginalExtension();
    $thumb_name = rand(111111, 888999)*time() .'.'. $image->getClientOriginalExtension();
    $destinationPath = public_path('/uploads');
    
    $image->move($destinationPath, $image_name);
    $orgImgPath = $destinationPath. '/'.$image_name;
    $thumbPath = $destinationPath. '/'.$thumb_name;
    shell_exec("convert $orgImgPath -resize 200x200\! $thumbPath");
    

    This will forcely resize your image to 200x200 because of the ! but if you like to keep the aspect ratio then ! need to be removed. This code will save the original uploaded and thumbnail will be generated.

    0 讨论(0)
  • 2020-12-08 11:57

    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:

    Intervention\Image\ImageServiceProvider::class
    

    In the $aliases array,add the following:

    'Image' => Intervention\Image\Facades\Image::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 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.

    0 讨论(0)
  • 2020-12-08 11:58

    I would recommend Intervention Image for this task.

    Simply install it with composer

    composer require intervention/image
    

    and just use it like this:

    \Intervention\Image\ImageManagerStatic::make('public/foo')->fit(100)->save($path);
    

    Remarks

    • Service Provider The intervention image provides a Service Provider for Laravel. You may add the service provider manually as explained here. After that, you may push the configuration file to Laravel. However, the configuration has only one option and that is the image driver. gd is default , so if you don't want to change it, there is no need to use the service provider and configuration.

    • Alias Instead of the service Provider, you could create an alias in config/app:

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

      Then you can use it like this:

      \Image::make('public/foo')->fit(100)->save($path);
      
    • Testing You can still Laravel's file upload tests when using intervention image. For details, see How to fake image upload for testing with Intervention image package using Laravel
    0 讨论(0)
提交回复
热议问题