程式碼來自本人部落格:
http://www.taoniwu.com/archives/3234.html各位大神如果有什麼好的建議,感謝提出~~~~~
- ?class Collection{
- protected $url; //擷取位址
- protected $prefix; //重新命名檔案前綴
- protected $style; //需要收集的圖片格式,傳入一個陣列
- const prel = '/(?:http?|https?)://(?:[^./()?]+).( ?:[^./]+).(?:com|cn|net|org)/(?:[^.:"'()?]+).(jpg|png|gif)/i'; / /擷取規則
- //建構子
- function __construct($url,$prefix,$style){
- switch($this->checkdata($url,$prefix,$style)){
- case 1:
- echo '<script>alert("採集位址不能為空!")</script>';
- exit;
- break;
- case 2:
- echo ' <script>alert("需要收集的圖片格式,應該是數組!")</script>';
- exit;
- break;
- case 3:
- echo '<script>alert( "需要收集的圖片格式,不能為空!")</script>';
- exit;
- break;
- case 4:
- echo '<script>alert("檔案名稱不能含有. / |或用空格開頭! 🎜> $this->style = $style;<li> }<li> //開始收集資料<li> public function action(){<li> $url = $this->checkurl();<li> $ imgurl = $this->collecturl($url);<li> $this->savafile($imgurl);<li> }<li> //url處理<li> protected function checkurl(){<li> $munprel = '/([0-9]+,[0-9]+)/i';<li> $myurl;<li> if(preg_match($munprel,$this->url,$arr)){<li> $temp = substr($arr[0],1,strlen($arr[0])-2);<li> $mymunber = explode(',',$temp);<li> $temparr = explode ($arr[0],$this->url);<li> for($i=$mymunber[0];$i<=$mymunber[1];$i++){<li> $myurl[] = $temparr[0].$i.$temparr[1];<li> }<li> }else{<li> $myurl = $this->url;<li> }<li> return $myurl;<li> }<li> //檔案儲存<li> protected function savafile($imgurl){<li> if(!empty($imgurl)){<li> foreach($imgurl[0] as $key=>$value ){<li> $filename = '';<li> if(in_array($imgurl[1][$key],$this->style)){<li> $size = @getimagesize($value);<li> if($size === false){<li> continue;<li> }<li> list($w,$h,$t,$a) = $size;<li> if($w< 200 || $h<200){<li> continue;<li> } <li> ob_start();<li> readfile($value);<li> $obj = ob_get_contents();<li> ); ;<li> $dir = 'F:/php/';<li> if(!is_dir($dir)){<li> mkdir($dir,0777);<li> }<li> if(!empty ($this->prefix)){<li> $filename = $dir.$this->prefix.date('Ymd').rand(10000,99999).'.'.$imgurl[1][$key ];<li> }else{<li> $filename = $dir.date('Ymd').rand(10000,99999).'.'.$imgurl[1][$key];<li> }<li> $fo = @fopen($filename,'wb');<li> if($fo === false){<li> echo '<script>alert("建立檔案失敗,檔案目錄不可寫! ")</script>';
- exit;
- }
- $fw = fwrite($fo,$obj);
- echo '
'.$filename.'擷取成功 ';
- }
- }
- }
- }
-
- //位址擷取函數,包含圖片後綴名
- protected function collecturl($url){
- set_time_limit(0);
- if(is_array($url)){
- $arr = array();
- $imgkey = array();
- foreach($url as $value){
- $code = file_get_contents($value);
- preg_match_all(self::prel,$code,$arrimg);
- $arr = array_merge($array_merge($array_merge($array_merge($array_merge($array_merge))); arr,$arrimg[0]);
- $imgkey = array_merge($imgkey,$arrimg[1]);
- }
- return array($arr,$imgkey);
- }else{
- $code = file_get_contents($url);
- preg_match_all(self::prel,$code,$arrimg);
- return $arrimg;
- }
- }
- return $arrimg;
- }
- }
- // //檢定資料
- private function checkdata($url,$prefix,$style){
- if(empty($url)){
- return 1;
- }elseif(!is_array($style)){
- return 2;
- }elseif(count($style)==0){
- return 3;
- }elseif(stripos($prefix,'.') !== false || stripos ($prefix,'/') !== false || stripos($prefix,'|') !== false){
- return 4;
- }
- }
- }
- }
-
-
-
-
-
-
-
- ?>
-
複製程式碼
- class Collection{
- protected $url; //擷取位址
- protected $prefix; //重新命名檔案前綴
- protected $style; //需要擷取圖片格式,傳入一個陣列
- const prel = '/(?:http?|https?)://(?:[^./()?]+).(?:[^./] +).(?:com|cn|net|org)/(?:[^.:"'()?]+).(jpg|png|gif)/i'; //擷取規則
- / /建構子
- function __construct($url,$prefix,$style){
- switch($this->checkdata($url,$prefix,$style)){
- case 1:
- echo '<script>alert("採集位址不能為空!")</script>';
- exit;
- break;
- case 2:
- echo '<script>alert("需要採集的圖片格式,應該是陣列!不能為空! ! ")</script>';
- exit;
- }
- $this->url = $url;
- $this->prefix = $prefix;
- $this->style = $style;
- }
- //開始收集資料
- public function action(){
- $url = $this->checkurl();
- $imgurl = $this->collecturl ($url);
- $this->savafile($imgurl);
- }
- //url處理
- protected function checkurl(){
- $munprel = '/([0- 9]+,[0-9]+)/i';
- $myurl;
- if(preg_match($munprel,$this->url,$arr)){
- $temp = substr( $arr[0],1,strlen($arr[0])-2);
- $mymunber = explode(',',$temp);
- $temparr = explode($arr[0], $this->url);
- for($i=$mymunber[0];$i $myurl[] = $temparr[0].$ i.$temparr[1];
- }
- }else{
- $myurl = $this->url;
- }
- return $myurl;
- }
- //檔案儲存
- protected function savafile($imgurl){
- if(!empty($imgurl)){
- foreach($imgurl[0]as $key=>$value){
- $filename = '';
- if(in_array($imgurl[1][$key],$this->style)){
- $size = @getimagesize($value);
- if($size = == false){
- continue;
- }
- list($w,$h,$t,$a) = $size;
- if($w continue;
- }
- ob_start();
- readfile($value);
- $obj = ob_get_contents();
- ob_end_clean(); if(!is_dir($dir)){
- mkdir($dir,0777);
- }
- if(!empty($this->prefix) ){
- $filename = $dir.$this->prefix.date('Ymd').rand(10000,99999).'.'.$imgurl[1][$key];
- }else {
- $filename = $dir.date('Ymd').rand(10000,99999).'.'.$imgurl[1][$key];
- }
- $fo = @fopen ($filename,'wb');
- if($fo === false){
- echo '<script>alert("建立檔案失敗,檔案目錄不可寫! ")</script>';
- exit;
- }
- $fw = fwrite($fo,$obj);
- echo '
'.$filename.'擷取成功 ';
- }
- }
- }
- }
-
- //位址擷取函數,包含圖片後綴名
- protected function collecturl($url){
- set_time_limit(0);
- if(is_array($url)){
- $arr = array();
- $imgkey = array();
- foreach($url as $value){
- $code = file_get_contents($value);
- preg_match_all(self::prel,$code,$arrimg);
- $arr = array_merge($array_merge($array_merge($array_merge($array_merge($array_merge))); arr,$arrimg[0]);
- $imgkey = array_merge($imgkey,$arrimg[1]);
- }
- return array($arr,$imgkey);
- }else{
- $code = file_get_contents($url);
- preg_match_all(self::prel,$code,$arrimg);
- return $arrimg;
- }
- }
- return $arrimg;
- }
- }
- // //檢定資料
- private function checkdata($url,$prefix,$style){
- if(empty($url)){
- return 1;
- }elseif(!is_array($style)){
- return 2;
- }elseif(count($style)==0){
- return 3;
- }elseif(stripos($prefix,'.') !== false || stripos ($prefix,'/') !== false || stripos($prefix,'|') !== false){
- return 4;
- }
- }
- }
- }
-
-
- 複製程式碼
-
-
|