我正在建設一個畫廊,目標是低入口門檻。我的用戶是使用數碼相機拍照的人,因此文件大小在每張圖像200至400 KB之間。如何上傳/調整使用數碼相機拍攝的圖像?
我使用GD庫時遇到的問題是,當調整大小和上傳時,每個圖像使用大約90MB內存+服務器有64 MB限制時。
當我使用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);
因爲ImageMagick應該能夠處理400kb的圖像而沒有任何問題,所以您可能做得不夠高效。你想一次調整大小?你能發佈你的代碼嗎? – 2009-04-17 04:32:41
我假設你正在打開一個文件,但沒有關閉它或獲取那麼多的內存使用量。也許發佈你的代碼? – Anthony 2009-04-17 04:34:30