Image functions - extend canvas

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
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Image functions - extend canvas

Post by Unipus »

I want to write an evaluative function that takes an uploaded image and examines its dimensions. Easy enough. The tricky part is if the image isn't perfectly square, or is below a certain size, I want to extend the canvas to that size, filled with a solid color. From what I've read of the image functions, there's no way to do this, but maybe someone knows something I don't?
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

So I'm guessing that's a "no" then.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

seems quite possible, I'm just not that up on the image functions yet to give an exact route with functions..
  1. grab the dimensions with getimagesize()
  2. if the image is the right size, return
  3. if not, create an image resource from it
  4. create a new image with the dimensions desired
  5. fill the new image with a solid color
  6. copy in the image resource onto the new image.
  7. spit it out somewhere.
the manual's example here, sorta does half this.
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

It's perfectly do-able. Get the ratio of the max width/height you want the image to be. Compare that ratio with the same ratio of the image you want to resize. If the image has a smaller ratio, it's taller than it is wider, so you know you have to resize its height to your max height. Using the ratio, put your max height in, and you get your new width. Create a new image with the right background colour you want and max width/height dimensions, and copy the original image there, starting at ((max width)-(new width))/2, ((max height)-(new height))/2, and it'll always perfectly centre it. You end up with the original image resized to fit in your pre-defined size, with the spaces coloured as you want.
Post Reply