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

时间:2023-01-19 20:30:55

i have an array of $h and $w. it will only create the move_uploaded_file($_FILES['image']['tmp_name'],$_FILES['image']['name']); how can i use the array of sizes in creating new images?

我有一个$ 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 个解决方案

#1


0  

In your code snippet $w and $h do not appear to be arrays.

在你的代码片段中,$ w和$ h似乎不是数组。

Let assume that $w and $h are arrays of size $resize with different image sizes.

假设$ w和$ h是具有不同图像大小的大小$ resize的数组。

The following code also detects image MIME type using getimagesize function and resizes image of JPG, PNG and GIF formats.

以下代码还使用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;
    }
}

#1


0  

In your code snippet $w and $h do not appear to be arrays.

在你的代码片段中,$ w和$ h似乎不是数组。

Let assume that $w and $h are arrays of size $resize with different image sizes.

假设$ w和$ h是具有不同图像大小的大小$ resize的数组。

The following code also detects image MIME type using getimagesize function and resizes image of JPG, PNG and GIF formats.

以下代码还使用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;
    }
}