I want to resize my images through Intervention image functionality in Laravel 4, but to keep aspect ratio of an image, this is what my code looks like:
$image_make = Image::make($main_picture->getRealPath())->fit('245', '245', function($constraint) { $constraint->aspectRatio(); })->save('images/articles/'.$gender.'/thumbnails/245x245/'.$picture_name);
Problem is that this doesn't keep aspect ratio of my image, thanks.
Intervention Image is an open source PHP image handling and manipulation library. It provides an easier and expressive way to create, edit, and compose images and supports currently the two most common image processing libraries GD Library and Imagick.
If you need to resize within constraints you should use resize
not fit
. If you also need center the image inside the constraints, you should create a new canvas
and insert the resized image within that:
// This will generate an image with transparent background
// If you need to have a background you can pass a third parameter (e.g: '#000000')
$canvas = Image::canvas(245, 245);
$image = Image::make($main_picture->getRealPath())->resize(245, 245, function($constraint)
{
$constraint->aspectRatio();
});
$canvas->insert($image, 'center');
$canvas->save('images/articles/'.$gender.'/thumbnails/245x245/'.$picture_name);
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