2012-01-12 152 views
1

我有下面的代码,我用我的服务器上的一些图像调整到一个更小的尺寸:imagecopyresampled调整至最大宽度或高度,但保持比

// 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和数学奇才有东西煮熟,请分享:)

谢谢!

+0

必须有这个数百重复。 – Jon 2012-01-12 00:59:15

+2

[在PHP中调整图片大小的智能方式]的可能重复(http://stackoverflow.com/questions/1906899/smart-way-of-resizing-i-php-) – Jon 2012-01-12 00:59:23

+0

计算原始图像的宽高比,然后检查你需要的最终图像的宽度/高度是否在该宽高比下。如果不是,您可能需要更改最终图像的宽度和高度。但是,如果您不想更改最终图像的宽度/高度,则可能会扭曲或模糊。 – insomiac 2012-01-12 01:01:24

回答

相关问题