php工具型代码之印章抠图

能将白底红字的印章抠出来,用的是php,框架是laravel,其他框架请自行调节。扣其他颜色也可以,把里面的那段rgb参数判断改改就行了,最后抠出来的效果就是白底变透明,然后只留下红色的章,放在其他页面上就能形成盖章的效果了。代码自己写的,可能有bug,但是做做测试还是ok的,用到工作上的话请自行测试和优化。(在我自己测试对比下,能做到和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);//拾取白色 imagefill($im , 0 , 0 , $white);//把画布染成白色 imagecolortransparent($im , $white ) ;//把图片中白色设置为透明色 imagepng($im , storage_path('test2.png'));//生成图片 return false; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/53c0ca1927eeb1eb5698c2b4f913a2a3.html