【问题标题】:Image resizing question using php?使用php调整图像大小的问题?
【发布时间】:2009-11-11 09:32:09
【问题描述】:

我想知道如何使用 getimagesize() 创建一段 PHP 代码来检查 jpg、gif、png 或任何其他类型的图像。

然后检查图像宽度是否小于以下 CSS 代码中的宽度,如果是,则 PHP 代码应在保持图像纵横比的同时重新调整图像大小,无论是横向还是纵向。

CSS 代码

overflow: hidden;  width:100px; height: auto;

我发现下面列出的这段代码似乎在使用 getimagesize() 但它似乎没有重新调整图像大小。有什么办法可以解决这个问题。

<?php

function imageResize($width, $height, $target) {

//takes the larger size of the width and height and applies the  
//formula accordingly...this is so this script will work  
//dynamically with any size image

if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}

//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);

//returns the new sizes in html image tag format...this is so you
//can plug this function inside an image tag and just get the

return "width=\"$width\" height=\"$height\"";

}

?>

<?php

//get the image size of the picture and load it into an array
$mysock = getimagesize("images/sock001.jpg");

?>

<!--using a standard html image tag, where you would have the  
width and height, insert your new imageResize() function with  
the correct attributes -->

<img src="images/sock001.jpg" <?php imageResize($mysock[0],  
$mysock[1], 150); ?>>

【问题讨论】:

    标签: php css


    【解决方案1】:

    您也可以使用 CSS 设置图像的大小。无论哪种方式,当您放大图像时,图像的质量都会下降。

    【讨论】:

    • 我想知道如何检查图像宽度是否小于 100 像素,如果是,请不要重新调整大小。
    • 要是能像电影里那样工作就好了Zoom!提高!旋转 90 度到 z 维度!
    • 所以,实际上,您要检查的是图像是否 > 100px。如果是这样,调整到 100px 宽度?在这种情况下,请检查图像的宽度。如果大于 100 像素,则使用 CSS 将图像的宽度设置为 100 像素。否则,别管它。唯一可能出现问题的情况是您的文件很大(并且希望避免加载 5MB 的图像)。
    • 你有没有比较过用 PHP 重绘和用 HTML 缩小图像的质量?在 PHP 中重绘总是比在客户端调整大小更可取。
    • 我很清楚图像插值的问题。我提个建议。在某些情况下,只使用 CSS 来完成这项工作更有意义。这可能是也可能不是这里的情况,但值得注意。
    【解决方案2】:

    您可以尝试使用 PHP ImageMagick Library's resizeImage method,或者更好的是 scaleImage method。也就是说,如果我的理解是正确的,那么您确实想要调整图像的大小。

    如果您只是在谈论在演示阶段调整它的大小,那么this article 似乎在谈论这个。它使用getimagesize 获取尺寸,然后返回一个字符串,您将相应地传递给img 标记。我相信这对你会有一些用处。

    【讨论】:

    • 我阅读并尝试了该代码,如您在上面看到的代码来自那里,但我已经玩过它,但由于某种原因它不会重新调整图像大小?
    • 不要重新发明轮子。使用 ImageMagick!
    【解决方案3】:

    您的问题是如何询问服务器图像是否大于某个宽度,以便您知道不要使用 CSS/HTML 来调整这些图像的大小。

    更好的方法是使用服务器按比例缩小任何太大的图像。如果您可以相信您的服务器将始终执行此操作,那么您无需在浏览器中进行任何调整大小。

    【讨论】:

      【解决方案4】:

      我为我们的内部框架构建了一个自定义 Image 类,它使用 Simon Jarvis 的代码来实现图像大小调整的基础知识。

      您可以在此处找到示例代码和教程:

      http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php

      基本上,您想要做的事情的魔力归结为我在我们的图像类中使用的以下方法(任何类属性都在其他地方设置,但如果需要,您可以硬编码这些值):

      public function resize($width,$height) 
      {
        $new_image = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_image, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->_image = $new_image;
      
        unset($new_image);
      
        return $this;   
      }
      
      public function resizeToHeight($height) 
      {
        $ratio = $height / $this->getHeight();
        $width = $this->getWidth() * $ratio;
        $this->resize($width,$height);
      
        return $this;
      }
      
      public function resizeToWidth($width) 
      {
        $ratio = $width / $this->getWidth();
        $height = $this->getheight() * $ratio;
        $this->resize($width,$height);
      
        return $this;
      }
      
      public function output()
      {   
          if($this->_image) {
              ob_clean();
              ob_end_clean();
      
              // Start sending headers
              header("Pragma: public"); // required
              header("Expires: 0");
              header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
              header("Cache-Control: private",false); // required for certain browsers
              header("Content-Transfer-Encoding: binary");
              header("Content-Type: " . $this->_settings['mime']);
      
              $function = 'image' . substr($this->_settings['mime'], 6); 
              $function($this->_image);
          }   
      }
      

      【讨论】:

        【解决方案5】:

        您可能会看到您的服务器是否安装了 ImageMagick 或 gd,或任何数量的其他 PHP 图像库。

        另一个可能更好的选择是在 javascript 中进行图像处理。

        【讨论】:

        • 客户端操作永远不会比服务器端图像操作更好。
        • 他不是在处理图像,而是在获取图像的大小并根据显示目的设置他的 CSS。如果你读过这个问题,你就会明白。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-04
        • 1970-01-01
        • 2012-02-28
        • 1970-01-01
        相关资源
        最近更新 更多