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; } }
Intervention Image is a PHP image handling and manipulation library providing an easier and expressive way to create, edit, and compose images. The package includes ServiceProviders and Facades for easy Laravel integration.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With