【问题标题】:How to properly resize large images in PHP如何在 PHP 中正确调整大图像的大小
【发布时间】:2011-03-26 07:21:19
【问题描述】:

对于我运营的网站,用户可以将他们绘制的图片上传到画廊。我们创建该图像的缩略图和小视图以显示给其他用户(单击小视图图像会显示完整大小的图像)。

考虑到这一点,我创建了一个非常简单的调整大小脚本。在大多数情况下,此脚本可以完美运行。但是,我遇到过一个奇怪的情况,其中脚本完全搞砸了。

当通过脚本运行文件http://img191.imageshack.us/img191/2268/935full.png (1641x3121) 时(创建一个最大宽度或高度为 150 和另一个为 400 的缩略图),我们得到一个完美的缩略图http://img267.imageshack.us/img267/5803/935thumb.png (78x150) 和一个小视图图像大小合适,但被切断和拉伸http://img28.imageshack.us/img28/4002/935show.png (211 x 400)。

考虑到这一点,我的问题是:这是 PHP 中的问题还是逻辑错误?我该如何解决?

感谢您的宝贵时间。我用来创建这些缩略图的代码如下。

<?php
/**
 * Creates a thumbnail for any type of pre-existing image. Always saves as PNG image
 *
 * @param string - The location of the pre-existing image.
 * @param string - The location to save the thumbnail, including filename and extension.
 * @param int    - The Maximum Width, Default of 150
 * @param int    - The Maximum Height, Default of 150
 * @return bool  - Success of saving the thumbnail.
 */
function imagecreatethumbnail($file,$output,$max_width = 150,$max_height = 150)
{
        $img = imagecreatefromstring(file_get_contents($file));
        list($width, $height, $type, $attr) = getimagesize($file);
        if($height > $max_height || $width > $max_width)
        {
                if($width > $height)
                {
                        $thumb_width = $max_width;
                        $thumb_height = ceil(($height * $thumb_width)/$width);
                }
                else
                {
                        $thumb_height = $max_height;
                        $thumb_width = ceil(($width * $thumb_height)/$height);
                }
        } else {
                $thumb_width = $width;
                $thumb_height = $height;
        }
        imagesavealpha($img,true);
        $thumb = imagecreatetruecolor($thumb_width,$thumb_height);
        imagesavealpha($thumb,true);
        imagealphablending($thumb,false);
        imagecopyresampled($thumb,$img,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
        $return = imagepng($thumb,$output);
        imagedestroy($img);
        imagedestroy($thumb);
        return $return;
}

【问题讨论】:

  • 脚本是否中断(由于 max_execution_time 或 memory_limit)?
  • 使用imagecreatethumbnail("http://img191.imageshack.us/img191/2268/935full.png", "m.png", 400, 400); 运行代码会生成正确的图像。 PHP 5.2.6-1+lenny8。 php的GD是5.2.6.dfsg.1-1+lenny8版本。
  • 那一定是一些奇怪的 PHP GD 错误。运行 PHP 5.2.14,它说 GD 版本是“捆绑的(2.0.34 兼容)”

标签: php image-processing image-manipulation gd


【解决方案1】:

试试这个库,如果发生同样的情况,告诉我:

http://phpthumb.gxdlabs.com/

【讨论】:

    【解决方案2】:

    您是否尝试过删除 ceil() 函数?无论如何,图像函数都会自动将浮点数转换为整数。

    编辑:

    看看这是否有效:

    if($width > $max_width && $height * $thumb_width / $width < $max_width)
    {
        $thumb_width = $max_width;
        $thumb_height = $height * $thumb_width / $width;
    }
    elseif($height > $max_height && $width * $thumb_height / $height < $max_height)
    {
        $thumb_height = $max_height;
        $thumb_width = $width * $thumb_height / $height;
    }
    else
    {
        $thumb_width = $width;
        $thumb_height = $height;
    }
    

    【讨论】:

    • 好吧,我无法制作 x.xxx 像素的图像。
    • 事实上,问题不在于返回的数字。手动计算它仍然计算图像的正确尺寸。只是没有正确调整图像大小。
    【解决方案3】:

    这似乎是 PHP 中的一个错误。

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      相关资源
      最近更新 更多