我有一个$ h和$ w的数组。它只会创建move_uploaded_file($_FILES['image']['tmp_name'],$_FILES['image']['name']);
如何在创建新图像时使用大小数组?
for ($resize=1; $resize <= $units; $resize++)
{
$new = $album_name."_".$image_name."_".$resize.".".$image_ext;
$targetImage = imagecreatetruecolor($w, $h);
$image = imagecreatefromstring(file_get_contents($target));
imagecopyresampled($targetImage,$image, 0, 0, 0, 0, $w, $h, $originalWidth, $originalHeight);
imagejpeg($targetImage,'uploads/'.$albumName.'/'.$new,80);
}
答案 0 :(得分:0)
在您的代码段中$w
和$h
似乎不是数组。
假设$w
和$h
是大小为$resize
且数量不同的数组。
以下代码还使用getimagesize
功能检测图像MIME类型,并调整JPG,PNG和GIF格式的图像。
for ($resize=1; $resize <= $units; $resize++)
{
$size = getimagesize($target);
if ($size === false)
return false;
$format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
$icfunc = "imagecreatefrom" . $format;
if (!function_exists($icfunc))
return false;
$new = $album_name."_".$image_name."_".$resize.".".$image_ext;
$targetImage = imagecreatetruecolor($w[$resize], $h[$resize]);
$image = $icfunc($target);
imagecopyresampled($targetImage,$image, 0, 0, 0, 0, $w[$resize], $h[$resize], $originalWidth, $originalHeight);
switch ($size['mime']) {
case 'image/jpg':
case 'image/jpeg':
case 'image/pjpeg':
return imagejpeg($dst_r, 'uploads/'.$albumName.'/'.$new, 80);
break;
case 'image/png';
return imagepng($dst_r, 'uploads/'.$albumName.'/'.$new, 2);
break;
case 'image/gif';
return imagegif($dst_r, 'uploads/'.$albumName.'/'.$new);
break;
default:
return;
}
}