我正在开发一个网站,用户可以将多个图像上传到服务器/网站,在我的上传脚本中,我将图像大小限制为 10MB。这是因为我认为很多现代相机都会拍摄大图像。
上传脚本一次拍摄一张图片,并将其大小调整为 3 个不同的版本,900x600、600x450 和一个较小的缩略图,并在 2 个较大的图像的顶部放置一个水印图像。
我将 php.ini memory_limit 设置为 96MB,我认为这很容易。
经过一番测试,我上传了一张 6.38MB 大小的 jpg 图像,分辨率为 6143 x 3855 像素。我收到错误消息“致命错误:允许的内存大小为 100663296 字节已用尽(尝试分配 24572 字节)”
您认为需要这么多内存来上传和处理这种大小的图像是否合理?还是您认为这更有可能是我脚本中的编码问题?
96 MB 内存限制对我来说似乎很多。其他人处理大图像上传的经验是什么?我应该将内存限制设置为 128MB 或更高吗?或者我应该看看重写我的上传脚本?
我的代码添加如下:
//If a new image has been added, resize and upload to filesystem
if ($_FILES['new_image']['name'] !=''){
$allowed_types=array(
'image/gif' => '.gif',
'image/jpeg' => '.jpg',
'image/png' => '.png',
'image/x-png' => '.png',
'image/pjpeg' => '.jpg'
);
$img = $_FILES['new_image'];
// Check the file to be uploaded is the correct file type and is under 9MB
if ((array_key_exists($img['type'], $allowed_types)) && ($img['size'] < 9000000)) {
// File to be uploaded is Valid
// File to be uploaded is Valid
$imagename = stripslashes($_FILES['new_image']['name']);
// make the random file name
$randName = md5(rand() * time());
$ext = pathinfo($imagename, PATHINFO_EXTENSION);
$imagename = $randName . "." . $ext;
$source = $_FILES['new_image']['tmp_name'];
// Check if Directory Exists, if not create it
if(!file_exists("images/breeds/".$trimmed['profile_id']))
{
mkdir("images/breeds/".$trimmed['profile_id']) or die("Could not create images folder for article ".$trimmed['profile_id']);
}
// Check if thumbnail Directory Exists
if(!file_exists("images/breeds/".$trimmed['profile_id']."/thumbs"))
{
mkdir("images/breeds/".$trimmed['profile_id']."/thumbs") or die("Could not create thumbnail folder for article ".$trimmed['profile_id']);
}
// Check if thumbnail Directory Exists
if(!file_exists("images/breeds/".$trimmed['profile_id']."/large"))
{
mkdir("images/breeds/".$trimmed['profile_id']."/large") or die("Could not create thumbnail folder for article ".$trimmed['profile_id']);
}
$LargeImage = "images/breeds/".$trimmed['profile_id']."/large/".$imagename;
$NormalImage = "images/breeds/".$trimmed['profile_id']."/".$imagename;
$SmallImage = "images/breeds/".$trimmed['profile_id']."/thumbs/".$imagename;
//uploaded temp file
$file = $_FILES['new_image']['tmp_name'];
//Get Image size info
list($width, $height, $image_type) = getimagesize($file);
//SourceImage
switch ($image_type)
{
case 1: $image = imagecreatefromgif($file); break;
case 2: $image = imagecreatefromjpeg($file); break;
case 3: $image = imagecreatefrompng($file); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}
// Constraints for Large Image
$max_width = 900;
$max_height = 600;
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);
if (($height < $max_height) && ($width < $max_width)) {
//keep same dimensions
$modwidth = $width;
$modheight = $height;
} else {
// New dimensions
$modwidth = intval($ratio*$width);
$modheight = intval($ratio*$height);
}
$tmpLarge = imagecreatetruecolor( $modwidth, $modheight );
imagecopyresampled($tmpLarge, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
// Add Watermark to large image at top right
$wm = "images/p4h-wm-200.png";
$wmImage = imagecreatefrompng($wm);
$wmW = imagesx($wmImage);
$wmH = imagesy($wmImage);
$photoW = imagesx($tmpLarge);
$photoH = imagesy($tmpLarge);
$dest_x = $photoW - $wmW - 10;
$dest_y = 10;
// imagecopymerge($tn, $wmImage, $dest_x, $dest_y, 0, 0, $wmW, $wmH, 100);
imagecopy($tmpLarge, $wmImage, $dest_x, $dest_y, 0, 0, $wmW, $wmH);
switch ($image_type)
{
case 1: imagegif($tmpLarge,$LargeImage); break;
case 2: imagejpeg($tmpLarge,$LargeImage, 80); break;
case 3: imagepng($tmpLarge,$LargeImage, 0); break;
default: trigger_error('Failed resize image!', E_USER_WARNING); break;
}
// Destroy tmp images to free memory
imagedestroy($tmpLarge);
imagedestroy($wmImage);
// Constraints for Normal Image
$max_width = 550;
$max_height = 413;
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);
if (($height < $max_height) && ($width < $max_width)) {
//keep same dimensions
$modwidth = $width;
$modheight = $height;
} else {
// New dimensions
$modwidth = intval($ratio*$width);
$modheight = intval($ratio*$height);
}
$tmpNormal = imagecreatetruecolor( $modwidth, $modheight );
imagecopyresampled($tmpNormal, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
// Add Watermark to large image at top right
$wm = "images/p4h-wm-150.png";
$wmImage = imagecreatefrompng($wm);
$wmW = imagesx($wmImage);
$wmH = imagesy($wmImage);
$photoW = imagesx($tmpNormal);
$photoH = imagesy($tmpNormal);
$dest_x = $photoW - $wmW - 10;
$dest_y = 10;
// imagecopymerge($tn, $wmImage, $dest_x, $dest_y, 0, 0, $wmW, $wmH, 100);
imagecopy($tmpNormal, $wmImage, $dest_x, $dest_y, 0, 0, $wmW, $wmH);
switch ($image_type)
{
case 1: imagegif($tmpNormal,$NormalImage); break;
case 2: imagejpeg($tmpNormal,$NormalImage, 90); break;
case 3: imagepng($tmpNormal,$NormalImage, 0); break;
default: trigger_error('Failed resize image!', E_USER_WARNING); break;
}
// Destroy tmp images to free memory
imagedestroy($tmpNormal);
imagedestroy($wmImage);
// Now that the full size image has been saved, resize the thumbnail one to a fixed size for homepage display
// Constraints
$thumb_width = 150;
$thumb_height = 112.5;
// Calculate stuff and resize image accordingly
$src_ratio = $width/$height;
$dst_ratio = $thumb_width/$thumb_height;
if($src_ratio < $dst_ratio) // trim top and bottom
{
$ratio = $width/$thumb_width;
$crop_height = $thumb_height*$ratio;
$src_y = round(($height-$crop_height)/2);
$crop_width = $width;
$src_x = 0;
}
else // trim left and right
{
$ratio = $height/$thumb_height;
$crop_width = $thumb_width*$ratio;
$src_x = round(($width-$crop_width)/2);
$crop_height = $height;
$src_y = 0;
}
$tmpSmall = imagecreatetruecolor( $thumb_width, $thumb_height );
imagecopyresampled($tmpSmall, $image, 0, 0, $src_x, $src_y, $thumb_width, $thumb_height, $crop_width, $crop_height);
switch ($image_type)
{
case 1: imagegif($tmpSmall,$SmallImage); break;
case 2: imagejpeg($tmpSmall,$SmallImage, 90); break;
case 3: imagepng($tmpSmall,$SmallImage, 0); break;
default: trigger_error('Failed resize image!', E_USER_WARNING); break;
}
// Destroy images to free memory
imagedestroy($image);
imagedestroy($tmpSmall);