【问题标题】:Why is resized PNG image so much bigger than original image?为什么调整大小的PNG图像比原始图像大得多?
【发布时间】:2012-10-26 18:46:13
【问题描述】:

我很困惑为什么使用 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) {
    // 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 保存为 mysql 数据库中的 blob。

我试图阻止 Alpha 通道并仅设置白色背景,文件大小没有显着变化。我尝试设置“压缩”参数(0 到 9),但仍然比原来的大。

示例
我把这个image (1058px*1296px) 调整为 900px * 1102px。结果如下:

原始文件:328 KB
PNG (0):3,79 MB
PNG (5):564 KB
PNG (9):503 KB

感谢任何提示如何使调整后的图像的文件大小更小。

--

PS:我认为这可能是位深度,但如您所见,上面的示例图像有 32 位,而调整大小的图像是 24 位。

【问题讨论】:

  • 您使用5 作为压缩系数。试试9 看看会发生什么。
  • 我想知道您的新尺寸是否是导致压缩效果不佳的原因。看看什么不同的目标尺寸会压缩到什么文件大小会很有趣。例如,如果目标尺寸是原始大小的二分之一,那么新的文件大小是多少。
  • @MarcB 见上图:PNG (9): 503 KB
  • 缩小图像很可能已经减小了任何“相同颜色”区域的大小,或者引入了不可见的条带,从而减少了可能的压缩量。如果您只是打开原件并使用 imagepng 重新保存它而不做任何事情会发生什么?
  • 这里有一些关于 PNG 的参考资料:optipng.sourceforge.net/pngtech/optipng.htmllibpng.org/pub/png/book/chapter09.html,这里是对 PNG 技术细节的描述以及如何利用它们 smashingmagazine.com/2009/07/15/…

标签: php image resize png


【解决方案1】:

您调用的大多数函数都不是为了缩小图像,imagefillimagealphablending 等可能会导致文件大小变大。

为了保持透明使用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

【讨论】:

  • 太好了,谢谢!原始:328KB → 使用您的代码调整大小:163KB。 ...我唯一不明白的是,为什么透明通道现在还存在,有一些早期的代码它变黑了。
  • 看我的解释......你现在正在使用imagecreate......它是一张白纸......
  • 当我遇到有关 PNG 质量的问题(GD 使用索引的 8 位调色板)时,它会扭曲文本并且颜色会丢失,我现在使用来自 here 的提示和以下代码: $newImageTmp = imagecreatetruecolor($newwidth,$newheight); imagecopyresampled($newImageTmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); $newImage = imagecreate($newwidth,$newheight); imagecopy($newImage,$newImageTmp,0,0,0,0,$newwidth,$newheight);调色板是根据重采样结果确定的,画质更好(虽然颜色会丢失)!
  • Image with createimage directly: i50.tinypic.com/6egcjc.jpg # 调整后的图像(先使用真彩色,然后复制):i45.tinypic.com/qn18gg.png
  • 这是一个不同的问题.. 你能把它改成一个新问题
猜你喜欢
  • 2012-12-10
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多