【问题标题】:imagecopyresampled Resize to max-width OR height, but keep ratioimagecopyresampled 调整为最大宽度或高度,但保持比例
【发布时间】:2012-01-12 00:53:01
【问题描述】:

我有以下代码用于将服务器上的一些图像调整为更小的尺寸:

// get the ratio of the original image
list($orgwidth, $orgheight) = getimagesize($originalImageDir);
// create a thumb and source image to be resized
$thumb = imagecreatetruecolor($width, $height);
$source = imagecreatefromjpeg($originalImageDir);
// resize
imagecopyresampled($thumb, $source, 0, 0, 0, 0, round($height / $ratio), $height, $orgwidth, $thumbheight);
// save new thumb with quality 75
imagejpeg($thumb, $path, 75);

我遇到了以下问题:我将函数的高度和宽度分别设置为 300 和 200。现在,原始图像不是那个比例,所以我希望能够将原始图像的最大宽度调整为 200 或最大高度 300;取较大值。

例如,如果我有一个 1200h x 1000w 的图像,我想将其调整为 240px 和 200px 的高度,因为在这种情况下 200px 是我允许的最大宽度。如果我有一个 480h x 300w 的图像,我的新图像将被调整为 300px 高度和 187px 宽度,因为 300px 是我的最大高度,这使我低于允许的最大宽度。

希望我说得通。无论如何,如果任何 php 和数学向导有什么东西,请分享:)

谢谢!

【问题讨论】:

  • 这个必须有数百个重复。
  • 计算原始图像的纵横比,然后检查您需要的最终图像的宽度/高度是否在该纵横比之下。如果不是,您可能需要更改最终图像的宽度和高度。但是,如果您不想更改最终图像的宽度/高度,它可能会失真或模糊。

标签: php image-manipulation


【解决方案1】:

为什么不看看像 http://pear.php.net/package/Image_Transform/ 这样的东西,它具有适用于不同图像库的各种驱动程序,并且具有您正在寻找的功能?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-22
    • 2015-01-31
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 2013-04-07
    相关资源
    最近更新 更多