Php 制作缩略图,但在PNG方面有问题
我正在使用php创建缩略图,它对GIF和JPEG效果良好,但对PNG效果不佳。当我使用PNG运行此脚本时,缩略图不会保存。你能告诉我我做错了什么吗?提前谢谢Php 制作缩略图,但在PNG方面有问题,php,gd,Php,Gd,我正在使用php创建缩略图,它对GIF和JPEG效果良好,但对PNG效果不佳。当我使用PNG运行此脚本时,缩略图不会保存。你能告诉我我做错了什么吗?提前谢谢 public function make_thumbs($img_src) { //Desired thumbnail width $width = 100; //Thumbnail name $thumb = 'th_' . $img_src; //Ensure the image exist
public function make_thumbs($img_src)
{
//Desired thumbnail width
$width = 100;
//Thumbnail name
$thumb = 'th_' . $img_src;
//Ensure the image exists
if(file_exists($img_src)){
if (exif_imagetype($img_src) == IMAGETYPE_GIF)
{
//Create image stream
$image = imagecreatefromgif($img_src);
}
elseif(exif_imagetype($img_src) == IMAGETYPE_JPEG)
{
//Create image stream
$image = imagecreatefromjpeg($img_src);
}
elseif(exif_imagetype($img_src) == IMAGETYPE_PNG)
{
//Create image stream
$image = imagecreatefrompng($img_src);
}
//Gather and store the width and height
list($image_width, $image_height) = getimagesize($img_src);
//Calculate new height while maintaining aspect ratio
$height = (($width / $image_width) * $image_height);
//Resample/resize the image
$tmp_img = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp_img, $image, 0, 0, 0, 0, $width, $height, $image_width, $image_height);
//Attempt to save the new thumbnail
if(is_writeable(dirname($thumb)))
{
if (exif_imagetype($img_src) == IMAGETYPE_GIF)
{
imagegif($tmp_img, $thumb, 100);
}
elseif(exif_imagetype($img_src) == IMAGETYPE_JPEG)
{
imagejpeg($tmp_img, $thumb, 100);
}
elseif(exif_imagetype($img_src) == IMAGETYPE_PNG)
{
imagepng($tmp_img, $thumb, 100);
}
}
//clear memory
imagedestroy($tmp_img);
imagedestroy($image);
}
}
尝试抛出故意警告(例如打印未定义的var)和故意错误(例如除零),以确保屏幕上的错误报告已打开。还要检查日志中是否有任何错误。最后,在运行PNG部分时,您是否获得了有关
$image\u width
和$image\u height
的任何信息?请检查此信息并了解是否支持PNG。发回你的发现,我们可以从那里继续这是什么回来:数组(12){[“GD版本”]=>string(27)“捆绑(2.0.34兼容)”[“自由类型支持”]=>bool(真)[“自由类型链接”]=>string(13)”与自由类型“[“T1Lib支持”]=>bool(假)[“GIF读取支持”]=>bool(真)[“GIF创建支持”]=>bool(真)[“JPEG支持”]=>bool(true)[“PNG支持”]=>bool(true)[“WBMP支持”]=>bool(true)[“XPM支持”]=>bool(false)[“XBM支持”]=>bool(true)[“JIS映射日语字体支持”]=>bool(false)}解决了这个问题。我将PNG质量设置为100。它只能是介于0和9之间的值。我试图添加一个答案,但我的声誉不够高。@user1660638我一直在到处寻找解决这个问题的方法。谢谢分享