【问题标题】:Image resize and keep ratio bug图像调整大小和保持比例错误
【发布时间】:2014-01-07 21:09:49
【问题描述】:

我找到并修改了一个 GD 图像调整大小和保持比例脚本,但它没有按应有的方式工作。

例如,我想将图片调整为 MAXIMUM 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);
}

【问题讨论】:

    标签: php image resize gd


    【解决方案1】:

    本节:

    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....运行上面的sn-p后,宽/高会设置为100 x 56,也就是1.7857的宽高比。将输出宽度/高度向上或向下移动一个像素将永远无法获得确切的输入纵横比,除非您允许像素值具有小数部分。

    但是,您上传文件并确定输入文件的高度/宽度并不重要,上面的 sn-p 只应该让您调整后的尺寸尽可能接近输入的纵横比。

    【讨论】:

    • 我应该用你写的内容替换 elseif 下面的所有内容吗?你能修改我的脚本吗?谢谢!
    • 我会使用上面的代码来确定缩略图的宽度和高度(基本上是整个 if/elseif 结构)。你甚至可以把它放在一个单独的函数中,比如resizeWithAspect
    • 今天在上传一些照片时,我发现即使使用您的代码,问题仍然存在。我该如何解决这个问题??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 2017-08-24
    • 2011-04-27
    • 2013-06-26
    • 2012-04-15
    相关资源
    最近更新 更多