The code comes from my blog:
http://www.taoniwu.com/archives/3234.html If you have any good suggestions, thank you~~~~~
- ?class Collection{
- protected $url; //Collection address
- protected $prefix; //Rename file prefix
- protected $style; //Pass in an array for the image format to be collected
- const prel = '/(?:http?|https?)://(?:[^./()?]+).(?:[^./]+).(?:com|cn| net|org)/(?:[^.:"'()?]+).(jpg|png|gif)/i'; //Collection rules
- //Constructor function
- function __construct($url,$prefix ,$style){
- switch($this->checkdata($url,$prefix,$style)){
- case 1:
- echo '<script>alert("The collection address cannot be empty!")< /script>';</li>
<li> exit;</li>
<li> break;</li>
<li> case 2:</li>
<li> echo '<script>alert("The image format to be collected should be an array!")</script>';
- exit;
- break;
- case 3:
- echo '<script>alert("The image format that needs to be collected cannot be empty!")</script>';
- exit;
- break;
- case 4:
- echo '<script> alert("The file name cannot contain . / | or start with a space!")</script>';
- exit;
- }
- $this->url = $url;
- $this->prefix = $prefix ;
- $this->style = $style;
- }
- //Start collecting data
- public function action(){
- $url = $this->checkurl();
- $imgurl = $this->collecturl ($url);
- $this->savafile($imgurl);
- }
- //url processing
- 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<=$mymunber[1];$i++){
- $myurl[] = $temparr[0].$i.$temparr[1];
- }
- }else{
- $myurl = $this- >url;
- }
- return $myurl;
- }
- //File saving
- 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<200 || $h<200){
- continue ;
- }
- ob_start();
- readfile($value);
- $obj = ob_get_contents();
- ob_end_clean();
- $dir = 'F:/php/';
- 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("Failed to create file, The file directory cannot be written! ")</script>';
- exit;
- }
- $fw = fwrite($fo,$obj);
- echo '
'. $filename.'Collection successful ';
- }
- }
- }
- }
-
- //Address collection function, including image suffix name
- 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($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;
- }
- }
- //Check data
- 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;
- }
- }
- }
-
-
-
-
-
-
-
- ?>
Copy code
- class Collection{
- protected $url; //Collection address
- protected $prefix; //Rename file prefix
- protected $style; //The image format to be collected, pass in an array
- const prel = '/ (?:http?|https?)://(?:[^./()?]+).(?:[^./]+).(?:com|cn|net|org)/( ?:[^.:"'()?]+).(jpg|png|gif)/i'; //Collection rules
- //Constructor function
- function __construct($url,$prefix,$style){
- switch($this->checkdata($url,$prefix,$style)){
- case 1:
- echo '<script>alert("The collection address cannot be empty!")</script>';
- exit;
- break;
- case 2:
- echo '<script>alert("The image format to be collected should be an array!")</script>';
- exit;
- break;
- case 3:
- echo '<script>alert("The image format that needs to be collected cannot be empty!")</script>';
- exit;
- break;
- case 4:
- echo '<script>alert("The file name cannot be Contains . / | or starts with a space! ")</script>';
- exit;
- }
- $this->url = $url;
- $this->prefix = $prefix;
- $this-> ;style = $style;
- }
- //Start collecting data
- public function action(){
- $url = $this->checkurl();
- $imgurl = $this->collecturl($url);
- $this->savafile($imgurl);
- }
- //url processing
- 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<= $mymunber[1];$i++){
- $myurl[] = $temparr[0].$i.$temparr[1];
- }
- }else{
- $myurl = $this->url;
- }
- return $myurl;
- }
- //File saving
- 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<200 || $h<200){
- continue;
- }
- ob_start( );
- readfile($value);
- $obj = ob_get_contents();
- ob_end_clean();
- $dir = 'F:/php/';
- 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("Failed to create the file, the file directory is not writable! ")</script>';
- exit;
- }
- $fw = fwrite($fo,$obj);
- echo '
'. $filename.'Collection successful ';
- }
- }
- }
- }
-
- //Address collection function, including image suffix name
- 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($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;
- }
- }
- //Check data
- 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;
- }
- }
- }
Copy code
|