crwork Posted February 22, 2013 Share Posted February 22, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/274823-maintain-larger-image-aspect-ratio-in-smaller-fixed-frame/ Share on other sites More sharing options...
shlumph Posted February 22, 2013 Share Posted February 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274823-maintain-larger-image-aspect-ratio-in-smaller-fixed-frame/#findComment-1414168 Share on other sites More sharing options...
Stooney Posted February 22, 2013 Share Posted February 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274823-maintain-larger-image-aspect-ratio-in-smaller-fixed-frame/#findComment-1414171 Share on other sites More sharing options...
crwork Posted February 22, 2013 Author Share Posted February 22, 2013 Thanks folks, that sounds like the best solution to me as well. Quote Link to comment https://forums.phpfreaks.com/topic/274823-maintain-larger-image-aspect-ratio-in-smaller-fixed-frame/#findComment-1414189 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.