php分析gif图片的问题

PHP编程者 2010-12-04 06:26:46
我有个小程序,以前可以分析png图片,但现在用同样的算法来分析gif图片,则不能分析,出问题处的代码如下:

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;
}
}

color_filter所在的类如下,执行完上面的getBlock()函数后打印$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()
{

}

}
...全文
118 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
life169 2010-12-06
  • 打赏
  • 举报
回复
那个价格图片是bmp位图。不是gif格式。
PHP编程者 2010-12-04
  • 打赏
  • 举报
回复
我就是想把这个网址里面的新蛋价格解析出来,http://www.newegg.com.cn/Product/98-c25-007.htm

4,250

社区成员

发帖
与我相关
我的任务
社区描述
国内外优秀PHP框架讨论学习
社区管理员
  • Framework
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