Jump to content

Maintain larger image aspect ratio in smaller fixed frame


crwork

Recommended Posts

I'm trying to resize a larger image into a small 24x24 avatar. Via imagecopyresampled, I was able to crop the larger image into a square and resize successfully so that the image wouldn't be distorted when transformed into a small avatar.

 

However, I was told I need to keep the original image aspect ratio as much as possible when fitting into a fixed 24x24 frame. So the avatar frame still needs to be 24x24 but the image that fits inside it, which will most likely be a landscape image, should retain the original aspect ratio as much as possible. This will no doubt result in a letterbox appearance.

 

With regard to the code, can this be done with imagecopyresampled so that the actual image, assuming landscape, doesn't fill up the entire 24x24 window and become distorted?

 

This is my current code that will crop a landscape large image into a square then resize down to 24x24:

imagecopyresampled($imNew, $im, 0, 0, $xoffset, 0, 24, 24, $origHeight, $origHeight);

However, I was told I need to keep the original image aspect ratio as much as possible when fitting into a fixed 24x24 frame. So the avatar frame still needs to be 24x24 but the image that fits inside it, which will most likely be a landscape image, should retain the original aspect ratio as much as possible.[/code]

So, the key phrase here is as much as possible, right? I would scale the image down, keeping the same ratio until either the width or height hits 24px. After that's done, I would crop off the excess.

I agree with shlumph. If landscape, width = original width - (original height - 24). Then use whatever library you use to crop the image to 24x24, most likely wanting to trim the edges and keeping the center most 24 pixels.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.