Resize Image Keeping Aspect Ratio in Imagine

I was working with Imagine recently. I want to create a standard width and height for the image that is created. Eg: 500 X 300 .

The user will be uploading different size images and I want to resize the image to X width and Y height. I was using imagine and the code helps to make this happen filling the blank space with white color without loosing aspect ratio.

resize image with imagine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
$source = 'image.jpeg';
$destination = 'resize.jpg';
$width  = 300;
$height = 500;

$imagine   = new Imagine\Gd\Imagine();
$size      = new Imagine\Image\Box($width, $height);
$mode      = Imagine\Image\ImageInterface::THUMBNAIL_INSET;
$resizeimg = $imagine->open($source)
                ->thumbnail($size, $mode);
$sizeR     = $resizeimg->getSize();
$widthR    = $sizeR->getWidth();
$heightR   = $sizeR->getHeight();

$preserve  = $imagine->create($size);
$startX = $startY = 0;
if ( $widthR < $width ) {
    $startX = ( $width - $widthR ) / 2;
}
if ( $heightR < $height ) {
    $startY = ( $height - $heightR ) / 2;
}
$preserve->paste($resizeimg, new Imagine\Image\Point($startX, $startY))
    ->save($destination);

A normal image of size 500 X 500

a kitten

when resized to 300 X 200

resized image

and 300 X 400

resized image

A huge Thanks to the person who helped me on Symfony2 irc to find the different modes. I am not recalling the irc handle currently. I will update once I remember the same.

Image courtsey http://placekitten.com/

imagine, php, resize

Comments