Can PNG image transparency be preserved when using PHP's GDlib imagecopyresampled?

問題:

The following PHP code snippet uses GD to resize a browser-uploaded PNG to 128x128. 以下PHP代碼段使用GD將瀏覽器上傳的PNG調整爲128x128。 It works great, except that the transparent areas in the original image are being replaced with a solid color- black in my case. 效果很好,只是在我的情況下,原始圖像中的透明區域已被替換爲純黑色。

Even though imagesavealpha is set, something isn't quite right. 即使設置了imagesavealpha ,也不是很正確。

What's the best way to preserve the transparency in the resampled image? 在重新採樣的圖像中保留透明度的最佳方法是什麼?

$uploadTempFile = $myField[ 'tmp_name' ]
list( $uploadWidth, $uploadHeight, $uploadType ) 
  = getimagesize( $uploadTempFile );

$srcImage = imagecreatefrompng( $uploadTempFile );    
imagesavealpha( $targetImage, true );

$targetImage = imagecreatetruecolor( 128, 128 );
imagecopyresampled( $targetImage, $srcImage, 
                    0, 0, 
                    0, 0, 
                    128, 128, 
                    $uploadWidth, $uploadHeight );

imagepng(  $targetImage, 'out.png', 9 );

解決方案:

參考: https://stackoom.com/en/question/8O3
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章