1

我有一个 $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);
     }
4

1 回答 1

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;
    }
}
于 2013-09-10T10:55:37.717 回答