I use the following two functions to upload and resize images on a web site.
function imageupload($temppath) {
if (is_uploaded_file($_FILES['userfile']['tmp_name']) &&
($_FILES['userfile']['type'] == "image/jpeg")) {
$file = $_FILES['userfile']['name'];
$short = substr("$file", -12, 12);
$fixed = str_replace(" ", "_", $short);
$realname = time().$fixed;
copy($_FILES['userfile']['tmp_name'], "..".$temppath.$realname);
unset($_FILES['userfile']['tmp_name']);
return $realname;
} else {
unset($_FILES['userfile']['tmp_name']);
return false;
}
}
function handleupload($temppath,$maxwidth,$maxheight) {
$realname = imageupload($temppath);
#Begin resizing
#$fullpath = $GLOBALS['fullpath'];
global $CFG;
$fullpath = $CFG['fullpath'];
$savepath = $temppath;
$filename = $fullpath . $savepath . $realname;
@list($width, $height, $type, $attr) = getimagesize($filename);
// Resize an image if it exceeds a predefined size.
if (file_exists($filename) && ($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); //this is line 393
$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;
} elseif ($currwidth == $currheight &&
($currheight > $maxheight || $currwidth > $maxwidth)) {
$nheight = $maxheight;
$nwidth = $maxheight;
} 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 . $realname;
unlink($filename); /* we need to delete the original file
/* now save to file and clear up the memory */
imagejpeg($newim,$newfilename,90);
ImageDestroy($im);
ImageDestroy($newim);
return $realname;
} else {
/* if the file wasn't a file return false */
return $realname;
}
#End resizing
}
Oddly, it works perfect when uploading images from a Mac (Safari). However, if I try to upload the same image from a Windows PC, I am given the following error, which then triggers several other errors.
Warning: imagecreatefromstring(): Passed data is not in 'WBMP' format in /home/aricandk/includes/functions/standard.php on line 393
I marked line 393 above. I have no idea why it would work from one client platform, but not another. Path problem?