Yesterday I sent a simple image collection code. today I improved it and made it into an image collection class. although it is not perfect yet, I can barely use it because the time is not locked, so it can only be improved and improved later. now, we will release it first. if anyone needs it, we can use it,... "> <LINKhref =" http://www.php100.com//statics/style/headflo
Yesterday I sent a simple image collection code. today I improved it and made it into an image collection class. although it is not perfect yet, I can barely use it because the time is not locked, so we can only wait for further improvement and improvement. now we will release it first. if anyone needs it, we can use it. However, this program class occupies a large amount of memory and has no time to optimize it, please refer to the code below:
Class Collection {protected $ url; // Collection address php100.comprotected $ prefix; // rename the file prefix protected $ style; // format of the image to be collected, input an array const prel = '/(?: Http? | Https ?) :\/\/(?: [^ \. \/\ (\) \?] + )\.(?: [^ \. \/] + )\.(?: Com | cn | net | org )\/(?: [^ \.: \ "\ '\ (\) \?] + )\. (Jpg | png | gif)/I '; // collection rules // Constructor function _ construct ($ url, $ prefix, $ style) {switch ($ this-> checkdata ($ url, $ prefix, $ style) {case 1: echo 'script alert ("collection address cannot be blank! ") 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 format of the image to be collected. it cannot be blank! ") 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 to collect 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 ); $ tempa Rr = 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;} // Save the 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 ('ymmd '). rand (Random, 99999 ). '. '. $ imgurl [1] [$ key];} else {$ filename = $ dir. date ('ymmd '). rand (Random, 99999 ). '. '. $ imgurl [1] [$ key] ;}$ fo =@ fopen ($ filename, 'WB '); if ($ fo === false) {echo 'script alert ("file creation failed, file directory cannot be written! ") Script '; exit;} $ fw = fwrite ($ fo, $ obj); echo ''. $ filename. 'collection successful ';}}}// Address Collection function, including the image suffix 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);} els E {$ 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 ;}}}