Making Square Thumbnails

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Making Square Thumbnails

Post 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.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Re: Making Square Thumbnails

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Making Square Thumbnails

Post by Eran »

Use PHP Image, it's an image manipulation library made by forum moderator onion2k. recommended :)
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Re: Making Square Thumbnails

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Making Square Thumbnails

Post 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).
Post Reply