图像调整大小和高度和宽度的数组

时间:2013-09-10 10:41:11

标签: php arrays image resize

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

1 个答案:

答案 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;
    }
}