图像调整大小并保持比例错误

时间:2014-01-07 21:09:50

标签: php image resize gd

我发现并修改了GD图像调整大小并保持比率脚本,但它不能正常工作。

例如,我想将图片调整为最大200w x 200h保持率。我想要调整大小的图片是518w x 691h,脚本应该将其调整为150w x 200h以保持纵横比,但是它将其调整为200w x 226h。有什么问题?

function resize_image($source_image, $name, $new_width, $new_height, $destination)
{
    list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image);

    switch($source_image_type)
    {
        case IMAGETYPE_GIF:
            $source_gd_image = imagecreatefromgif($source_image);
            break;
        case IMAGETYPE_JPEG:
            $source_gd_image = imagecreatefromjpeg($source_image);
            break;
        case IMAGETYPE_PNG:
            $source_gd_image = imagecreatefrompng($source_image);
            break;
    }

    $source_aspect_ratio = $source_image_width / $source_image_height;
    $thumbnail_aspect_ratio = $new_width / new_height;

    if($source_image_width <= $new_width && $source_image_height <= new_height)
    {
        $thumbnail_image_width = $source_image_width;
        $thumbnail_image_height = $source_image_height;
    }
    elseif ($thumbnail_aspect_ratio > $source_aspect_ratio)
    {
        $thumbnail_image_width = (int)(new_height * $source_aspect_ratio);
        $thumbnail_image_height = new_height;
    }
    else
    {
        $thumbnail_image_width = $new_width;
        $thumbnail_image_height = (int)($new_width / $source_aspect_ratio);
    }

    $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);


    imagealphablending($thumbnail_gd_image, false);
    imagesavealpha($thumbnail_gd_image, true);


    imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);


    $destination = $destination.$name;

    switch($source_image_type)
    {
        case IMAGETYPE_GIF:
            imagegif($thumbnail_gd_image, $destination);
            break;
        case IMAGETYPE_JPEG:
            imagejpeg($thumbnail_gd_image, $destination, 100);
            break;
        case IMAGETYPE_PNG:
            imagepng($thumbnail_gd_image, $destination, 9);
            break;
    }


    imagedestroy($source_gd_image);
    imagedestroy($thumbnail_gd_image);
}

1 个答案:

答案 0 :(得分:2)

本节:

elseif ($thumbnail_aspect_ratio > $source_aspect_ratio)

永远不应该执行,因为您希望保持纵横比相同。要确定新的宽度/高度,请尝试以下方法:

if($width > $MAX_SIZE || $height > $MAX_SIZE) {
    $aspect = $width / $height;
    if($width > $height) {
        $width = $MAX_SIZE;
        $height = intval($MAX_SIZE / $aspect);
    } else {
        $height = $MAX_SIZE;
        $width = intval($MAX_SIZE * $aspect);
    }
}

<强>更新

所有这些代码正在尝试根据限制$MAX_SIZE确定新的宽度/高度,同时保持纵横比相同。它不会是完美的,因为浮点数绝对很少(特别是因为在这种情况下你不能有'小数'像素,这就是为什么上面的计算使用intval)。例如,在运行此代码之前,请考虑$width$height$MAX_SIZE是否设置如下:

$MAX_SIZE = 100;
$width = 1920;
$height = 1080;

原始宽高比为1.77777777 ....运行上面的片段后,宽度/高度将设置为100 x 56,宽高比为1.7857。将输出宽度/高度向上或向下移动一个像素将无法获得精确的输入宽高比,除非您允许带有小数分量的像素值。

然而,您上传文件并确定输入文件的高度/宽度无关紧要,上面的代码段只能让您调整尺寸尽可能接近输入宽高比。