cropping images with gd

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

cropping images with gd

Post by nigma »

Which function in the gd library is used for cropping images? imagecopyresampled() ?

Would someone provide an example of using the function to crop an image ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

pass a source rect that differs from the image's dimensions.

Moved to Graphics
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

could you elaborate? I don't really even understand what some of the function args do.

Here is the function call i'm playing with trying to get the image cropped:
imagecopyresampled($im,$im, 0,0,0,0, 2*602,2*324, 602,324);

i'd like to crop an image that is 602*2 in width and 324*2 in height to a w/h of 602, 324.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

btw, am i using thr right function to crop?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the code you are using will enlarge the image to 1204x648, not crop it.

cropping would be:

Code: Select all

imagecopyresampled($im,$im, 0,0, 0,0, 602,324, 602,324);

// or

imagecopyresampled($im,$im, 0,0, 301,127, 602,324, 602,324);
bool imagecopyresampled ( resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

When I said I didn't even fully understand what all the arguments do I didn't mean to imply that I hadn't read the php.net manual on the function.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I wasn't implying you hadn't.. just posting it for easier clarification. Your post said that the source image was 1204x648.. but you passed those as your destination request..
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

hey thanks, your examples helped me get imagecopyresampled() to crop for me now but which args do I change to alter the place where I crop?

like say I have an image and I want to get rid of everythign but a 10px square in the middle, how would I do something like that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"src" prefixed ones will alter the location and dimenions copied from in the source image.
"dst" prefixed ones will alter the location and dimensions copied to in the destination image.
Post Reply