我正在建造一个画廊,目标是降低进入门槛。我的用户是使用数码相机拍照的人,因此每张图像的文件大小在 200 - 400 KB 之间。
我在使用 GD 库时遇到的问题是,当服务器有 64 MB 的限制时,每个图像在调整大小和上传时使用大约 90MB 的内存+。
当我使用 ImageMagick 时,它会超时并引发内部服务器错误。
我想知道是否有人对上传/调整如此大的图像大小有任何经验,可以给我一些指示。
谢谢,
列维
编辑:这是我要上传的代码
/** Begin Multiple Image Upload**/
$numberImages = count($_FILES['galFile']['name'])-1;
for($i=1;$i<=$numberImages;$i++)
{
$imageName = $_FILES['galFile']['name'][$i];
$imageType = $_FILES['galFile']['type'][$i];
$imageSize = $_FILES['galFile']['size'][$i];
$imageTemp = $_FILES['galFile']['tmp_name'][$i];
$imageError = $_FILES['galFile']['error'][$i];
//Make sure it is an image
if(in_array(end(explode(".", $imageName)), $allowed))
{
//Where to upload image to
$uploadFile = $uploadDir . $imageName;
if (file_exists($uploadFile))
{
//What to do if file already exists
//Append random number to the end
$front = explode(".", $imageName);
$randomNum = rand(1,100);
$front[0] = $front[0].$randomNum;
$imageName = $front[0].".".$front[1];
$uploadFile = $uploadDir . $imageName;
}
if(move_uploaded_file($imageTemp,$uploadFile))
{
//Add $imageName to DB
$query = "INSERT INTO galleryImages VALUES(\"0\",\"$lastInsert\",\"$imageName\",\"$i\")";
mysql_query($query);
reSizePic($uploadFile);
}
}
}
这是我用来调整大小的 GD 代码:
function reSizePic($image)
{
$source_pic = $image;
$destination_pic = $image;
$max_width = 660;
$max_height = 500;
$src = imagecreatefromjpeg($source_pic);
list($width,$height)=getimagesize($source_pic);
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if(($width <= $max_width) && ($height <= $max_height))
{
$tn_width = $width;
$tn_height = $height;
}
elseif (($x_ratio * $height) < $max_height)
{
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
else
{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
$tmp = imagecreatetruecolor($tn_width,$tn_height);
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);
imagejpeg($tmp,$destination_pic,100);
imagedestroy($src);
imagedestroy($tmp);
}
这是我用来调整大小的 ImageMagick 代码:
$resource = NewMagickWand();
MagickReadImage($resource,$image);
MagickSetImageCompressionQuality( $resource, 100);
$resource = MagickTransformImage($resource,'0x0','660x500');
MagickWriteImage($resource, $image);
DestroyMagickWand($resource);