<?php 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('/<link.+href="?(.*?.css)"?.+>/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='/<img.+src="?(.+.(jpg|gif|bmp|bnp|png))"?.+>/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 '未找到檔案'; } } } ?> |