PHP的电话号码识别

<?php
/**
 * 电话号码识别.
 */

class gjPhone{
  //protected $imgPath;//图片路径    
  protected $imgSize;//图片大小
  protected $hecData;//分离后数组
  protected $horData;//横向整理的数据
  protected $verData;//纵向整理的数据
 
/**
 * 颜色分离转换...
 *
 * @param unknown_type $path
 * @return unknown
 */
  public function getHec($imgPath){   

		$res = @imagecreatefromjpeg($imgPath);
		if($res) $res = @imagecreatefromjpeg($imgPath);
        $size = @getimagesize($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)
                {
                   echo  $data[$i][$j]=1;
                }else{
                    echo $data[$i][$j]=0;
                }
				
            }echo "\r\n";
        }echo "\r\n";
		
        $this->imgSize = $size;
        $this->hecData = $data;
    }
	
//获取文件的扩展名

public function extend_file($imgPath){
  $extend = pathinfo($imgPath);
  $extend = strtolower($extend['extension']);
  return $extend;
}

/**
 * 颜色分离后的数据横向整理...
 *
 * @return unknown
 */
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'){                    
                 echo  $newdata[$z][$j] = 1;
                }else{
                 echo  $newdata[$z][$j] = 0;
                }
            }   echo "\r\n";        
       }    
        
    }echo "\r\n";
    return $this->horData = $newdata;
}
/**
 * 整理纵向数据...
 *
 * @return unknown
 */
public function magVerData($newdata){
	$size = $this->imgSize;

    for ($i=0;$i<$size[0];++$i){
        for($j=1;$j<$size[1];++$j){
            $ndata[$i][$j] = $newdata[$j][$i];
			$tmp999 .= $newdata[$j][$i];
			
        }
		
		
		if($tmp999<1){
			echo "\r\n";
			$tmp999 = '';
		}else{
			echo $tmp999;
			$tmp999 = '';
		}
		
    }
  
    $sum = count($ndata);
    $c = 0;
    for ($a=0;$a<$sum;$a++){
        $value = $ndata[$a];
        if(in_array(1,$value)){ 
		           
              $ndatas[$c] = $value;
              $c++;
        }elseif(is_array($ndatas)){
          $b = $c-1;
          if(in_array(1,$ndatas[$b])){
              $ndatas[$c] = $value;
              $c++;
          }
        }        
    }
    
    return $this->verData = $ndatas;
    
}
/**
 * 显示电话号码...
 *
 * @return unknown
 */
public function showPhone($ndatas){
    $phone = null;
    $d = 0;
    if(!is_array($ndatas)) return '';
    foreach ($ndatas as $key => $val){
        if(in_array(1,$val)){
            foreach ($val as $k => $v){
               $ndArr[$d].=$v;
            }//echo $ndArr[$d]."<br>";
        }        
        if(!in_array(1,$val)){
            $d++;
        }
    }
   foreach ($ndArr as $key01 =>$val01){
        $phone .= $this->initData($val01);
   }   
   return $phone;
}

    /**
     * 初始数据...
     *
     * @param unknown_type $numStr
     * @return unknown
     */
    public function initData($numStr){
        $result = null;
        $data = array(
        	'-'=>'0000100000001000000010000000100000001000',
            '0'=>'0111111010000001100000011000000101111110',
            '1'=>'0100000110000001111111110000000100000001',
            '2'=>'1000001110000101100010011001000101100001',
            '3'=>'1000000110010001100100011001000101101110',
            '4'=>'0000110000110100010001001111111100000100',
            '5'=>'11110001100100011001000110001110',
            '6'=>'0111111010001001100100011001000110001110',
            '7'=>'1000001110001100100100001010000011000000',
            '8'=>'0110111010010001100100011001100101101110',
            '9'=>'0111000110001001100010011001000101111110'
        );
           foreach ($data as $key => $val){
           similar_text($numStr,$val,$pre);
           if($pre>95){//相似度90%以上
               $result = $key;
               break;
           }
        }
        return $result;
    }
 //处理图片
 public function Climg($imgPath){
        $this->getHec($imgPath);
		$horData = $this->magHorData();
		
		$verData = $this->magVerData($horData);

		$phone = $this->showPhone($verData);
		return $phone;
 }
 //检查远程文件
 static public  function url_exists($url) {
      if(!empty($url)){ 
		$head=@get_headers($url);
        if(!empty($head)) {
           if(in_array('Content-Type: image/jpeg', $head)){
           		
           		return true;
           }else return false;
        }else{
			return false;
		}
       }
  	}	
}
//==============================
	function vcurl($url){//发送数据
		$tmpInfo = '';
		$cookiepath = getcwd().'./'.$cookiejar;
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
		curl_setopt($curl, CURLOPT_TIMEOUT, 100);
		curl_setopt($curl, CURLOPT_HEADER, 0);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$tmpInfo = curl_exec($curl);
		if (curl_errno($curl)) {
			echo '<pre><b>错误:</b><br />'.curl_error($curl);
		}
		curl_close($curl);
		return $tmpInfo;
	}


	function GrabImage($url){ 
		if($url == ""){return false;} 
		$filename="img.bmp";
		@unlink($filename);
		
		ob_start(); 
		readfile($url);
		ob_end_clean(); 
		$size = strlen($url); 
		$fp2 = fopen($filename , "a"); 
		fwrite($fp2, $url); 
		fclose($fp2); 
		return $filename; 
	} 

