PHP 验证码图片转二值化图片

转载别人 最近在研究

class VerifyIdentify
{

    private $imagePath,$imageType,$pixelRGB;

    //图片句柄
    private $imageHandle , $imageWidth , $imageHeight;

    //灰度化图片地址
    private $grayImagePath , $grayImageWidth , $grayImageHeight;

    //灰度化图片句柄
    private $grayImageHandle;

    //灰度化像素 & RGB
    private $grayPixel = [] , $grayRGB = [];

    public function __construct($imagePath = '' , $imageType = 'jpg' , $RGB = 200)
    {
        $this->imagePath = $imagePath;
        $this->imageType = $imageType;
        $this->pixelRGB  = 50;

        //为图片生成唯一Hash
        $this->imageHash = md5(filesize($this->imagePath).$imagePath.$imageType);
    }

    public function run($printBinary = false)
    {
        //二值化图片
        $this->binaryImage();

        //是否打印二值化信息
        if($printBinary)
        {
            $binary = '';
            foreach ($this->grayPixel as $v) $binary .= implode('',$v)."\n";
            echo $binary; die();
        }
    }

    //图片灰度化处理
    private function grayImage()
    {
        //得到图片句柄
        $this->imageHandle = $this->imageElement();

        //得到图片宽高
        $this->imageWidth = imagesx($this->imageHandle);
        $this->imageHeight= imagesy($this->imageHandle);

        //循环像素点
        for($h = 0 ; $h < $this->imageHeight ; $h++)
        {
            $this->grayRGB[$h] = [];
            for($w = 0 ; $w < $this->imageWidth ; $w++)
            {
                //获取当前像素点RGB信息
                $imageRGB = imagecolorat($this->imageHandle,$w,$h);
                $R = ($imageRGB >> 16) & 0xFF;
                $G = ($imageRGB >> 8) & 0xFF;
                $B = $imageRGB & 0xFF;

                //灰度化处理
                $imageGray  = round(($R+$G+$B)/3);
                $imageColor = imagecolorallocate($this->imageHandle,$imageGray,$imageGray,$imageGray);
                imagesetpixel($this->imageHandle,$w,$h,$imageColor);
                $this->grayRGB[$h][] = $imageGray;
            }
        }

        //灰度化图片保存地址
        $this->grayImagePath = str_replace(basename($this->imagePath),'',$this->imagePath)."gray_{$this->imageHash}.{$this->imageType}";

        //保存图片
        $this->imageElement('save',$this->grayImagePath);
    }

    //图片二值化
    private function binaryImage($prototype = true)
    {

        if(!$prototype)
            //图片灰度处理
            $this->grayImage();

        $dealImagePath = $prototype ? $this->imagePath:$this->grayImagePath;

        //得到灰度化图片句柄
        $this->grayImageHandle = $this->imageElement('handle',$dealImagePath);

        //得到灰度化图标元素
        $this->grayImageWidth  = imagesx($this->grayImageHandle);
        $this->grayImageHeight = imagesy($this->grayImageHandle);

        // 遍历所有像素点
        for ($h = 0; $h < $this->grayImageHeight; $h++) {
            $binaryDots[$h] = [];
            for ($w = 0; $w < $this->grayImageWidth; $w++) {
                $pixelRGB = imagecolorsforindex($this->grayImageHandle,imagecolorat($this->grayImageHandle,$w,$h));

                // 对颜色值过滤 进行二值化
                $this->grayPixel[$h][] = (int) ($pixelRGB['red'] < $this->pixelRGB || $pixelRGB['green'] < $this->pixelRGB || $pixelRGB['blue'] < $this->pixelRGB);
            }
        }
    }

    //获取图片元素
    private function imageElement($type = 'handle' , $param = '')
    {
        switch ($type)
        {
            case 'handle':

                switch ($this->imageType)
                {
                    case 'jpg':
                    case 'jpeg':
                        $res = imagecreatefromjpeg($param?:$this->imagePath);
                        break;
                    default:
                        $res = imagecreatefrompng($param?:$this->imagePath);
                }

                break;

            case 'save':

                switch ($this->imageType)
                {
                    case 'jpg':
                    case 'jpeg':
                        $res = imagejpeg($this->imageHandle,$param);
                        break;
                    default:
                        $res = imagepng($this->imageHandle,$param);
                        break;
                }

                break;

            default:
                $res = '';

        }
        return $res;
    }
}


(new VerifyIdentify(realpath('1.jpg'),'jpg'))->run(true);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值