baker95935

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

片段一

//similar_text($numStr, $val, $pre);      //计算两个字符串的相似度
//print_r($pre);
 
$imgPath = 'time.jpg';
$size = getimagesize($imgPath);//得到图片的信息
$im = imagecreatefromjpeg($imgPath);//創建一張圖片
//$rgb = imagecolorat($im, 100, 100);//得到任意一點的颜色值
//$aa = imagecolorsforindex($im, $rgb);
//
print_r($size);
//410 =>0
//188 =>1
for ($i = 0; $i < $size[1]; ++ $i) {
    for ($j = 0; $j < $size[0]; ++$j) {
        $rgb = imagecolorat($im, $j, $i);          //取得某像素的颜色索引值
        $rgbarray[$i][$j] = imagecolorsforindex($im, $rgb);//取得某索引的颜色
 
        //foreach ($rgbarray as $key =>$value){
 
        }
    }
 
print_r($rgbarray);
 
echo "<img src=\"time.jpg\" />";

匹配图像中的验证码

如下

class gjPhone
{
 
    protected $imgPath; // 图片路径
    protected $imgSize; // 图片大小
    protected $hecData; // 分离后数组
    protected $horData; // 横向整理的数据
    protected $verData; // 纵向整理的数据
    function __construct ($path)
    {
        $this->imgPath = $path;
    }
 
    public function getHec ()
    {
        $size = getimagesize($this->imgPath);               //获取图片信息数组
        $res = imagecreatefrompng($this->imgPath);          //创建一个新图像
        for ($i = 0; $i < $size[1]; ++ $i) {
            for ($j = 0; $j < $size[0]; ++ $j) {
                $rgb = imagecolorat($res, $j, $i);          //取得某像素的颜色索引值
                $rgbarray = imagecolorsforindex($res, $rgb);//取得某索引的颜色
                if ($rgbarray['red'] < 125 || $rgbarray['green'] < 125 ||
                    $rgbarray['blue'] < 125) {
                    $data[$i][$j] = 1;
                } else {
                    $data[$i][$j] = 0;
                }
            }
        }
        $this->imgSize = $size;                             //图片大小
        $this->hecData = $data;                             //分离后数组
    }
 
    public function magHorData ()
    {
        $data = $this->hecData;
        $size = $this->imgSize;
        $z = 0;
        for ($i = 0; $i < $size[1]; ++ $i) {
            if (in_array('1', $data[$i])) {
                $z ++;
                for ($j = 0; $j < $size[0]; ++ $j) {
                    if ($data[$i][$j] == '1') {
                        $newdata[$z][$j] = 1;
                    } else {
                        $newdata[$z][$j] = 0;
                    }
                }
            }
        }
        return $this->horData = $newdata;
    }
 
    public function showPhone ($ndatas)
    {
        error_reporting(0);
        $phone = null;
        $d = 0;
        foreach ($ndatas as $key => $val) {
            if (in_array(1, $val)) {            //检查数组中是否有某个值
                foreach ($val as $k => $v) {
                    $ndArr[$d] .= $v;
                }
            }
            if (! in_array(1, $val)) {
                $d ++;
            }
        }
        foreach ($ndArr as $key01 => $val01) {
            $phone .= $this->initData($val01);
        }
        return $phone;
    }
 
    /**
     * 初始数据
     */
    public function initData ($numStr)
    {
        $result = null;
        $data = array(
            '1' => '00000000111000000000000001110000000001001000100000000010100011000000000011000110000000000110000100000000010110011000000',
            '5' => '00000000001000000000000000010000000000100100100000000000101001110000000000100000110000000011000000100000001101000010000',
            '10' => '00000011100011100000000011001100100100100010010001000110000100100010001100001001000100011000010010001001001001100010100'
        );
        foreach ($data as $key => $val) {
            similar_text($numStr, $val, $pre);      //计算两个字符串的相似度
            if ($pre > 95) { // 相似度95%以上
                $result = $key;
                break;
            }
        }
        return $result;
    }
}
 
$imgurl = 'jd.png';
list ($width, $heght, $type, $attr) = getimagesize($imgurl);
$new_w = 17;
$new_h = 11;
$thisimage = imagecreatetruecolor($new_w, $new_h); // $new_w, $new_h 为裁剪后的图片宽高
//新建一个真彩色图像 imagecreatetruecolor
$background = imagecolorallocate($thisimage, 255, 255, 255);
//为一幅图像分配颜色
imagefilledrectangle($thisimage, 0, 0, $new_w, $new_h, $background);
//画一矩形并填充
$oldimg = imagecreatefrompng($imgurl); // 载入原始图片
 
// 首先定位要取图的位置(这里可以通过前端js或者其他手段定位,由于我这是测试,所以就ps定位并写死了)
$weizhi = array(
    '1' => 165,
    '5' => 308,
    '10' => 456
);
 
foreach ($weizhi as $wwzz) {
    $src_y = 108;
    imagecopy($thisimage, $oldimg, 0, 0, $wwzz, $src_y, $new_w, $new_h); // $src_y,$new_w为原图中裁剪区域的左上角坐标拷贝图像的一部分将src_im图像中坐标从src_x,src_y开始,宽度为src_w,高度为src_h的一部分拷贝到dst_im图像中坐标为dst_x和dst_y的位置上。
    $tem_png = 'tem_1.png';
    imagepng($thisimage, __DIR__ . '/' . $tem_png); // 通过定位从原图中copy出想要识别的位置并生成新的缓存图,用以后面的图像识别类使用。
 
    $gjPhone = new gjPhone($tem_png); // 实例化类
    $gjPhone->getHec(); // 进行图像像素分离
    $horData = $gjPhone->magHorData(); // 将分离出是数据转成01表示的图像、这里可以根据自己喜好定
    $phone = $gjPhone->showPhone($horData); // 将转换好的01表示的数据与库中的数据进行匹配,匹配度95以上就算成功,库这里由于是做测试就直接写了数组
    echo '| ' . $phone . ' | ';
}

 

posted on 2019-05-21 13:36  baker95935  阅读(693)  评论(0编辑  收藏  举报