Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intervention image aspect ratio

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.

like image 767
sk4yb3n Avatar asked Nov 12 '14 15:11

sk4yb3n


People also ask

What is image intervention?

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.


1 Answers

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);
like image 146
Bogdan Avatar answered Oct 14 '22 20:10

Bogdan