Image Thumbnail Creator
Like many other people, I found Mike Lopez's image thumbnail script quite useful. He has taken the time to go through the brain-crunching algorithm to create image proportional image thumbnails when given a max height/width. Although this script is very handy, it was lacking in a few areas, such as transparent PNG and GIF support, as well as the un-needed resize if the image was smaller than the requested dimensions. I hope I have "helped" in this situation by modifying the script so that it handles such requests. Credit for the transparency support goes to Martin Schmidt (icheee).
One modification I made that is important to note is that the image is now fed through with the SRC variable, rather than the IMG variable. Thanks Mike for this script and I hope my updates really help.
-
<?php
-
/*
-
JPEG / GIF / PNG Resizer / Image Viewer
-
Parameters (passed via URL):
-
src = path / url of jpeg or png image file
-
percent = if this is defined, image is resized by it's
-
value in percent (i.e. 50 to divide by 50 percent)
-
w = image width
-
h = image height
-
thumb: set = displays thumbnail.
-
not set = displays original image.
-
force: by default this script will constrain the image proportions. If you wish to
-
force with width/height settings, set &force in the URL
-
width and height and thumb MUST be set.
-
Requires the PHP GD Extension
-
Originally By: Michael John G. Lopez - www.sydel.net
-
Modified By: Brian DiChiara - www.briandichiara.com
-
Modifications:
-
Supports Transparent PNG and GIF
-
Does not resize images smaller than specified w/h
-
Supports Remote files (http://www.notyoursite.com/image.jpg)
-
Usage Example:
-
<img src="image.php?src=myimage.jpg&thumb&w=200&h=200" border="0" alt="my image" />
-
*/
-
-
function getExtension($f){
-
if(!$pos) {
-
return '';
-
}
-
}
-
-
$img = $_GET['src'];
-
//$constrain = (isset($_GET['constrain'])) ? ($_GET['constrain'] == 0 || $_GET['constrain'] == "false" || $_GET['constrain'] == "0") ? false : true : true;
-
-
if($remote){
-
$data = '';
-
}
-
$src = @imagecreatefromstring($data);
-
$sw = @imagesx($src);
-
$sh = @imagesy($src);
-
} else {
-
-
$sw = $dims[0]; //returns width
-
$sh = $dims[1]; //returns height
-
}
-
-
-
$resize = false; //assume no resize is needed
-
-
if ($percent> 0) {
-
// calculate resized height and width if percent is defined
-
$percent = $percent * 0.01;
-
$w = $sw * $percent;
-
$h = $sh * $percent;
-
} else {
-
$resize = ($force) ? true : $resize;
-
// autocompute height if only width is set
-
$h = (100 / ($sw / $w)) * .01;
-
// autocompute width if only height is set
-
$w = (100 / ($sh / $h)) * .01;
-
// get the smaller resulting image dimension if both height
-
// and width are set and $constrain is also set
-
$hx = (100 / ($sw / $w)) * .01;
-
-
$wx = (100 / ($sh / $h)) * .01;
-
-
if ($hx <$h) {
-
$h = (100 / ($sw / $w)) * .01;
-
} else {
-
$w = (100 / ($sh / $h)) * .01;
-
}
-
}
-
} else {
-
$w = $sw;
-
$h = $sh;
-
}
-
}
-
-
switch ($ext){
-
case "jpg" : $type = "jpeg"; break;
-
case "bmp" : $type = "x-ms-bmp"; break;
-
case "png" : $type = "x-png"; break;
-
case "tif" : $type = "x-tiff"; break;
-
case "ico" : $type = "x-icon"; break;
-
default : $type = $ext; break;
-
}
-
-
-
//display contents
-
} else {
-
$im = ($remote) ? $src : false;
-
switch ($ext){
-
case "jpg" :
-
case "jpeg" : $im = @imagecreatefromjpeg($img); break;
-
case "wbmp" : $im = @imagecreatefromwbmp($img); break;
-
case "png" : $im = @imagecreatefrompng($img); break;
-
case "gif" : $im = @imagecreatefromgif($img); break;
-
default : $im = false; break;
-
}
-
-
-
if (!$im) {
-
// just return the contents of the actual image.
-
} else {
-
// Create the resized image destination
-
$thumb = @imagecreatetruecolor ($w, $h);
-
-
//preserve alpha opacity - thanks to Martin Schmidt
-
$colorcount = @imagecolorstotal($im);
-
@imagetruecolortopalette($thumb,true,$colorcount);
-
@imagepalettecopy($thumb,$im);
-
$transparentcolor = @imagecolortransparent($im);
-
@imagefill($thumb,0,0,$transparentcolor);
-
@imagecolortransparent($thumb,$transparentcolor);
-
-
// Copy from image source, resize it, and paste to image destination
-
@imagecopyresampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
-
-
// Output resized image
-
switch ($ext){
-
case "wbmp" : @imagewbmp ($thumb); break;
-
case "png" : @imagepng ($thumb); break;
-
case "gif" : @imagegif ($thumb); break;
-
default : @imagejpeg ($thumb); break;
-
}
-
-
imagedestroy($thumb);
-
imagedestroy($im);
-
}
-
}
-
?>
Last Update: 2008-01-10 23:12 CST
* fixed resize issue where only 1 dimension was provided.
October 19th, 2007 at 4:59 pm
You need to change the filename to download from image.php to another extension like image.phps in order for people to download…
I can’t copy and paste the actual text above because the text because all crumpled up.
October 20th, 2007 at 8:17 am
I changed the code highlighter plugin to something a little better. You should be able to copy it now.
October 20th, 2007 at 3:57 pm
I have made sure already that the image does exist. In my case, the image is on my server, but it will not always be this case. I’d like this to work with all images on any server.
Thanks.
October 21st, 2007 at 7:27 pm
The script has been updated to support remote images. Should work the exact same, just use a remote file URL rather than a relative URL in the SRC variable. Let me know if you have any questions.
January 4th, 2008 at 5:24 am
Hi,
great script, thanks for this!!!
need a feature, crop image
Thanks!
April 12th, 2008 at 4:36 pm
Great script! Especially the feature to resize remote images.
Haven’t seen that in the other scripts I’ve looked at (http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/, http://shiftingpixel.com/2008/03/03/smart-image-resizer/).
But those scripts do have file caching, is there anyone who knows how to add that to this script? I’ve tried combining some parts but not succesfully yet, so far…
April 16th, 2008 at 9:56 am
[...] http://blog.briandichiara.com/image-thumbnail-creator/ [...]
December 5th, 2008 at 2:22 pm
Thank you, thank you, thank you for modifying this script. It works perfectly.
March 16th, 2009 at 6:25 pm
Looks very nice Brian!
Have not tested it yet, but looks very nice. Just one question: What about at cache for the tumbnails? Is it just to change sometning in the code, make a folder (777) and it would work?
Why cache you said? Slow remote server…
Thanks:)
November 18th, 2009 at 7:55 am
Dear Brian, just a question since you are (and I am not, not yet) a CodeIgnitor user. I noticed that there is also an image maniputalor class in CI. Can that not do what your script above does?
Btw, nice that you also discovered PHPDesigner
December 3rd, 2009 at 12:38 am
Yes, Alexander. This post was written LONG before I discovered CI. CI rocks, although I actually haven’t messed with CI’s Image Manipulation Library very much. (not at all actually) You should give it a try!