tags:

views:

118

answers:

1

I am uploading images and resizing them to 300x200 pixels

i'm using the imagick extension and specifically the adaptiveResizeImage() function.

when uploaded using bestfit there is a lot of whitespace on the sides of the image (depending on portrait or landscape).

what i'd rather it did was resize to fill the entire area (no whitespace) and crop the longer length (height or width), ie i'd rather lose some image than have whitespace.

is there an easy way to do this with the imagick extension?

EDIT: I managed to do what i needed, but there has to be a better way

header('Content-type: image/jpeg');

// target sizes
$target_width = 300 ;
$target_height = 100 ;

// create new image
$image = new Imagick('test.jpg');

// get current size and calculate diffences from target sizes
$size = $image->getImageGeometry();
$size['width_diff'] = $target_width/$size['width'] ;
$size['height_diff'] = $target_height/$size['height'] ;

// resize by smallest size
if($size['width_diff']>=$size['height_diff'])
{
    $width = $target_width ;
    $height = $size['height']*$size['width_diff'] ;
}
else
{
    $width = $size['width']*$size['height_diff'] ;
    $height = $target_height ;
}

// get offsets
$x = ($width-$target_width)/2 ;
$y = ($height-$target_height)/2 ;

// resize and offset image
$image->adaptiveResizeImage($width, $height) ;
$image->extentImage($target_width, $target_height,-$x,-$y);

// output and clean up
echo $image ;
$image->clear();
$image->destroy();
+1  A: 

Maybe you can take a look at the extent flag in the ImageMagick tutorial.

From a quick look it seems the equivalent PHP would be Imagick::ExtentImage but have not used it from php.

renick
that is a great help, do you know how to use the Fill Area Flag ('^' flag) in the imagick extension?
mononym
Sorry never used the PHP extension. But from a quick look they do have a setOption http://www.php.net/manual/en/function.imagick-setoption.php
renick