Page 1 of 1

Making Square Thumbnails

Posted: Thu Nov 13, 2008 1:10 am
by Theory?
How would I go about cropping an image into a perfect square from the top left corner. I don't want to resize the image, I just want to start at the top left and make a square the size of my choosing and crop the image.

Re: Making Square Thumbnails

Posted: Thu Nov 13, 2008 1:25 am
by Rovas
You calculate the dimensions of the square based on the dimensions of the image (use imagesx and imagesy to retrieve them) then use imagecreate to create the image where you are going to place the cutout then use to copy the square from the target image imagecopy. If you want to copy from images that contain alpha channel (i.e. transparency) is going to be a little more complicated because you have to "save" the alpha channel: use these functions imagecolorallocatealpha, imagecolortransparent.

Re: Making Square Thumbnails

Posted: Thu Nov 13, 2008 2:28 am
by Eran
Use PHP Image, it's an image manipulation library made by forum moderator onion2k. recommended :)

Re: Making Square Thumbnails

Posted: Thu Nov 13, 2008 2:06 pm
by Theory?
Thanks for the link.

I have most of the code I need, I found it lying around the intertron. However, I'm using imagecopyresampled and the params I have set now only resize the full image. I'm just wondering how to get it to crop the image instead.

Re: Making Square Thumbnails

Posted: Thu Nov 13, 2008 3:02 pm
by requinix
Theory? wrote:Thanks for the link.

I have most of the code I need, I found it lying around the intertron. However, I'm using imagecopyresampled and the params I have set now only resize the full image. I'm just wondering how to get it to crop the image instead.
Change the src_w and src_h to be the size of the cropped image, not the full size.
I think Rovas missed the "size of my choosing" and suggested imagesx/y to find the smallest dimension (then you'd crop the longest one to match).