<?
set_time_limit(0);
error_reporting(0);
/**
* 京东价格识别.
* @author by hzj for 08/20/12
* http://blog.sina.com.cn/outakukou
*/
class gjPriceBy360{
protected $imgPath;//图片路径
protected $imgSize;//图片大小
protected $hecData;//分离后数组
protected $horData;//横向整理的数据
protected $verData;//纵向整理的数据
function __construct($path){
$this->imgPath = $path;
}
/**
* 颜色分离转换...
*
* @param unknown_type $path
* @return unknown
*/
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;
}
/**
* 颜色分离后的数据横向整理...
*
* @return $this->horData
*/
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;
}
/**
* 整理纵向数据...
*
* @return $this->verData
*/
public function magVerData($newdata){;
for ($i=0;$i<$this->imgSize[0];++$i){
for($j=1;$j<$this->imgSize[1]+1;++$j){
$ndata[$i][$j] = $newdata[$j][$i];
}
}
$sum = count($ndata);
$c = 0;
$ndatas = null;
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 $Price
*/
public function showPrice($ndatas){
$Price = null;
$d = 0;
$ndArr = null;
foreach ($ndatas as $key => $val){
if(in_array(1,$val)){
foreach ($val as $k => $v){
$ndArr[$d].=$v;
}
}else{
$d++;
}
}
//var_dump($ndArr);//匹配图像数据时开启查看
foreach ($ndArr as $key01 =>$val01){
$Price .= $this->initData($val01);
}
return $Price;
}
/**
* 初始数据...
*
* @param unknown_type $numStr
* @return $result
*/
public function initData($numStr){
$result = null;
//京东图片数据
$data = array(
'¥'=>'100000000110000000111100100111111111001111111100011111111100101110000000100000000',
0=>'001111110011111111010000001010000001011111111001111110',
1=>'001000001011111111011111111000000001',
2=>'001000011011000111010001101010011001011110001001100001',
3=>'001000010011000011010010001010010001011111111001101110',
4=>'000001100000010100000100100001111111011111111000000100',
5=>'000000010011110011011110001010010001010011111010001110',
6=>'000111110001111111011010001010010001010011111000001110',
7=>'010000000010000011010001111010111100011110000011000000',
8=>'001101110011111111010010001010010001011111111001101110',
9=>'001110000011111001010001001010001011011111110001111100',
'.'=>'000000011000000011'
);
foreach ($data as $key => $val){
similar_text($numStr,$val,$pre);
if($pre>95){//相似度95%以上
$result = $key;
break;
}
}
return $result;
}
}
function saveImage($path){
if ( !preg_match('/gp(\d*)-1-1-(\d).(\w*)/i', $path, $matches) ) die('Use image please');
$image_name = strToLower($matches[1].'.'.$matches[3]);
$ch = curl_init ($path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$img = curl_exec($ch);
curl_close ($ch);
$local_path = 'goods/'.$image_name;
$fp = fopen($local_path,'w');
fwrite($fp, $img);
fclose($fp);
$size = getimagesize($local_path);
if(!$size){
die('价格图片获取失败');
}
return $local_path;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="" method="post">
<input type="text" name="url" />
<input type="submit" />
<?
$url=$_POST['url'];
if($url){
preg_match("/(\d*).html/i", $url, $matches);
$goodsid = $matches[1];
$imgPath = "http://jprice.360buyimg.com/price/gp".$goodsid."-1-1-1.png";
$imgPath = saveImage($imgPath);
$gjPrice = new gjPriceBy360($imgPath);
$gjPrice->getHec();
$horData = $gjPrice->magHorData();
$verData = $gjPrice->magVerData($horData);
$Price = $gjPrice->showPrice($verData);
echo "<br/>";
echo "===============京东价==============";
echo "<br/>";
echo $Price;
}
?>
</body>
</html>