I have a script that will resize photos to create thumbnails using GD2. (JPG only, of course)... but it doesn't always work. Most JPGs do work, but some do not. And I have no idea why! It does copy the file and rename it, but it won't resize. I've attached a file it will not resize (u2.jpg). And the code is below. Why is this so inconsistent?
function thumbnail($realname,$maxwidth,$maxheight,$savepath,$originalpath) {
$fullpath = "/full/path/to/my/directory"; /***** With no trailing slash *****/
$thumb = $fullpath . $savepath . "t" . $realname;
$filename = $fullpath . $originalpath . $realname;
$thumbname = "t" . $realname;
@list($width, $height, $type, $attr) = getimagesize($thumb);
#Begin resizing
// Resize an image if it exceeds a predefined size.
if (file_exists($filename) && (!file_exists($thumb)) && ($type = 2)) { /* check if the file is really one */
$fd = @fopen($filename,"r");
$image_string = fread($fd,filesize($filename));
fclose($fd);
$im = ImageCreateFromString($image_string);
$currwidth = ImageSX($im);
$currheight = ImageSY($im);
/* now we have to check if the image
is higher than wider and larger
than the defined size, then
calculte it and resize it proportional */
if ($currwidth > $currheight && $currwidth > $maxwidth) {
$percent = ($maxwidth * 100) / $currwidth;
$nwidth = $maxwidth;
$nheight = ($percent * $currheight) / 100;
} elseif ($currwidth < $currheight && $currheight > $maxheight) {
$percent = ($maxheight * 100) / $currheight;
$nheight = $maxheight;
$nwidth = ($percent * $currwidth) / 100;
} else {
$nheight = $currheight;
$nwidth = $currwidth;
}
$nwidth = intval($nwidth);
$nheight = intval($nheight);
/* ok. let's create the new smaller image
with calculated size... */
$newim = imagecreatetruecolor ($nwidth - 1, $nheight - 1);
/* ...and put the larger image into the
smaller one. */
imagecopyresized($newim,$im,0,0,0,0,$nwidth,$nheight,$currwidth,$currheight);
$newfilename = $fullpath . $savepath . "t" . $realname;
/* now save to file and clear up the memory */
imagejpeg($newim,$newfilename,90);
ImageDestroy($im);
ImageDestroy($newim);
return $thumbname;
} elseif (file_exists($thumb)) {
return $thumbname;
} else {
/* if the file wasn't a file return false */
return false;
}
#End resizing
}