Php: an image collection program for image collection

Source: Internet
Author: User
& Nbsp; & lt ;? PhpclassCollection {protected $ url; collection address protected $ prefix; rename the file prefix protected $ style; format of the image to be collected, input an array constprel (? : Http? | Https ?) :(? : [^. ()?] +) Class Collection {
Protected $ url; // Collection Address
Protected $ prefix; // rename the file prefix
Protected $ style; // The 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 format of the image to be collected, which 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 data collection
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;
}
// Save the file
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 ('ymd'). 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 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 );

12 Next page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.