php縮小png圖片不損失透明色的解決方法
主要是利用gd庫(kù)的兩個(gè)方法:
imagecolorallocatealpha //分配顏色 + alpha
imagesavealpha //設(shè)置在保存 png 圖像時(shí)保存完整的 alpha 通道信息
代碼示例:
//獲取源圖gd圖像標(biāo)識(shí)符
$srcImg = imagecreatefrompng('./src.png');
$srcWidth = imagesx($srcImg);
$srcHeight = imagesy($srcImg);
//創(chuàng)建新圖
$newWidth = round($srcWidth / 2);
$newHeight = round($srcHeight / 2);
$newImg = imagecreatetruecolor($newWidth, $newHeight);
//分配顏色 + alpha,將顏色填充到新圖上
$alpha = imagecolorallocatealpha($newImg, 0, 0, 0, 127);
imagefill($newImg, 0, 0, $alpha);
//將源圖拷貝到新圖上,并設(shè)置在保存 PNG 圖像時(shí)保存完整的 alpha 通道信息
imagecopyresampled($newImg, $srcImg, 0, 0, 0, 0, $newWidth, $newHeight, $srcWidth, $srcHeight);
imagesavealpha($newImg, true);
imagepng($newImg, './dst.png');
相關(guān)文章
php ajax數(shù)據(jù)傳輸和響應(yīng)方法
今天小編就為大家分享一篇php ajax數(shù)據(jù)傳輸和響應(yīng)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08php生成二維碼不保存服務(wù)器還有下載功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了php生成二維碼不保存服務(wù)器還有下載功能的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08Thinkphp自定義美化success和error提示跳轉(zhuǎn)頁(yè)面代碼實(shí)例
這篇文章主要介紹了Thinkphp自定義美化success和error提示跳轉(zhuǎn)頁(yè)面代碼實(shí)例,有需要的同學(xué)可以直接借鑒文中代碼,可以增加頁(yè)面的美觀和友好程度2021-03-03