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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

imagecopy cropping troubles 1

Status
Not open for further replies.

frozenpeas

Technical User
Sep 13, 2001
893
CA
I am trying to copy a part of one image to another. My trouble is that with portrait images, the aspect ratio gets squashed. I have tried the following:

imagecopy($thumb,$thumb_src,0,0,120,90,$newwidth,$newheight);
imagecopyresized($thumb,$thumb_src,0,0,120,90,$newwidth,$newheight);
imagecopyresampled($thumb,$thumb_src,0,0,0,0,120,90,$newwidth,$newheight);

I figured it was because of $newwidth and $newheight... so I tried an if/else to swap them for portrait/landscape. But I got the same result.

Any ideas? Thanks.


frozenpeas
 
how do you get the new width and height?

you do, of course, need to determine which should be width and height. something like this normally does it.

Code:
$maxdimension = 500;
list ($w, $h) = getimagesize($image);
if ($w > $h){
 //landscape
 $newwidth = $maxdimension;
 $newheight = $h * ($maxdimension / $w );
}
if ($w > $h){
//landscape
 $newheight = $maxdimension;
 $newwidth = $w * ($maxdimension / $h );
}
if ($w === $h){
 $newwidth = $newheight = $maxdimension;
}

and the best quality transformation will be with imagecopyresampled on an image created with imagecreatetruecolor or one of file based imagecreates.
 
Thanks, jpadie. I'll give it a try when I get home.

I am getting $newwidth and $newheight from an earlier part in my script... they are the size of the image that this new cropped image is being made from.

frozenpeas
 
oops the second conditional is incorrect should be
Code:
if ($w [red]<[/red] $h){
[red]//portrait[/red]
 $newheight = $maxdimension;
 $newwidth = $w * ($maxdimension / $h );
}
 
Well, hot dog. That worked liked a charm. Thanks!

frozenpeas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top