【发布时间】: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