function bmp2gd($src, $dest = false) {
	if(!($src_f = fopen($src, "rb"))) return false;
	if(!($dest_f = fopen($dest, "wb"))) return false;
	$header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f, 14));
	$info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", fread($src_f, 40));
	extract($info);
	extract($header);
	if($type != 0x4D42) return false;
	$palette_size = $offset-54;
	$ncolor = $palette_size/4;
	$gd_header = "";
	$gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
	$gd_header .= pack("n2", $width, $height);
	$gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
	if($palette_size) $gd_header .= pack("n", $ncolor);
	$gd_header .= "\xFF\xFF\xFF\xFF";
	fwrite($dest_f, $gd_header);
	if($palette_size) {
		$palette = fread($src_f, $palette_size);
		$gd_palette = "";
		$j = 0;
		while($j < $palette_size) {
			$b = $palette{$j++};
			$g = $palette{$j++};
			$r = $palette{$j++};
			$a = $palette{$j++};
			$gd_palette .= "$r$g$b$a";
		}
		$gd_palette .= str_repeat("\x00\x00\x00\x00", 256-$ncolor);
		fwrite($dest_f, $gd_palette);
	}
	$scan_line_size = (($bits * $width) + 7) >> 3;
	$scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03) : 0;
	for($i = 0, $l = $height -1; $i < $height; $i++, $l--) {
		fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l));
		$scan_line = fread($src_f, $scan_line_size);
		if($bits == 24) {
			$gd_scan_line = "";
			$j = 0;
			while($j < $scan_line_size) {
				$b = $scan_line{$j++};
				$g = $scan_line{$j++};
				$r = $scan_line{$j++};
				$gd_scan_line .= "\x00$r$g$b";
			}
		} else if($bits == 8) {
			$gd_scan_line = $scan_line;
		} else if($bits == 4) {
			$gd_scan_line = "";
			$j = 0;
			while($j < $scan_line_size) {
				$byte = ord($scan_line{$j++});
				$p1 = chr($byte >> 4);
				$p2 = chr($byte & 0x0F);
				$gd_scan_line .= "$p1$p2";
			}
			$gd_scan_line = substr($gd_scan_line, 0, $width);
		} else if($bits == 1) {
			$gd_scan_line = "";
			$j = 0;
			while ($j < $scan_line_size) {
				$byte = ord($scan_line{$j++});
				$p1 = chr((int) (($byte & 0x80) != 0));
				$p2 = chr((int) (($byte & 0x40) != 0));
				$p3 = chr((int) (($byte & 0x20) != 0));
				$p4 = chr((int) (($byte & 0x10) != 0));
				$p5 = chr((int) (($byte & 0x08) != 0));
				$p6 = chr((int) (($byte & 0x04) != 0));
				$p7 = chr((int) (($byte & 0x02) != 0));
				$p8 = chr((int) (($byte & 0x01) != 0));
				$gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
			}
			$gd_scan_line = substr($gd_scan_line, 0, $width);
		}
		fwrite($dest_f, $gd_scan_line);
	}
	fclose($src_f);
	fclose($dest_f);
	return true;
}

function imagecreatefrombmp($filename) {
	$tmp_name = tempnam("tmp", "BMP");
	if(bmp2gd($filename, $tmp_name)) {
		$img = imagecreatefromgd($tmp_name);
		unlink($tmp_name);
		return $img;
	}
	return false;
}

//#######################################################################################################################################
$imgurl = 'http://www.gqsoso.com/Include/gq.EnStrToImg.asp?Original=80%2C83%2C87%2C73%2C93%2C215%2C207%2C255%2C462%2C57%2C72%2C52';
$imgurl = $_GET['url'];

echo $imgurl."\r\n";

$imgcontent =  vcurl($imgurl);
$imgpath = GrabImage($imgcontent);

/* BMP -> JPRG */
$bmp = imagecreatefrombmp($imgpath);
imagejpeg($bmp, 'img.jpg');


$size = getimagesize('img.jpg');
print_r($size);
echo "\r\n";


$gjPhone = new gjPhone;
$Label = $gjPhone->Climg('img.jpg');

@unlink('img.jpg');
@unlink('img.bmp');



//#######################################################################################################################################


//ob_clean();
echo "<hr>".$Label.'<br>';
?>







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值