Seal matting of php tool code

  • 2021-10-24 19:15:43
  • OfStack

You can dig out the seal with scarlet letter on white background, using php, and the frame is laravel. Please adjust other frames by yourself. Other colors can also be deducted. Just judge and change the rgb parameters inside. The final result is that the white background becomes transparent, and then only the red chapter is left, which can be stamped on other pages. Code written by themselves, there may be bug, but do the test or ok, used to work, please test and optimize. (Under my own test and comparison, I can achieve the same matting effect as PS)


function getStamp(){
  $path = storage_path('2018052411173848180.png');
  $image = file_get_contents($path);


  $info = getimagesize($path);
  $im = imagecreatefromstring($image);
  $width = $info[0];
  $height = $info[1];
  for($i=0;$i<$height;$i+=1){
    for($j=0;$j<$width;$j+=1){
      $rgb = ImageColorAt($im, $j, $i);
      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;
      echo $r.'.'.$g.'.'.$b.'.='.$rgb.'<br>x='.$j.', y='.$i.'<br>';

      if(intval($r)>220 && $g >220 && $b>220){
        $hex = imagecolorallocate($im, 255, 255, 255);
        imagesetpixel($im,$j, $i, $hex);
      }
    }
  }
  $white = imagecolorallocate($im , 255 , 255 , 255);// Pick up white 
  imagefill($im , 0 , 0 , $white);// Dye the canvas white 
  imagecolortransparent($im , $white ) ;// Set the white color in the picture to transparent color 

  imagepng($im , storage_path('test2.png'));// Generate a picture 

  return false;
}

Related articles: