Problems with cropImage()

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
Spade42
Posts: 2
Joined: 2011-09-26T12:59:25-07:00
Authentication code: 8675308

Problems with cropImage()

Post 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 :(";
	}
}
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Problems with cropImage()

Post 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" );
 
?> 
Spade42
Posts: 2
Joined: 2011-09-26T12:59:25-07:00
Authentication code: 8675308

Re: Problems with cropImage()

Post 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 =]
Post Reply