4,250
社区成员
发帖
与我相关
我的任务
分享
class competitor_360buy_ImageRule extends competitor_ImageRule
{
function __construct()
{
$this->set_color_filter(array(230,255,0,10,0,10),array(200,255,200,255,200,255));
}
function save($img,$path)
{
$path = $path.rand(0,100000000).'.gif';
imagegif($img,$path);
return $path;
}
function getBlock($img,$size)//$img是被分析的图片的标识符,$size是其大小
{
$stat = 0;
$start = 0;
$end = 0;
$block = 0;
$imgs = array();
$n = 0;
$has_word = 0;
$word_color = imagecolorallocate($img, 0, 0, 0); //为一幅图像分配颜色
$blank_color = imagecolorallocate($img, 255, 255, 255);
// var_dump($img);
// exit;
for($i = 0; $i < $size[0]; ++$i) {
$has_word = 0;
for($j =0; $j < $size[1]; ++$j){
$pixelrgb = imagecolorat($img,$i,$j); //取得某像素的颜色索引值
$cols = imagecolorsforindex($img, $pixelrgb); //取得某索引的颜色
$cf = $this->color_filter($cols); //匹配颜色值,白返1,黑返0,否则返2
switch($cf) {
case 1:
if($stat == 0) {//如果还没开始一个新的块,启动它
$stat = 1;
$start = $i;
}
$has_word++;
imagesetpixel($img,$i,$j,$word_color); //画一个单一像素
break;
case 0:
if($stat == 1 && $j == $size[1] - 1 && $has_word == 0) {
$stat = 0;
$end = $i;
$width = $end - $start;
$tmp_img =imagecreatetruecolor($width,$size[1]);
imagecopy($tmp_img,$img,0,0,$start,0,$width,$size[1]);
$imgs[$n]['image'] = $tmp_img;
$imgs[$n]['width'] = $width;
$imgs[$n]['height'] = $size[1];
++$n;
// imagegif($tmp_img,rand(0,100000).'.gif');
}
imagesetpixel($img,$i,$j,$blank_color);
break;
case 2:
imagesetpixel($img,$i,$j,$word_color);
break;
}
// if($cf == 'word')break;
}
}
parent::__construct();
$stat = 0;
foreach($imgs as &$img){
$tmp_img =imagecreatetruecolor($img['width'],11);
imagecopy($tmp_img,$img['image'],0,0,0,4,$img['width'],11);
$img['image'] = $tmp_img;
$img['height'] = 11;
}
return $imgs;
}
}
//用于拆分图片中的文字
abstract class competitor_imagerule
{
protected $_image_type = 'gif';
function __construct()
{
$this->set_color_filter(array(0,0,0,0,0,0),array(255,255,255,255,255,255));
}
abstract function getBlock($img,$size);
function get_type()
{
return $this->_image_type;
}
//文字拆分函数,返回图片资源数组;
function getImage($file)
{
$type = image_type_to_mime_type(exif_imagetype($file));
$a = array();
preg_match('/\/(.*)$/',$type,$a);
if($a[1]!='gif'){
return false;
}
return imagecreatefromgif($file);
}
function save()
{
return true;
}
//查看输入的色彩是否在范围内
function color_filter($cols,$d = 'h')
{
//print_r($this);
if(
$cols['red'] <= $this->max_r &&
$cols['red'] >= $this->min_r &&
$cols['green'] <= $this->max_g &&
$cols['green'] >= $this->min_g &&
$cols['blue'] <= $this->max_b &&
$cols['blue'] >= $this->min_b) {
return 1;
}elseif(
$cols['red'] <= $this->blank_max_r &&
$cols['red'] >= $this->blank_min_r &&
$cols['green'] <= $this->blank_max_g &&
$cols['green'] >= $this->blank_min_g &&
$cols['blue'] <= $this->blank_max_b &&
$cols['blue'] >= $this->blank_min_b) {
return 0;
}else{
if($d == 'h')
return 2;
else
return 0;
}
}
function set_color_filter($cols,$cols_blank)
{
$this->min_r = $cols[0];
$this->max_r = $cols[1];
$this->min_g = $cols[2];
$this->max_g = $cols[3];
$this->min_b = $cols[4];
$this->max_b = $cols[5];
$this->blank_min_r = $cols_blank[0];
$this->blank_max_r = $cols_blank[1];
$this->blank_min_g = $cols_blank[2];
$this->blank_max_g = $cols_blank[3];
$this->blank_min_b = $cols_blank[4];
$this->blank_max_b = $cols_blank[5];
}
function set_blank()
{
}
}