[英]PNG Transparency with PHP
嘿,當我從png創建縮略圖時,嘗試保持png的透明度時遇到麻煩,有人對此有任何經驗嗎? 任何幫助都會很棒,這是我目前正在做的事情:
$fileName= "../js/ajaxupload/tees/".$fileName;
list($width, $height) = getimagesize($fileName);
$newwidth = 257;
$newheight = 197;
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($thumb, true);
$source = imagecreatefrompng($fileName);
imagealphablending($source, true);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagesavealpha($thumb, true);
imagepng($thumb,$newFilename);
過去,我已經成功地做到了:
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
$source = imagecreatefrompng($fileName);
imagealphablending($source, true);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($thumb,$newFilename);
我發現輸出的圖像質量更好的使用imagecopyresampled()
比imagecopyresized()
忘了顏色透明度指數,它永遠不會在所有渲染產品中起作用。 而是使用Alpha圖層蒙版:
$image = imagecreatetruecolor($size, $size);
imagealphablending($image, false);
imagesavealpha($image, true);
$trans_layer_overlay = imagecolorallocatealpha($image, 220, 220, 220, 127);
imagefill($image, 0, 0, $trans_layer_overlay);
這些函數訪問底層的gdlib庫,這是一個很好的玩具,但並不能取得很好的效果。 如果您有選擇,請改用imagemagick 。 缺點是目前尚無良好的php綁定,因此您需要通過外殼訪問它,通常在共享主機上不允許這樣做。
請參閱dycey對“我如何調整大小...”的回答 。 本質上,在執行任何其他操作之前,您需要使整個背景透明。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.