【发布时间】:2012-02-12 23:38:30
【问题描述】:
我已使用 GD 库成功调整图像大小。我将任何图像的大小调整为 350 x 250,问题是某些图片在调整大小时看起来不太好(拉伸),因为我将它们调整为固定大小。我有一个 350 x 250 的空间,需要调整图片大小,只要不拉伸,我不介意图片大小是否小于 350 x 250。我该如何解决这个问题?
$save = "$directory/" . $file_name; //This is the new file you saving
$file = "$directory/" . $file_name; //This is the original file
list($width, $height) = getimagesize($file) ;
$modwidth = 350;
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
$diff = $width / $modwidth;
$modheight = 250;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $save, 100) ;
【问题讨论】:
-
您似乎没有使用计算出的变量(不同,x,y)。您需要根据您的计算传递 imagecopy 计算的比例大小和偏移量,以避免拉伸
-
知道了……完全错过了。现在一切都说得通了