Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php imagemagick crop

Status
Not open for further replies.

overflo

Technical User
Feb 10, 2003
70
AU
Hi

I have this script working so far.
It get gets an image, resizes the shortest edge to 300, now I need it to crop the image so that it is 300x300 (from the center). I have tried so many different ways that I'm totally confused and would be eternally grateful if anyone can give me any ideas.

$img = "pathtoimage.jpg";
$size = GetImageSize($img);

// if Wide Image
if($size[0] > $size[1]) {
$thumbnail_height = 300;
$thumbnail_width = (int)(300 * $size[0]/size[1]);
} else{
//if Tall Image
$thumbnail_height = (int)(300 * $size[1] / $size[0]);
$thumbnail_width = 300;
}

$mogrify = "pathtoImageMagick/mogrify -geometry";
system("$mogrify $thumbnail_heightx$thumbnail_width $img", $exec_retval);
 
Got it, was a lot easier than I thought. Here's the solution in case anyone wants to know.

$img = "pathtoimage.jpg";
$size = GetImageSize($img);

// if Wide Image
if($size[0] > $size[1]) {
$thumbnail_height = 300;
$thumbnail_width = (int)(300 * $size[0]/size[1]);
} else{
//if Tall Image
$thumbnail_height = (int)(300 * $size[1] / $size[0]);
$thumbnail_width = 300;
}

$mogrify = "pathtoImageMagick/mogrify -geometry";
$crop = "pathtoImageMagick/mogrify -gravity Center -crop";
system("$mogrify $thumbnail_heightx$thumbnail_width $img", $exec_retval);
system("$crop 300x300+0+0 $img", $exec_retval);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top