Re: Making Thumbnails
| From: | Rasmus Lerdorf | Date: | Mon, 24 Jul 2000 05:35:18 +0000 |
| Subject: | Re: Making Thumbnails | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-7827@lists.php.net to get a copy of this message | ||
> I have a form where I allow a user to upload either a jpg, png, or gif.
> I'd like to have PHP automatically generate a thumbnail to scale; however, I
> don't know what function to use.
There is no decent built in function to do this. I use the command line
conversion tools from the pbmplus package.
My thumbnail function looks like this:
/* globals */
$theight=96;
$djpeg = "/usr/bin/djpeg"; /* decompresses a jpeg to ppm format */
$cjpeg = "/usr/bin/cjpeg"; /* compreses a ppm to jpeg format */
$pnmscale = "/usr/bin/pnmscale"; /* scales a ppm image */
$giftopnm = "/usr/bin/giftopnm"; /* convert a gif to ppm format */
$ppmtogif = "/usr/bin/ppmtogif"; /* convert a ppm to gif format */
$ppmquant = "/usr/bin/ppmquant"; /* colour quantize a ppm image */
$pnmpad = "/usr/bin/pnmpad";
$shadow = "/usr/local/bin/shadow"; /* Put a drop-shadow on a GIF */
/*
* The thumbnail function uses the pnmscale program from
* the popular Netpbm image manipulation tools package
* to create a thumbnail of the given image. If a thumbnail
* already exists for the image, the function simply returns.
*/
function thumbnail($filename) {
global $theight, $djpeg, $cjpeg, $pnmscale, $giftopnm, $ppmtogif, $ppmquant, $pnmpad,
$shadow;
$tdir = dirname($filename)."/thumbnails"; /* thumbnail directory */
if(!filetype($tdir)) {
if(!@mkdir($tdir,0777)) {
echo "Unable to create $tdir directory - check permissions<br>\n";
return;
}
}
$tfile = $tdir."/".basename($filename); /* thumbnail file */
$tfile = ereg_replace("\.[^\.]+$",".gif",$tfile);
if(!filesize($tfile)) {
if(eregi("\.gif$",$filename)) { /* Look for .gif extension */
exec("$giftopnm $filename | $pnmscale -height $theight | $pnmpad -white -l6 -b6
| $ppmquant 248 | $ppmtogif | $shadow > $tfile");
} elseif(eregi("\.jpe?g",$filename)) { /* Look for .jpg or .jpeg extension */
exec("$djpeg $filename | $pnmscale -height $theight | $pnmpad -white -l6 -b6 |
$ppmquant 248 | $ppmtogif | $shadow > $tfile");
} else { /* not a GIF or JPG file */
return("");
}
}
return($tfile);
}
/usr/local/bin/shadow is pretty funny actually. It is a little script I
wrote to stick a drop shadow on a thumbnail. It was written as a bit of a
joke, but turned out to actually be somewhat useful:
#!/usr/local/bin/php -q
<?
$arr = array("000000",
"000011",
"001122",
"011334",
"124556",
"145666");
$ary = array("045566",
"034556",
"024556",
"011445",
"000124",
"000001");
$bot = "666541";
$im = imagecreatefromgif("/dev/stdin");
$black = ImageColorAllocate($im, 0,0,0);
$c0 = ImageColorAllocate($im, 255,255,255);
$c1 = ImageColorAllocate($im, 220,220,220);
$c2 = ImageColorAllocate($im, 185,185,185);
$c3 = ImageColorAllocate($im, 150,150,150);
$c4 = ImageColorAllocate($im, 135,135,135);
$c5 = ImageColorAllocate($im, 95,95,95);
$c6 = ImageColorAllocate($im, 45,45,45);
$x=0; $y=0;
$sy = imagesy($im);
$sx = imagesx($im);
/* top left */
while(list(,$v)=each($arr)) {
$x=0;
while($x<strlen($v)) {
ImageSetPixel($im,$x,$y,${"c".$v[$x]});
$x++;
}
$y++;
$last=$v;
}
/* left edge */
$x=0;
while($x<strlen($last)) {
ImageLine($im,$x,$y,$x,$sy-5,${"c".$last[$x]});
$x++;
}
$y=$sy-5;
/* bottom left */
while($y<=$sy) {
$x=0;
while($x<strlen($ary[$y-$sy+5])) {
$l = $ary[$y-$sy+5];
ImageSetPixel($im,$x,$y,${"c".$l[$x]});
$x++;
}
$y++;
}
/* bottom edge */
$y=$sy-6;
while($y<$sy) {
ImageLine($im,$x-1,$y,$sx-5,$y,${"c".$bot[$y-$sy+6]});
$y++;
}
/* bottom right */
$y=$sy-6;
while($y<$sy) {
$x=$sx-6;
while($x-$sx+6 < strlen($ary[$y-$sy+6])) {
$l = $ary[$y-$sy+6];
ImageSetPixel($im,$x,$y,${"c".$l[$sx-$x-1]});
$x++;
}
$y++;
}
ImageGif($im);
ImageDestroy($im);
?>
Sorry for the lack of comments in that second one. But you should be able
to read through it and figure out what it does. And when you do you will
see why it is so silly.
-Rasmus