Code from my blog: http://www.taoniwu.com/archives/3234.html all the great god if there is any good advice, thank you for proposing ~~~~~
- ? class collection{
- protected $url;//Capture Address
- protected $prefix;//rename file prefix
- protected $style;//image format to be collected, passing 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 ';
- Exit
- Break
- Case 2:
- Echo ';
- Exit
- Break
- Case 3:
- Echo ';
- Exit
- Break
- Case 4:
- Echo ';
- 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 Handling
- 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 Save
- 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 ';
- Exit
- }
- $FW = fwrite ($fo, $obj);
- Echo '. $filename. ' Acquisition success ';
- }
- }
- }
- }
- Address acquisition functions, including image suffix names
- 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::p rel, $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::p rel, $code, $arrimg);
- return $arrimg;
- }
- }
- Test 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;//Capture Address
- protected $prefix;//rename file prefix
- protected $style;//image format to be collected, passing 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 ';
- Exit
- Break
- Case 2:
- Echo ';
- Exit
- Break
- Case 3:
- Echo ';
- Exit
- Break
- Case 4:
- Echo ';
- 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 Handling
- 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 Save
- 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 ';
- Exit
- }
- $FW = fwrite ($fo, $obj);
- Echo '. $filename. ' Acquisition success ';
- }
- }
- }
- }
- Address acquisition functions, including image suffix names
- 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::p rel, $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::p rel, $code, $arrimg);
- return $arrimg;
- }
- }
- Test 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 |