How to use php code to realize cutout of seal (code)

不言
Release: 2023-04-03 12:52:02
Original
3327 people have browsed it

The content of this article is about how to use php code to realize the cutout (code) of the seal. The content is very detailed. Friends in need can refer to it. I hope it can help you.

I can cut out the seal with red text on the white background. I use PHP and the framework is Laravel. Please adjust other frameworks by yourself. You can also cut out other colors. Just change the rgb parameter judgment inside. The final cutout effect is that the white background becomes transparent, and then only the red stamp is left, which can be placed on other pages to form a stamp effect. . The code is written by myself and may have bugs, but it is still ok for testing. Please test and optimize it yourself if you use it at work. (After my own test and comparison, I can achieve a cutout effect similar to that of 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.&#39;.&#39;.$g.&#39;.&#39;.$b.&#39;.=&#39;.$rgb.&#39;<br>x=&#39;.$j.&#39;, y=&#39;.$i.&#39;<br>&#39;;

      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);//拾取白色
  imagefill($im , 0 , 0 , $white);//把画布染成白色
  imagecolortransparent($im , $white ) ;//把图片中白色设置为透明色

  imagepng($im , storage_path(&#39;test2.png&#39;));//生成图片

  return false;
}
Copy after login

Related recommendations:

PHP code example to implement Alipay app payment and asynchronous notification

php socket programming commonly used functions and implementation of simple c/s interaction

The above is the detailed content of How to use php code to realize cutout of seal (code). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template