php processing png picture white background color changed to transparent color example code

  • 2021-11-13 01:02:00
  • OfStack

First look at the following 1 code, php processing png picture white background color changed to transparent color


function pngMerge($o_pic,$out_pic){
 $begin_r = 255;
 $begin_g = 250;
 $begin_b = 250;
 list($src_w, $src_h) = getimagesize($o_pic);//  Obtain the original image information   Width and height 
 $src_im = imagecreatefrompng($o_pic); // Read png Picture 
 print_r($src_im);
 imagesavealpha($src_im,true);// It's important here   It means don't lose it $src_im Transparent color of image 
 $src_white = imagecolorallocatealpha($src_im, 255, 255, 255,127); //  Create 1 A white transparent canvas 
 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); // Fill the color of a point 
    imagecolortransparent($src_im, $src_white); // Replace the original color with transparent color 
    }
    if (!($r <= $begin_r && $g <= $begin_g && $b <= $begin_b)) {
     imagefill($src_im, $x, $y, $src_white);// Replace it with white 
     imagecolortransparent($src_im, $src_white); // Replace the original color with transparent color 
    }
  }
 }
 $target_im = imagecreatetruecolor($src_w, $src_h);// New graph 
 imagealphablending($target_im,false);// It's important here , It means not to merge colors , Direct use $target_im Image color replacement , Includes transparent colors ;
 imagesavealpha($target_im,true);// It's important here , It means don't lose it $target_im Transparent color of image ;
 $tag_white = imagecolorallocatealpha($target_im, 255, 255, 255,127);// Change the white color of the generated new image to transparent color   Save as tag_white
 imagefill($target_im, 0, 0, $tag_white);// Fill the new target image with empty white 
 imagecolortransparent($target_im, $tag_white);// Replace with transparent color 
 imagecopymerge($target_im, $src_im, 0, 0, 0, 0, $src_w, $src_h, 100);// Merge the original image with the newly generated transparent image 
 imagepng($target_im,$out_pic);
 return $out_pic;
}
$o_pic = '1.png';
$name = pngMerge($o_pic,'aaaa.png');
print_r($name);

Add: Replace the background of the picture with transparent background with GD library of PHP

Before writing a function, use PHP to make the background of the picture transparent, leaving words (black). I also look for it on Baidu and try other people's codes. The idea of most codes is this:

Generate a new canvas, read the color of each coordinate of the source picture, and use imagecolortransparent () function to replace the color with transparent one if it does not meet the requirements.


$o_pic = '1.jpg';
// The starting value of the color scale to be processed 
$begin_r = 215;
$begin_g = 215;
$begin_b = 215;
list($src_w,$src_h,$src_type) = getimagesize($o_pic);//  Obtain the original image information 
$file_ext = get_ext($o_pic);// Get Extensions 
$target_im = imagecreatetruecolor($src_w,$src_h);// New graph 
if($file_ext == 'jpg') // Conversion JPG  Begin 
{
  $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));        
      }
    }
  }
}

But with this idea, the background of the picture can't be transparent straight, and it has been changed many times.
Later, it was found that only the last time imagecolortransparent () worked, and the front was covered.

I changed my thinking, and changed the unwanted color into white first, and finally replaced white with transparent


$begin_r = 98;
$begin_g = 98;
$begin_b = 98;
list($src_w, $src_h) = getimagesize($o_pic);//  Obtain the original image information 
$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);// Replace it with white 
   }
  }
}
$target_im = imagecreatetruecolor($src_w, $src_h);// New graph 
$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);

Summarize


Related articles: