目前我想創建一個質量最低的透明png文件。使用PHP創建一個透明PNG文件
代碼:
<?php
function createImg ($src, $dst, $width, $height, $quality) {
$newImage = imagecreatetruecolor($width,$height);
$source = imagecreatefrompng($src); //imagecreatefrompng() returns an image identifier representing the image obtained from the given filename.
imagecopyresampled($newImage,$source,0,0,0,0,$width,$height,$width,$height);
imagepng($newImage,$dst,$quality); //imagepng() creates a PNG file from the given image.
return $dst;
}
createImg ('test.png','test.png','1920','1080','1');
?>
但是,也存在一些問題:
我需要特定的PNG文件創建任何新的文件之前?或者我可以創建沒有任何現有的PNG文件?
警告:imagecreatefrompng(test.png):未能打開流:在
C無這樣的文件或目錄:\ DSPadmin \ DEV \ ajax_optipng1.5 \ create.php第4行
雖然有錯誤信息,但它仍然生成一個PNG文件,但是,我發現該文件是黑色圖像,我需要指定任何參數使其透明嗎?
謝謝。
感謝您的幫助!你介意教我如何最小化PNG文件的大小?imagepng函數中設置'9'質量級別是我能做的唯一事情嗎?謝謝 – user782104
'imagepng'默認的「質量」設置(應該命名爲壓縮,因爲'png的壓縮是無損的)是9(afaik,我測試沒有設置質量(234'Bytes'),質量爲0百KB')和設置9(234字節))。所以我想這是GD能做的最好的。 –
這使我的黑線消失 –