2012-11-06 91 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