Reputation: 4248
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;
}
}
Upvotes: 27
Views: 89427
Reputation: 28969
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);
Upvotes: 0
Reputation: 358
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.
Upvotes: 0
Reputation: 5997
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.
Upvotes: 114