分享一段工具型代码:印章抠图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29238009/article/details/80610038

能将白底红字的印章抠出来,用的是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;
}

猜你喜欢

转载自blog.csdn.net/qq_29238009/article/details/80610038
今日推荐