Page 1 of 1

Image functions - extend canvas

Posted: Tue May 18, 2004 1:33 pm
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?

Posted: Tue May 18, 2004 5:52 pm
by Unipus
So I'm guessing that's a "no" then.

Posted: Tue May 18, 2004 6:02 pm
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.

Posted: Wed May 19, 2004 3:08 am
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.