Image Thumbnails
PHP Code
+ 1 likes
Please Register to submit score.
| Average Score | 7.4 (of 5 scores) |
| Date Added | Mar 10, 2005 |
| Last Updated | Jun 07, 2006 |
| Tags | images |
Introduction
$infile = path to source image<br />
$outfile = file and path to write the thumbnail to<br />
$maxw = Maximum width of the thumbnail<br />
$maxh = Maximum height of the thumbnail<br />
$stretch = Stretch image if it is smaller than the maxw and maxh
The function will preserve the aspect ratio of the image. If you specify 0 for either $maxw or $maxh then the function will assume that the max width/height can be unlimited (ie, if you specify 0 for $maxw it will only resize the height to $maxh likewise for specifying 0 for $maxh)<br />
<br />
By default if the source image is already smaller than the thumbnail size it will not be stretched to the $maxw or $maxh. If you specify TRUE for $stretch it will be stretched if it is already smaller than the $maxw and $maxh parameters.<br />
<br />
This function supports PNG, JPEG and GIF formats. Adding new formats shouldn't be hard.
PHP Snippet:
Image Thumbnails
Posted on Mar 10, 2005 10:43 pm
Posted on Mar 10, 2005 10:43 pm
Awesome tye, this is very useful in a lot of different ways.
PHP Snippet:
Image Thumbnails
Posted on Mar 11, 2005 10:58 am
Posted on Mar 11, 2005 10:58 am
it could use a bit more comments but i still gave you a 7 :D it's good and very usefull
PHP Snippet:
Image Thumbnails
Posted on Apr 19, 2005 6:38 am
Posted on Apr 19, 2005 6:38 am
tye, looks like PHP supports GIF's now after GD version 2.0.28
PHP Snippet:
Image Thumbnails
Posted on Apr 19, 2005 8:03 am
Posted on Apr 19, 2005 8:03 am
tye, I found a bug in your code. I'm not sure if you meant to use $ImageFunc2 at the end or not, but it's not used at all. Also you set $ext to .jpg in your if conditiional. It should be ==
PHP Snippet:
Image Thumbnails
Posted on Apr 21, 2005 11:36 am
Posted on Apr 21, 2005 11:36 am
There's a problem for images that are taller than they are wide. I think you're missing an else statement there. Also when you set maxx, but you don't set maxy on images of this type, they aren't restricted to maxx. It tries to use the maxy value which is 0. I'm not sure how to fix this.
PHP Snippet:
Image Thumbnails
Posted on Apr 21, 2005 11:48 am
Posted on Apr 21, 2005 11:48 am
I figured it out. You don't consider if maxx == 0 or maxy == 0 in either of your cases. I'm sure there's a better way to organize this code.
PHP Snippet:
Image Thumbnails
Posted on Apr 21, 2005 12:34 pm
Posted on Apr 21, 2005 12:34 pm
I've written a better version of this snippet since I uploaded it. I'll post that soon. This one seems to have a few problems.
PHP Snippet:
Image Thumbnails
Posted on Apr 21, 2005 3:09 pm
Posted on Apr 21, 2005 3:09 pm
I updated the snippet to support GIFs and fix a few bugs. The new code has more error checking and more comments.
PHP Snippet:
Image Thumbnails
Posted on Apr 14, 2006 9:47 pm
Posted on Apr 14, 2006 9:47 pm
very nice, gave you an 8
PHP Snippet:
Image Thumbnails
Posted on May 11, 2006 1:28 am
Posted on May 11, 2006 1:28 am
tye, I think it's better to use imagecreate() rather than imagecreatetruecolor() because I had a couple cases where a gif with a transparent background was converted to a gif with a black background.
PHP Snippet:
Image Thumbnails
Posted on May 11, 2006 1:41 am
Posted on May 11, 2006 1:41 am
Actually, it might be more complicated than that. It seems imagecreatetruecolor is the way to go, but there has to be a way to retain the transparency.
PHP Snippet:
Image Thumbnails
Posted on May 14, 2006 10:37 pm
Posted on May 14, 2006 10:37 pm
Here are some changes I made to support GIF transparencies. I also changed the imagecopyresized function to imagecopyresampled because the quality of the shrunk images was fairly poor. This fixed it right up.
// Create new image
if($function == 'ImageCreateFromGif')
{
$colorTransparent = imagecolortransparent($source_img);
$new_img = imagecreate($neww,$newh);
imagepalettecopy($new_img,$source_img);
imagefill($new_img,0,0,$colorTransparent);
imagecolortransparent($new_img, $colorTransparent);
}
else
{
$new_img = imagecreatetruecolor($neww,$newh);
}
// Copy and resize image
imagecopyresampled($new_img,$source_img,0,0,0,0,$neww,$newh,$size[0],$size[1]);
// Create new image
if($function == 'ImageCreateFromGif')
{
$colorTransparent = imagecolortransparent($source_img);
$new_img = imagecreate($neww,$newh);
imagepalettecopy($new_img,$source_img);
imagefill($new_img,0,0,$colorTransparent);
imagecolortransparent($new_img, $colorTransparent);
}
else
{
$new_img = imagecreatetruecolor($neww,$newh);
}
// Copy and resize image
imagecopyresampled($new_img,$source_img,0,0,0,0,$neww,$newh,$size[0],$size[1]);
PHP Snippet:
Image Thumbnails
Posted on May 27, 2006 7:04 am
Posted on May 27, 2006 7:04 am
Thanks for the changes.
I put them in the snippet and extended them to work on transparent PNG images.
I put them in the snippet and extended them to work on transparent PNG images.
PHP Snippet:
Image Thumbnails
Posted on Jun 5, 2006 12:49 pm
Posted on Jun 5, 2006 12:49 pm
I've found a problem with the script (other than this small problem, this script is GREAT!).
I'll explain by example:
- Call the function with a max width of 250 and height of 150
- Input file that I call function with is 320x320
- The thumbnail generates the thumbnail at 250x250... ABOVE the maximum height of 150 pixels
Is this a bug? Or is this how it is supposed to work?
I'll explain by example:
- Call the function with a max width of 250 and height of 150
- Input file that I call function with is 320x320
- The thumbnail generates the thumbnail at 250x250... ABOVE the maximum height of 150 pixels
Is this a bug? Or is this how it is supposed to work?
PHP Snippet:
Image Thumbnails
Posted on Jun 6, 2006 12:48 pm
Posted on Jun 6, 2006 12:48 pm
Thanks for posting that bug. The scale_dimensions function wasn't working for some combinations of numbers. I've rewritten the function and tested it with every combination of numbers I could think of and it seems to work. If it doesn't please let me know.
PHP Snippet:
Image Thumbnails
Posted on Jun 7, 2006 4:21 pm
Posted on Jun 7, 2006 4:21 pm
Works great! Thanks tye, best script I've ever put to use.
PHP Snippet:
Image Thumbnails
Posted on Jul 5, 2006 7:03 am
Posted on Jul 5, 2006 7:03 am
Some hint if you want to use this code for true-color alpha channel transparent images (actually tried it on png-s).
After the
$new_img = imagecreatetruecolor($neww,$newh);
line I added these:
imagealphablending( $new_img, false );
$col = imagecolorallocatealpha( $new_img, 0, 0, 0, 127 );
imagefilledrectangle( $new_img, 0, 0, $neww, $newh, $col );
imagealphablending( $new_img, true );
imageSaveAlpha($new_img, true);
This fills the new image with fully transparent pixels.
The most painful was the last function call, because it took some half an hour to discover that without it the alpha channel isn't saved with the image.
After the
$new_img = imagecreatetruecolor($neww,$newh);
line I added these:
imagealphablending( $new_img, false );
$col = imagecolorallocatealpha( $new_img, 0, 0, 0, 127 );
imagefilledrectangle( $new_img, 0, 0, $neww, $newh, $col );
imagealphablending( $new_img, true );
imageSaveAlpha($new_img, true);
This fills the new image with fully transparent pixels.
The most painful was the last function call, because it took some half an hour to discover that without it the alpha channel isn't saved with the image.
PHP Snippet:
Image Thumbnails
Posted on Oct 21, 2009 7:47 am
Posted on Oct 21, 2009 7:47 am
hi,
sorry, but how i can use this script. which way is the right to implement?
thanks for helping me!
regards Chris
sorry, but how i can use this script. which way is the right to implement?
thanks for helping me!
regards Chris
PHP Snippet:
Image Thumbnails
Posted on Oct 21, 2009 8:51 am
Posted on Oct 21, 2009 8:51 am
cthaele, It's basically a function that you call with the required parameters.








