欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php 處理png圖片白色背景色改為透明色的實(shí)例代碼

 更新時(shí)間:2018年12月10日 09:44:04   投稿:mrr  
這篇文章主要介紹了php 處理png圖片白色背景色改為透明色的實(shí)例代碼,文中通過(guò)實(shí)例代碼給大家介紹了用PHP的GD庫(kù)把圖片的背景替換成透明背景,需要的朋友參考下

先看下面一段代碼,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)建一副白色透明的畫(huà)布
 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); //填充某個(gè)點(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庫(kù)把圖片的背景替換成透明背景

之前寫(xiě)個(gè)功能用PHP把圖片的背景弄成透明,之留下文字(黑色的),我也在百度上找,也試過(guò)別人的代碼。大多數(shù)代碼的思路都是這樣:

生成新的畫(huà)布,讀取源圖片每個(gè)坐標(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 開(kāi)始
{
  $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));        
      }
    }
  }
}

但是用了這個(gè)思路,圖片的背景一直都不能便透明,改了好多次。
后來(lái)發(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í)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論