【问题标题】:Arbitrary image resizing in PHPPHP中的任意图像大小调整
【发布时间】:2013-11-25 19:45:18
【问题描述】:

将图像(可以是任何尺寸)调整为固定大小或至少适合固定大小的最佳方法是什么?

图片来自不受我控制的随机网址,我必须确保图片不会超出大约 250 像素 x 300 像素或层的 20% x 50% 的区域。

我认为我会首先确定大小,如果它超出范围,则按一个因子调整大小,但如果图像大小可以是任何值,我不确定如何制定调整大小的逻辑。

编辑:我没有对图像的本地访问权限,并且图像 url 在一个变量中,以 img src=... 输出,我需要一种方法来指定宽度和高度标签的值。

【问题讨论】:

    标签: php


    【解决方案1】:

    使用 ImageMagick 很容易做到。您可以通过 exec 使用 convert 命令行工具,也可以使用 http://www.francodacosta.com/phmagick/resizing-images
    如果您使用“转换”,您甚至可以告诉 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=...
    • 我编辑了我的答案以处理非本地图像。希望对你有帮助。
    • 这些功能中哪些来自phmagick,phmagick还有必要吗?您的示例与francodacosta.com/phmagick/resizing-images 的示例完全不同
    • 抱歉,已编辑答案。 ImageMagick 仅在您对图像具有完全访问权限时才有用。如果您在 html 中通过 img-tag 包含图像,您将无法使用它。
    • PHP 的 Imagick 扩展能够从原始字符串加载图像数据。意思是,你可以 file_get_contents() 图像然后修改它。
    【解决方案2】:

    您是否查看过 GD 库文档,尤其是 imagecopyresized 方法?

    【讨论】:

      【解决方案3】:

      如果您必须保持图像的纵横比不变,则应将其缩放一个因子,以使图像的较长边(高度或宽度)符合该边的限制。

      【讨论】:

        【解决方案4】:

        您可以使用以下内容:

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

        &amp;=bitwise-and operator &amp;assignment operator = 的组合运算符。因此$foo &amp;= *expr*$foo = $foo &amp; *expr* 是等价的。

        【讨论】:

        • 我不明白你的例子,你能解释一下吗?
        • 只是检查图像的宽度和高度是否分别大于$maxWidth和$maxHeight。 & 运算符用于确保两个条件都为真。
        【解决方案5】:

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

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

        您必须学习一些 Drupal API 才能理解这个示例,但它非常广泛,如果您了解它们的算法,您就能够解决其他更复杂的问题。

        【讨论】:

          【解决方案6】:

          我创建了以下函数来根据比例智能调整图像大小,甚至还有一个参数可以放大较小的图像(如果您的 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);
          }
          

          【讨论】:

            猜你喜欢
            • 2011-09-11
            • 2011-11-25
            • 2012-01-09
            • 1970-01-01
            • 1970-01-01
            • 2013-01-16
            • 2014-08-07
            相关资源
            最近更新 更多