Page 1 of 1

Problems with cropImage()

Posted: 2011-09-26T13:03:54-07:00
by Spade42
I've been a programmer for a long time, but am just now getting into PHP. I have an application where I need to re-size and crop an image, below is a snippet of code. The re-size works, but the crop does not, yet I still get "Crop Succeeded!" as a result. Ideas? Thanks in advance!

Code: Select all

if($w > $h){      //Image is landscape
	$orientation = 'landscape';
	if($w > 600){
		$picture->resizeImage($w*(600/$h) , 600 , Imagick::STYLE_NORMAL , 0); //Set height to 600, scale width proportionally (so that when the crop is done, the final result is 600x600)
	}
	if($picture->cropImage($height, $height,($width / 2) - ($height / 2), 0)){
		echo "Crop Succeeded!";
	}
	else{
		echo "Crop Failed :(";
	}
}

Re: Problems with cropImage()

Posted: 2011-09-26T13:55:46-07:00
by Bonzo
Out of interest I use php with exec() and the command line as I find it simpler and has more options.

Have you tried catching any php errors with http://php.net/manual/en/language.exceptions.php ?

This may be of interest - I found it on the web somewhere. The fit fails and from memory it depends on the Imagick version installed. The last piece of code is probably what you are looking for?

Code: Select all

<?php
 
/* Create new imagick object */
$im = new imagick( 'small.jpg' );

/* Clone the object for different types */
$normal = $im->clone();
$fit = $im->clone();
$fixed = $im->clone();
$crop = $im->clone();
 
/* Create a normal thumbnail 100x width, save dimensions */
$normal->thumbnailImage( 100, null );
$normal->writeImage( "normal_thumbnail.jpg" );
 
/* Create a fit thumbnail, both sides need to be smaller 
   than the given values, save dimensions */
$fit->thumbnailImage( 100, 100, true );
$fit->writeImage( "fit_thumbnail.jpg" );

/* Create a fixed size thumbnail, ignore dimensions */
$fixed->thumbnailImage( 100, 100 );
$fixed->writeImage( "fixed_thumbnail.jpg" );
 
/* Create a thumbnail, keep dimensions, crop overflowing parts */
$crop->cropThumbnailImage( 100, 100 );
$crop->writeImage( "crop_thumbnail.jpg" );
 
?> 

Re: Problems with cropImage()

Posted: 2011-09-29T07:35:30-07:00
by Spade42
Turns out it was a rather simple logic error on my part, feel kinda stupid now :p

The $width and $height variables were supposed to be $w and $h, since the values where 0 it was screwing up.. Thanks for the reply =]