I have a function here that will automatically upload, rename, and resize JPGs. Currently, it will not resize a JPG that has been saved from Photoshop. It will upload the file and rename it, but it won't resize it. Digital camera JPGs seem to work fine. Any ideas?
<?
function thumbnail($realname,$maxwidth,$maxheight,$savepath,$originalpath) {
include("config.php");
$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
}
?>