Php captures website images and saves local server instances-PHP source code

Source: Internet
Author: User
In this example, the file_get_contents () function is used to capture the image content and then saved to the local server using fopen, then, replace the Address url to implement a complete page collection function and save the address locally. Let's take a look at the example below. In this example, the file_get_contents () function is used to capture the image content and then saved to the local server using fopen, then, replace the Address url to implement a complete page collection function and save the address locally. Let's take a look at the example below.

Script ec (2); script

First, let's give an example. The Code is as follows:

The Code is as follows:

$ Text = file_get_contents ("http://www.111cn.net ");
// Obtain all the img tags and store them to the two-dimensional array match

Preg_match_all ('/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Gif | \. jpg]) [\ '| \ "]. *? [\/]?> /', $ Text, $ match );

// Print the match
$ Houzhui = "./tp/". time (). rand (). ".". png;
$ Yuanname = getImage ($ match [1] [2], $ houzhui, tp );

// How to download images

Function getImage ($ url, $ filename = '', $ type = 0 ){
If ($ url = '') {return false ;}
If ($ filename = ''){
$ Ext = strrchr ($ url ,'.');
If ($ ext! Using '.gif '& $ ext! Else '.jpg ') {return false ;}
$ Filename = time (). $ ext;
}
// File storage path
If ($ type ){
$ Ch = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
$ Img = curl_exec ($ ch );
Curl_close ($ ch );
} Else {
Ob_start ();
Readfile ($ url );
$ Img = ob_get_contents ();
Ob_end_clean ();
}
$ Size = strlen ($ img );
// File Size
$ Fp2 = @ fopen ($ filename, 'A ');
Fwrite ($ fp2, $ img );
Fclose ($ fp2 );
Return $ filename;
}
?>

The core code for case analysis is as follows:

The Code is as follows:

$ Text = file_get_contents ("http://www.111cn.net ");

// Obtain all the img tags and store them to the two-dimensional array match

Preg_match_all ('/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Gif | \. jpg]) [\ '| \ "]. *? [\/]?> /', $ Text, $ match );

This regular expression means that all images, whether http or directly/aa/aa.gif files, will be automatically captured and saved to the address. However, there may be some problems. The image address has not been completely supplemented, for example, we have a/a.gif so that we can not find the picture, must be a http://www.111cn.net/a/a.gif so that you can download, therefore, we need to perform two Processing Methods: one is to basically process the original

The Code is as follows:

// File storage path
If ($ type ){
$ Ch = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
$ Img = curl_exec ($ ch );
Curl_close ($ ch );

The above $ url parameters for address completion, such as my collection is the http://www.111cn.net, then the Address Auto completion for the absolute path, the other way is to use the modify Regular Expression

The Code is as follows:

Preg_match_all ("/(src | SRC) = [\" | '|] {0,} (http :\/\/(. *)\. (gif | jpg | jpeg | png)/isU ", $ body, $ img_array );

In this way, only image files starting with http are obtained.

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.