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?

Link to comment
Share on other sites

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]);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.