<?
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>
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
360buy.rar (6个子文件)
data
3749.png 250B
233.png 214B
888.png 197B
106.png 219B
245.png 233B
goods
360buy.php 5KB
共 6 条
- 1
资源评论
- Younggod2013-08-03可用,但是不知道这么弄会不会影响速度
- qq8922352013-04-15好吧 不过是Php的不会用。先看看算法
- meichele2013-04-06可以用 谢谢无私共享
- Aiden2014-02-08可以用,修改了下又可以用于新蛋的,嘻嘻
outakukou
- 粉丝: 3
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功