先看下面一段代碼,php 處理png圖片白色背景色改為透明色
function pngMerge($o_pic,$out_pic){
$begin_r = 255;
$begin_g = 250;
$begin_b = 250;
list($src_w, $src_h) = getimagesize($o_pic);// 獲取原圖像信息 寬高
$src_im = imagecreatefrompng($o_pic); //讀取png圖片
print_r($src_im);
imagesavealpha($src_im,true);//這里很重要 意思是不要丟了$src_im圖像的透明色
$src_white = imagecolorallocatealpha($src_im, 255, 255, 255,127); // 創(chuàng)建一副白色透明的畫布
for ($x = 0; $x $src_w; $x++) {
for ($y = 0; $y $src_h; $y++) {
$rgb = imagecolorat($src_im, $x, $y);
$r = ($rgb >> 16) 0xFF;
$g = ($rgb >> 8) 0xFF;
$b = $rgb 0xFF;
if($r==255 $g==255 $b == 255){
imagefill($src_im,$x, $y, $src_white); //填充某個點(diǎn)的顏色
imagecolortransparent($src_im, $src_white); //將原圖顏色替換為透明色
}
if (!($r = $begin_r $g = $begin_g $b = $begin_b)) {
imagefill($src_im, $x, $y, $src_white);//替換成白色
imagecolortransparent($src_im, $src_white); //將原圖顏色替換為透明色
}
}
}
$target_im = imagecreatetruecolor($src_w, $src_h);//新圖
imagealphablending($target_im,false);//這里很重要,意思是不合并顏色,直接用$target_im圖像顏色替換,包括透明色;
imagesavealpha($target_im,true);//這里很重要,意思是不要丟了$target_im圖像的透明色;
$tag_white = imagecolorallocatealpha($target_im, 255, 255, 255,127);//把生成新圖的白色改為透明色 存為tag_white
imagefill($target_im, 0, 0, $tag_white);//在目標(biāo)新圖填充空白色
imagecolortransparent($target_im, $tag_white);//替換成透明色
imagecopymerge($target_im, $src_im, 0, 0, 0, 0, $src_w, $src_h, 100);//合并原圖和新生成的透明圖
imagepng($target_im,$out_pic);
return $out_pic;
}
$o_pic = '1.png';
$name = pngMerge($o_pic,'aaaa.png');
print_r($name);
補(bǔ)充:用PHP的GD庫把圖片的背景替換成透明背景
之前寫個功能用PHP把圖片的背景弄成透明,之留下文字(黑色的),我也在百度上找,也試過別人的代碼。大多數(shù)代碼的思路都是這樣:
生成新的畫布,讀取源圖片每個坐標(biāo)的顏色,不符合要求的用imagecolortransparent()函數(shù)將該顏色替換成透明的。
$o_pic = '1.jpg';
//要處理的色階起始值
$begin_r = 215;
$begin_g = 215;
$begin_b = 215;
list($src_w,$src_h,$src_type) = getimagesize($o_pic);// 獲取原圖像信息
$file_ext = get_ext($o_pic);//獲取擴(kuò)展名
$target_im = imagecreatetruecolor($src_w,$src_h);//新圖
if($file_ext == 'jpg') //轉(zhuǎn)換JPG 開始
{
$src_im = ImageCreateFromJPEG($o_pic);
imagecopymerge($target_im,$src_im,0,0,0,0,$src_w,$src_h,100);
for($x = 0; $x $src_w; $x++)
{
for($y = 0; $y $src_h; $y++)
{
$rgb = imagecolorat($src_im, $x, $y);
$r = ($rgb >> 16) 0xFF;
$g = ($rgb >> 8) 0xFF;
$b = $rgb 0xFF;
if($r > $begin_r $g > $begin_g $b > $begin_b ){
imagecolortransparent($target_im, imagecolorallocate($target_im,$r, $g, $b));
}
}
}
}
但是用了這個思路,圖片的背景一直都不能便透明,改了好多次。
后來發(fā)現(xiàn)只有最后一次imagecolortransparent()有效果,前面都都被覆蓋了。
把思路改了下,把不要的顏色先統(tǒng)一轉(zhuǎn)換成白色,最后再將白色替換成透明
$begin_r = 98;
$begin_g = 98;
$begin_b = 98;
list($src_w, $src_h) = getimagesize($o_pic);// 獲取原圖像信息
$src_im = imagecreatefromjpeg($o_pic);
//imagecopymerge($target_im, $src_im, 0, 0, 0, 0, $src_w, $src_h, 100);
//imagecopyresampled($target_im, $src_im, 0, 0, 0, 0, $src_w, $src_h, $src_w, $src_h);
$i = 0;
$src_white = imagecolorallocate($src_im, 255, 255, 255);
for ($x = 0; $x $src_w; $x++) {
for ($y = 0; $y $src_h; $y++) {
$rgb = imagecolorat($src_im, $x, $y);
$r = ($rgb >> 16) 0xFF;
$g = ($rgb >> 8) 0xFF;
$b = $rgb 0xFF;
if($r==255 $g==255 $b == 255){
$i ++;
continue;
}
if (!($r = $begin_r $g = $begin_g $b = $begin_b)) {
imagefill($src_im, $x, $y, $src_white);//替換成白色
}
}
}
$target_im = imagecreatetruecolor($src_w, $src_h);//新圖
$tag_white = imagecolorallocate($target_im, 255, 255, 255);
imagefill($target_im, 0, 0, $tag_white);
imagecolortransparent($target_im, $tag_white);
imagecopymerge($target_im, $src_im, 0, 0, 0, 0, $src_w, $src_h, 100);
總結(jié)
以上所述是小編給大家介紹的php 處理png圖片白色背景色改為透明色的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
您可能感興趣的文章:- PHP簡單實(shí)現(xiàn)圖片格式轉(zhuǎn)換(jpg轉(zhuǎn)png,gif轉(zhuǎn)png等)
- PHP中使用Imagick讀取pdf并生成png縮略圖實(shí)例
- PHP使用imagick讀取PDF生成png縮略圖的兩種方法
- PHP輸出圖像imagegif、imagejpeg與imagepng函數(shù)用法分析
- php縮放gif和png圖透明背景變成黑色的解決方法
- PHP實(shí)現(xiàn)生成透明背景的PNG縮略圖函數(shù)分享
- PHP基于GD庫的縮略圖生成代碼(支持jpg,gif,png格式)
- PHP實(shí)現(xiàn)對png圖像進(jìn)行縮放的方法(支持透明背景)
- 支持png透明圖片的php生成縮略圖類分享
- PHP添加PNG圖片背景透明水印操作類定義與用法示例
- php 實(shí)現(xiàn)svg轉(zhuǎn)化png格式的方法分析