PHP中的任意图像调整大小

3
什么是将图片大小调整为固定大小或至少适合固定大小的最佳方法?
这些图片来自于我无法控制的随机url,我必须确保图片不会超出大约250px x 300px的区域,或者说是图层的20% x 50%。
我认为应该首先确定图片尺寸,如果超出范围,则按比例缩小,但是我不确定如何处理逻辑,以便在图片尺寸可以是任何尺寸时进行调整。
编辑:我无法本地访问图片,并且图片url在一个变量中输出,需要一种指定宽度和高度标签值的方法。
6个回答

5
使用ImageMagick可以轻松实现。您可以通过exec使用convert命令行工具,或者使用http://www.francodacosta.com/phmagick/resizing-images 。如果使用'convert',您甚至可以告诉ImageMagick只调整较大的图像而不转换较小的图像:http://www.imagemagick.org/Usage/resize/ 如果您没有本地访问权限,则无法使用ImageMagick。
<?php
$maxWidth  = 250;
$maxHeight = 500;

$size = getimagesize($url);
if ($size) {
    $imageWidth  = $size[0];
    $imageHeight = $size[1];
    $wRatio = $imageWidth / $maxWidth;
    $hRatio = $imageHeight / $maxHeight;
    $maxRatio = max($wRatio, $hRatio);
    if ($maxRatio > 1) {
        $outputWidth = $imageWidth / $maxRatio;
        $outputHeight = $imageHeight / $maxRatio;
    } else {
        $outputWidth = $imageWidth;
        $outputHeight = $imageHeight;
    }
}
?>

phMagick看起来不错,但我没有直接访问图像的权限。它是从一个存储在变量中的URL中获取的,并且通过img src=...显示。 - user1253538
我编辑了我的答案,以处理非本地图像。希望能对您有所帮助。 - Sven Lilienthal
抱歉,之前的回答有误。只有在完全访问图片的情况下,ImageMagick 才有用。如果通过 HTML 中的 img 标签包含图像,则无法使用它。 - Sven Lilienthal
你可以在else块中包含die(print_r(error_get_last()));以获取调试输出。 - Sven Lilienthal
感谢提供最大宽度和高度的调整代码。当你过度疲劳时,逻辑思维就会飞出窗外,我自己没有机会把它组合起来;) - Kristian Rafteseth
显示剩余5条评论

1

1

如果您必须保持图像的纵横比不变,则应按比例缩放图像,使图像的较长一侧(高度或宽度)适合该侧的限制。


0

我不确定你问题的确切解决方案,但我知道有一个用PHP编写的项目有这个解决方案。去看看为Drupal CMS构建的ImageCache,它是用PHP编写的。

它基本上让你定义一些对任意图像要执行的操作,并且可以产生几乎任何缩放/裁剪你想要的图像。

你需要学习一些Drupal API才能理解这个例子,但它非常详尽,如果你理解了他们的算法,你就能解决其他更复杂的问题。


0
你可以使用以下内容:
$maxWidth  = 250;
$maxHeight = 500;

$validSize = true;
$imageinfo = getimagesize($filename);
if ($imageinfo) {
    $validSize &= $imageinfo[0] <= $maxWidth;
    $validSize &= $imageinfo[1] <= $maxHeight;
}

&= 是位运算符 & 和赋值运算符 = 的组合操作符。因此,$foo &= *expr*$foo = $foo & *expr* 是等价的。


我不确定我理解你的例子,你能否再解释一下? - user1253538
它只是检查图像的宽度和高度是否分别大于$maxWidth和$maxHeight。使用&运算符确保两个条件都为真。 - Gumbo

0
我创建了以下函数,以智能方式调整图像大小,保持宽高比,并具有一个参数来放大较小的图像(如果缩略图大小不正常会导致 HTML 布局出现问题,则此功能至关重要)。
function ImageIntelligentResize( $imagePath, $maxWidth, $maxHeight, $alwaysUpscale )
{
    // garbage in, garbage out
    if ( IsNullOrEmpty($imagePath) || !is_file($imagePath) || IsNullOrEmpty($maxWidth) || IsNullOrEmpty($maxHeight) )
    {
        return array("width"=>"", "height"=>"");
    }

    // if our thumbnail size is too big, adjust it via HTML
    $size = getimagesize($imagePath);
    $origWidth = $size[0];
    $origHeight = $size[1];

    // Check if the image we're grabbing is larger than the max width or height or if we always want it resized
    if ( $alwaysUpscale || $origWidth > $maxWidth || $origHeight > $maxHeight )
    {   
        // it is so let's resize the image intelligently
        // check if our image is landscape or portrait
        if ( $origWidth > $origHeight )
        {
            // target image is landscape/wide (ex: 4x3)
            $newWidth = $maxWidth;
            $ratio = $maxWidth / $origWidth;
            $newHeight = floor($origHeight * $ratio);
            // make sure the image wasn't heigher than expected
            if ($newHeight > $maxHeight)
            {
                // it is so limit by the height
                $newHeight = $maxHeight;
                $ratio = $maxHeight / $origHeight;
                $newWidth = floor($origWidth * $ratio);
            }
        }
        else
        {
            // target image is portrait/tall (ex: 3x4)
            $newHeight = $maxHeight;
            $ratio = $maxHeight / $origHeight;
            $newWidth = floor($origWidth * $ratio);
            // make sure the image wasn't wider than expected
            if ($newWidth > $maxWidth)
            {
                // it is so limit by the width
                $newWidth = $maxWidth;
                $ratio = $maxWidth / $origWidth;
                $newHeight = floor($origHeight * $ratio);
            }
        }
    }
    // it's not, so just use the current height and width
    else
    {
        $newWidth = $origWidth;
        $newHeight = $origHeight;
    }   

    return array("width"=>$newWidth, "height"=>$newHeight);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接