2012-11-06 93 views
7

我很困惑为什么使用GD库调整大小的PNG图像的尺寸比原始尺寸大得多。为什么调整大小的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位。

+1

使用了'5'的压缩因子。试试'9',看看会发生什么。 –

+0

我想知道如果你的新尺寸是什么导致压缩不那么有效。看到不同的目标尺寸会压缩到什么文件大小会很有趣。例如,如果目标尺寸是原始尺寸的一半,那么新的文件尺寸是多少? –

+0

@MarcB上面看到:PNG(9):503 KB –

回答

10

您不需要调用的大多数功能来减少图像,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

+0

好吧,现在测试。 –

+0

非常好,谢谢!原件:328KB→用您的代码调整大小:163KB。 ......我唯一不明白的是,为什么透明通道现在能够存活,而一些早期的代码变黑了。 –

+0

看到我的解释.....你现在正在使用'imagecreate' ...它的空白石板... – Baba