Php 为什么调整大小的PNG图像比原始图像大这么多?
我很困惑为什么使用GD库调整大小的PNG图像比原始图像大得多 这是我用来调整图像大小的代码:Php 为什么调整大小的PNG图像比原始图像大这么多?,php,image,resize,png,Php,Image,Resize,Png,我很困惑为什么使用GD库调整大小的PNG图像比原始图像大得多 这是我用来调整图像大小的代码: // create image from posted file $src = imagecreatefrompng($file['tmp_name']); // get original size of uploaded image list($width,$height) = getimagesize($file['tmp_name']); if($width>$maxImgWidth) {
// create image from posted file
$src = imagecreatefrompng($file['tmp_name']);
// get original size of uploaded image
list($width,$height) = getimagesize($file['tmp_name']);
if($width>$maxImgWidth) {
// resize the image to maxImgWidth, maintain the original aspect ratio
$newwidth = $maxImgWidth;
$newheight=($height/$width)*$newwidth;
$newImage=imagecreatetruecolor($newwidth,$newheight);
// fill transparent with white
/*$white=imagecolorallocate($newImage, 255, 255, 255);
imagefill($newImage, 0, 0, $white);*/
// the following is to keep PNG's alpha channels
// turn off transparency blending temporarily
imagealphablending($newImage, false);
// Fill the image with transparent color
$color = imagecolorallocatealpha($newImage,255,255,255,127);
imagefill($newImage, 0, 0, $color);
// restore transparency blending
imagesavealpha($newImage, true);
// do the image resizing by copying from the original into $newImage image
imagecopyresampled($newImage,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// write image to buffer and save in variable
ob_start(); // Stdout --> buffer
imagepng($newImage,NULL,5); // last parameter is compression 0-none 9-best (slow), see also http://www.php.net/manual/en/function.imagepng.php
$newImageToSave = ob_get_contents(); // store stdout in $newImageToSave
ob_end_clean(); // clear buffer
// remove images from php buffer
imagedestroy($src);
imagedestroy($newImage);
$resizedFlag = true;
}
然后我将$newImageToSave作为blob保存在mysql数据库中
我试图阻止alpha通道,只设置了白色背景,文件大小没有明显变化。我尝试设置“压缩”参数(0到9),但仍然比原始参数大
示例我拿了这个(1058px*1296px),并将它的大小调整为900px*1102px。结果如下: 原始文件:328KB
巴布亚新几内亚(0):3,79MB
PNG(5):564KB
巴布亚新几内亚(9):503KB
任何关于如何使大小调整后的图像文件变小的提示都值得欣赏 --
PS:我认为它可能是位深度,但正如您所看到的,上面的示例图像有32位,而调整大小的图像是24位。您不知道您调用的大多数函数都可以减少图像,
imagefill
,imagealphablending
等会导致更大的文件大小
要保持透明,请使用imagecreate
而不是imagecreatetruecolor
,只需简单调整大小即可
$file['tmp_name'] = "wiki.png";
$maxImgWidth = 900;
// create image from posted file
$src = imagecreatefrompng($file['tmp_name']);
// get original size of uploaded image
list($width, $height) = getimagesize($file['tmp_name']);
if ($width > $maxImgWidth) {
$newwidth = $maxImgWidth;
$newheight = ($height / $width) * $newwidth;
$newImage = imagecreate($newwidth, $newheight);
imagecopyresampled($newImage, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($newImage, "wiki2.png", 5);
imagedestroy($src);
imagedestroy($newImage);
$resizedFlag = true;
}
最终大小:164KB您正在使用
5
作为压缩因子。试试9
看看会发生什么。我想知道你的新尺寸是否是导致压缩效果不佳的原因。看看哪些不同的目标维度会压缩到什么文件大小会很有趣。例如,如果目标尺寸是原始尺寸的一半,那么新文件的大小是多少。@MarcB如上所述:PNG(9):503 kb缩小图像很可能会缩小任何“相同颜色”区域的大小,或者引入不可见的条带,从而减少可能的压缩量。如果你不做任何事情就打开原始文件并用imagepng重新保存,会发生什么?这里有一些关于PNG的参考资料:这里是PNG技术细节的描述,以及如何利用这些技术非常好,谢谢!原件:328KB→ 已使用您的代码调整大小:163KB。。。我唯一不明白的是,为什么透明通道现在还存在,在一些早期代码中它变成了黑色。请看我的解释。。。。。您正在使用imagecreate
现在。。。这是一张白纸……当我遇到有关PNG质量的问题时(GD使用索引的8位调色板),它会扭曲文本并丢失颜色,我现在使用以下代码的提示:$newImageTmp=ImageCreateTureColor($newwidth,$newheight);imagecopyresampled($newImageTmp,$src,0,0,0,$newwidth,$newheight,$width,$height)$newImage=imagecreate($newwidth,$newheight);imagecopy($newImage,$newImageTmp,0,0,0,$newwidth,$newheight)代码>调色板根据重采样结果确定,图像质量更好(尽管颜色丢失)!直接使用createimage的图像:#调整后的图像(先使用truecolor,然后复制):这是另一个问题。。你能把它变成一个新问题吗