这段代码已经给我带来了几个星期的问题,但还没有接近解决它......
function create_thumbnail($source, $destination, $thumb_width){
$extension = get_image_extension($source);
$size = getimagesize($source);
$imageWidth = $newWidth = $size[0];
$imageHeight = $newheight = $size[1];
if ($imageWidth > $thumb_width || $imageHeight > $thumb_width)
{
// Calculate the ratio
$xscale = ($imageWidth/$thumb_width);
$yscale = ($imageHeight/$thumb_width);
$newWidth = ($yscale > $xscale) ? round($imageWidth * (1/$yscale)) : round($imageWidth * (1/$xscale));
$newHeight = ($yscale > $xscale) ? round($imageHeight * (1/$yscale)) : round($imageHeight * (1/$xscale));
}
$newImage = imagecreatetruecolor($newWidth, $newHeight);
switch ($extension)
{
case 'jpeg':
case 'jpg':
$imageCreateFrom = 'imagecreatefromjpeg';
$store = 'imagejpeg';
break;
case 'png':
$imageCreateFrom = 'imagecreatefrompng';
$store = 'imagepng';
break;
case 'gif':
$imageCreateFrom = 'imagecreatefromgif';
$store = 'imagegif';
break;
default:
return false;
}
$container = $imageCreateFrom($source);
imagecopyresampled($newImage, $container, 0, 0, 0, 0, $newWidth, $newHeight, $imageWidth, $imageHeight);
return $store($newImage, $destination);
}
现在它工作正常,但是这些调整大小的图像要进入的空间是 200(高度)乘 225(宽度),有时我会得到一个又高又瘦的图像和一些又短又宽的图像,这使得它很难将它们放入空间中,理想情况下,如果我可以调整这些图像的大小并将它们压扁或切断,但我不确定这是否可能......是吗?
如果这是不可能的,我想我正在寻找调整此代码,所以如果它的图像又高又瘦,可以调整为 200 的高度,如果图像又宽又短,可以调整为 225 的宽度......我希望这个说得通。
无论如何,任何提示、建议或任何东西都将不胜感激。
我试图运行 mschr 代码(在答案下面)并得到了这些错误
Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/content/44/8713044/html/admin/Home.php on line 321
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/content/44/8713044/html/admin/Home.php on line 323
Warning: Cannot modify header information - headers already sent by (output started at /home/content/44/8713044/html/admin/include/header.php:7) in /home/content/44/8713044/html/admin/Home.php on line 325
Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/content/44/8713044/html/admin/Home.php on line 329
Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/content/44/8713044/html/admin/Home.php on line 334