Php implements batch download of images to local instances (anti-leech can be used)

Source: Internet
Author: User
Tags mkdir delete cache

For images without anti-Leech protection

The code is as follows: Copy code

/*
* For images without anti-Leech protection
* $ Url Image address
* $ Filename image storage address
* Return the path and name of the downloaded image, and the image size.
*/
Function GrabImage ($ url, $ filepath, $ filename = ""){
If ($ url = "") return false;
 
$ Ext = strrchr ($ url ,".");
If ($ filename = ""){
If ($ ext! = ". Gif" & $ ext! = ". Jpg" & $ ext! = ". Png") return false;
$ Filename = date ("YmdHis ");
}
 
Ob_start ();
Readfile ($ url );
$ Img = ob_get_contents ();
Ob_end_clean ();
$ Size = strlen ($ img); // Image size
 
! Is_dir (getcwd (). $ filepath )? Mkdir (getcwd (). $ filepath): null; // Generate a folder
 
$ Fp2 = fopen (getcwd (). $ filepath. $ filename. $ ext, "");
Fwrite ($ fp2, $ img );
Fclose ($ fp2 );
 
Return array ($ filepath. $ filename. $ ext, $ size );
}
 


Images with anti-Leech protection
 

The code is as follows: Copy code
/*
* Images with anti-Leech protection
* $ Url Image address
* $ Filename image storage address
* Return the path and name of the downloaded image, and the image size.
* $ Fromurl: source URL. You can enter the source image URL to crack anti-Leech protection.
*/
Function GrabImage2 ($ url, $ filepath, $ filename = "", $ fromurl ){
If ($ url = "") return false;
 
$ Ext = strrchr ($ url ,".");
If ($ filename = ""){
If ($ ext! = ". Gif" & $ ext! = ". Jpg" & $ ext! = ". Png") return false;
$ Filename = date ("YmdHis ");
 }
 
! Is_dir (getcwd (). $ filepath )? Mkdir (getcwd (). $ filepath): null; // Generate a folder
 
$ Re = $ this-> curlg ($ url, $ fromurl );
$ Size = file_put_contents (getcwd (). $ filepath. $ filename. $ ext, $ re); // return size
 
Return array ($ filepath. $ filename. $ ext, $ size );
}

Batch collection code:

The code is as follows: Copy code
/**
* Batch download of images
*/
Public function downimg (){
$ Keep = $ this-> _ get ("keep"); // 1 get from cache 2 delete cache/recache
$ Id = $ this-> _ get ("id ");
$ Id = $ id? $ Id: 0;
$ Nextid = $ id + 1;
If ($ keep! = 1 ){
$ Data = M ('cjlist')-> where ("status = 1")-> select ();
S ("keepcjdata", $ data, 3600 );
} Else {
$ Data = S ("keepcjdata ");
}
$ Endid = count ($ data );
// Echo $ id;
// Echo $ endid;
If ($ endid = $ id ){
Exit ('All images have been downloaded! ');
}
// Echo $ endid;
$ Data2 ['cover _ imgurl'] = $ this-> imgdown2 ($ data [$ id] ['cover _ imgurl']);
// Echo $ data [$ id] ['thum _ url'];
$ Data2 ['thum _ url'] = $ this-> imgdown2 ($ data [$ id] ['thum _ url'], 'thum _'); // medium-sized image type
$ Data2 ['id'] = $ data [$ id] ['id'];
If (! $ Data2 ['thum _ url'] |! $ Data2 ['cover _ imgurl']) {
$ Data2 ['status'] =-1;
} Else {
$ Data2 ['status'] = 2;
}
$ Res = M ('cjlist')-> save ($ data2 );
// $ This-> echosql ();
If ($ res ){
Echo 'download completion ID :'. $ data2 ['id']. "go to the next ID to download <script> window. location. href = '". U ('index/downimg '). "/keep/1/id /". $ nextid. "'</script> ";
} Else {
Echo 'failed to save. Refresh again. <Script> window. location. reload () </script> ';
}
// Dump ($ data2 );
// Dump ($ data );
 
}

You can also generate thumbnails.

The code is as follows: Copy code

/*
* Image Collection saves local generation of thumbnails
 *
**/
 
Public function imgdown ($ picurl = 'http: // response '){
$ Mulu = "/". date ("YmdH", time ())."/";
$ Reimg = $ this-> GrabImage2 ($ picurl, $ mulu, time (), $ fromurl );
// $ Img = file_get_contents ($ picurl );
// Echo file_put_contents('222.jpg ', $ img );
// Exit;
// $ Reimg = "/20140529/1401333398 .jpg ";
// Dump ($ reimg );
// Exit;
If ($ reimg [1] <= 0 ){
Exit ("failed to download image ");
 }
$ Reimg = $ reimg [0];
Import ('org. Util. Image ');
$ Image = new Image ();
$ Imgurl = getcwd (). $ reimg;
$ Ext = strrchr ($ imgurl ,".");
$ Thum_name = $ Image-> thumb ($ imgurl, "thum _". time (). $ ext, '', '000000', '000000 ');
Rename (getcwd (). "/". $ thum_name, getcwd (). $ mulu. $ thum_name); // move to the folder
$ Imginfo = $ Image-> getImageInfo (getcwd (). $ mulu. $ thum_name );
// Dump ($ imginfo );
$ Imgarr ['cover _ url'] = $ reimg;
$ Imgarr ['thum _ url'] = $ mulu. $ thum_name;
$ Imgarr ['thum _ W'] = $ imginfo ['width'];
$ Imgarr ['thum _ H'] = $ imginfo ['height'];
Return $ imgarr;
 }

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.