【问题标题】:Find new proportional dimensions of an image in PHP在 PHP 中查找图像的新比例尺寸
【发布时间】:2013-05-14 16:31:04
【问题描述】:

我有一个网站,我想上传一张图片并调整它的大小,因为我必须将它放入一个具有特定尺寸的 div 中。

例如我的最大宽度是 200 像素,最大高度是 100 像素

我想上传图像并检查宽度和高度,如果它们大于最大宽度或最大高度,我想找到图像的大小以保留在该 div 内。

如何按比例调整图像大小? 我只想在我的 div 200px*100px 的基础上找到新的宽度和新的高度

这是我的脚本:

            if(move_uploaded_file($tmp, $path.$actual_image_name))
            {
                list($width, $height, $type, $attr) = getimagesize($tmp);
                if($width>200){
                    //too large I want to resize
                }
                if($height>100){
                    //too height I want to resize
                }
                echo(base_url().$path.$actual_image_name);
            }

【问题讨论】:

  • 你在问数学问题吗?您想知道如何计算比率吗?
  • 我建议使用 jQuery 来调整图像大小...这会在您的服务器上投入大量工作...我的意思是可以有一张图片但是如果您有更多图片怎么办... .
  • 我不能在这个上下文中使用 jQuery,因为我必须在不同的模式下管理这个图像@user123_456
  • 然后查看这篇文章:white-hat-web-design.co.uk/blog/resizing-images-with-php 这有帮助吗?
  • 有也没有,我想知道有没有办法按比例调整大小@F4r-20

标签: php image getimagesize


【解决方案1】:

您可以使用以下函数将其留在边界框内。例如 200x200。只需发送文件位置和最大宽度和高度。它将返回一个数组,其中$ar[0] 是新宽度,$ar[1] 是新高度。

它写得很完整,所以你可以理解数学。

<?php
function returnSize($maxW,$maxH,$img) {
    $maxW = ($maxW>0 && is_numeric($maxW)) ? $maxW : 0;
    $maxH = ($maxH>0 && is_numeric($maxH)) ? $maxH : 0;

    // File and new size
    if (!file_exists($img)) {
        $size[0]=0;
        $size[1]=0;
        return $size;
    }

    $size = getimagesize($img);

    if ($maxW>0 && $maxH>0) {
        if ($size[0]>$maxW) {
            $scaleW = $maxW / $size[0];
        } else {
            $scaleW = 1;
        }

        if ($size[1]>$maxH) {
            $scaleH = $maxH / $size[1];
        } else {
            $scaleH = 1;
        }

        if ($scaleW > $scaleH) {
            $fileW = $size[0] * $scaleH;
            $fileH = $size[1] * $scaleH;
        } else {
            $fileW = $size[0] * $scaleW;
            $fileH = $size[1] * $scaleW;
        }

    } else if ($maxW>0) {
        if ($size[0]>$maxW) {
            $scaleW = $maxW / $size[0];
        } else {
            $scaleW = 1;
        }

        $fileW = $size[0] * $scaleW;
        $fileH = $size[1] * $scaleW;

    } else if ($maxH>0) {
        if ($size[1]>$maxH) {
            $scaleH = $maxH / $size[1];
        } else {
            $scaleH = 1;
        }

        $fileW = $size[0] * $scaleH;
        $fileH = $size[1] * $scaleH;

    } else {
        $fileW = $size[0];
        $fileH = $size[1];
    }

    $size[0] = $fileW;
    $size[1] = $fileH;

    return $size;
}
?>

【讨论】:

  • 这个功能很棒,调整大小的数学更清晰,我可以做所有!非常感谢 +1
【解决方案2】:

这是计算比率的基础知识(保持不变的比例):

if($width>200){
    $percentage = (200/$width)*100; //Work out percentage
    $newWidth = 200; // Set new width to max width
    $newHeight = round($height*$percentage); //Multiply original height by percentage
}
else if($height>100){
    $percentage = (100/$height)*100; //Work out percentage
    $newHeight = 100; // Set new height to max height
    $newWidth = round($width*$percentage); //Multiply original width by percentage
}

我使用round() 来确保您收到仅整数的新维度

【讨论】:

  • 感谢这项工作,但我认为 Hugo 的功能更好,但感谢您的帮助 +1 解决方案
  • 考虑一个宽度为 800,高度为 200 的图像。现在第一个 if 会将 $newWidth 和 $newHeight 值更改为适合 (200x50) 的值,因为百分比是 25% .但是您的第二个 if 将再次将其更改为没有的内容。您的$newWidth 值将是 400px (400x100),因为高度百分比仅为 50%。
【解决方案3】:

这里有一些选项...

第一个:http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

第二个:http://www.sitepoint.com/image-resizing-php/ - 所有的数学工作都已经完成了

【讨论】:

    猜你喜欢
    • 2018-10-13
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多