先看下面一段代碼,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); // 創建一副白色透明的畫布 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); //填充某個點的顏色 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);//在目標新圖填充空白色 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);
補充:用PHP的GD庫把圖片的背景替換成透明背景
之前寫個功能用PHP把圖片的背景弄成透明,之留下文字(黑色的),我也在百度上找,也試過別人的代碼。大多數代碼的思路都是這樣:
生成新的畫布,讀取源圖片每個坐標的顏色,不符合要求的用imagecolortransparent()函數將該顏色替換成透明的。
$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);//獲取擴展名 $target_im = imagecreatetruecolor($src_w,$src_h);//新圖 if($file_ext == 'jpg') //轉換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)); } } } }
但是用了這個思路,圖片的背景一直都不能便透明,改了好多次。
后來發現只有最后一次imagecolortransparent()有效果,前面都都被覆蓋了。
把思路改了下,把不要的顏色先統一轉換成白色,最后再將白色替換成透明
$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);
總結
以上所述是小編給大家介紹的php 處理png圖片白色背景色改為透明色的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!