Jump to content

[SOLVED] Issues cropping thumbnails to maintain aspect ratio


rtadams89

Recommended Posts

As part of my web gallery, I have a script which generates thumbnails from uploaded pictures. The uploaded pictures vary in dimensions from portrait to landscape. I want all the thumbnails to be exactly 100 x 66 pixels, and contain no white space or "filler." Additionally, I want to maintain the aspect ratio of the original image. Obviously, this means some cropping will occur with images that don't have a 100/66 aspect ratio.

 

I'm familiar with PHP, but not with the GD library, so I've been looking at example scripts to do this. I've found plenty that will create 100 x 66 thumbnails, but they all seem to either distort the aspect ratio to make the picture fit or include white space to pad the image. I have yet to find one that will properly crop and re size any image I throw at it to produce a thumbnail with as much of the original image as possible, while maintaining the aspect ratio.

 

Can anyone point me to such a script?

Shouldn't be a problem, just calculate what height the "scaled down" version will be when width=100. If it's smaller than 66 then you need to scale the image to height=66 and then crop it using something like:

 

<?php

$src = imagecreatefromjpg('gallery_image.jpg');

$size = getimagesize('gallery_image.jpg');

 

$dest = imagecreatetruecolor(100, 66);

 

imagecopy($dest, $src, 0, 0, 100, 66, $size[0], $size[1]);

?>

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.