header('Content-Type; text/json; charset=utf-8');
$Single=$_GET['Single'];
$more=$_GET['more'];
$TargetUrl=$_POST['TargetUrl'];
$Save=$_POST['Save'];
//判断是抓取单个图片还是多个图片
if ($Single=='Single') {
$ImageCatch=new ImageCatch($TargetUrl,$Save);
$ImageCatch->S();
}else if ($more=='more') {
$ImageCatch=new ImageCatch($TargetUrl,$Save);
$ImageCatch->M();
}
//图片抓取类
class ImageCatch {
private $TargetUrl; //目标地址
private $Save; //保存地址
private $FileName; //文件名称及路径
private $Type; //文件类型
private $Size; //文件大小
//构造函数
public function __construct($TargetUrl,$Save) {
$this->TargetUrl=str_replace("'",'',$TargetUrl); //去掉单引号
$this->Save=$Save;
}
//CSS样式表中图片抓取方法
public function CSS() {
$content=@file_get_contents($this->TargetUrl);
//CSS图片过滤
preg_match_all('//i',$content,$css);
$css[1]=array_unique($css[1]);//移除重复的值
$match2=array();
if (count($css[1])>0) {
foreach($css[1] as $val) {
if (!preg_match('/^(https?://)/i',$val)) {
$val=$this->TargetUrl.'/'.$val;
$csscontent=@file_get_contents($val);
}else {
$csscontent=@file_get_contents($val);
}
//匹配图片URL地址
preg_match_all('/url((.*))/i',$csscontent,$cssimg);
$cssimg[1]=array_unique($cssimg[1]);//移除重复的值
}
foreach($cssimg[1] as $val) {
//去除 " ) 字符
$val=preg_replace(array('/"|)/'),'',$val);
//去除../字符
$val=str_replace('../','',$val);
//检查是否是http://开头,如果不是则加上要抓取的网址
if (!preg_match('/^(https?://)/i',$val)) {
array_push($match2,$this->TargetUrl.'/'.$val);
}else {
array_push($match2,$val);
}
}
return $match2;
}
}
//计算并返回图片数量及地址
public function M() {
$content=@file_get_contents($this->TargetUrl);
//网页图片过滤
$str='//i';
preg_match_all($str,$content,$res);
if ($res[1]) {
$res[1]=array_unique($res[1]);//移除重复的值
$httpstr='/^(https?://)/i';
$match=array();
foreach($res[1] as $val) {
if (!preg_match($httpstr,$val)) {
array_push($match,$this->TargetUrl.'/'.$val);
}else {
array_push($match,$val);
}
}
$cssimg=$this->CSS();
//扫描出css文件图片的总数与网页图片相加得到总数
$total=array("total"=>count($match)+count($cssimg));
$result=array_merge($total,$match,$cssimg);
//返回JSON数据
echo json_encode($result);
}else {
$res=array('no');
echo json_encode($res);
}
exit;
}
//抓取并保存图片
public function S() {
$this->Type=substr(strrchr($this->TargetUrl,'.'),1);
$this->FileName=$this->Save.'/'.substr(strrchr($this->TargetUrl,'/'),1);
$this->imageType();
$content=@file_get_contents($this->TargetUrl);
$this->imageDir();
if (!@file_put_contents($this->FileName,$content,FILE_USE_INCLUDE_PATH)) {
@unlink($this->FileName);
exit('{"status":"没有找到 '.$this->TargetUrl.' 图片"}');
}else {
$this->imageSize();
exit('{"status":"ok","FileSave":"'.$this->FileName.'","FileSize":"'.$this->Size.'"}');
}
}
//新建目录
private function imageDir() {
if (!@file_exists($this->Save)) {
if (!@mkdir($this->Save,0700)) {
exit('{"status":"新建保存目录失败"}');
}
}
}
//文件类型判断
private function imageType() {
$typeArr=array('jpg','png','gif','zip','rar');
if (!in_array($this->Type,$typeArr)) {
exit('{"status":"要执行抓取的文件扩展名有错误,'.$this->TargetUrl.'"}');
}
}
//文件大小检测
private function imageSize() {
if (file_exists($this->FileName)) {
$this->Size=filesize($this->FileName);
if ($this->Size>1024*1024*1024) {
$this->Size=round($this->Size/1024/1024/1024,2).' GB';
}else if ($this->Size>1024*1024) {
$this->Size=round($this->Size/1024/1024,2).' MB';
}else if ($this->Size>1024) {
$this->Size=$this->Size/1024;
$this->Size=ceil($this->Size).'KB';
}else {
$this->Size=$this->Size.'bit';
}
}else {
return '未找到文件';
}
}
}
?>
|