imagecopy重采样调整到最大宽度或最大高度,但保持比例


imagecopyresampled Resize to max-width OR height, but keep ratio

我有以下代码,用于将服务器上的一些图像调整为较小的大小:

// 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和数学向导有什么问题,请分享:)

谢谢!

为什么不看看http://pear.php.net/package/Image_Transform/它为不同的图像库提供了各种驱动程序,并且具有您想要的功能?