This article mainly introduces the PHP remote Image Collection Detailed tutorial, step by step to achieve the Image collection function, a simple Getting Started Tutorial, for more information, see. when we need to collect the content of a webpage on the network, if the image on the target website is anti-Leech, the images we directly collect are not available on our website. Then we use the program to download the images on the target website to our website server, and then we can call the picture.
This article uses PHP to collect remote images. Basic process:
1. obtain the image address of the target website.
2. read the image content.
3. create a path to save the image and name the image.
4. write the image content.
5. complete.
We can implement this process by writing several functions.
The make_dir () function creates a directory. Determine whether the Directory of the image file to be saved exists. if it does not exist, create a directory and set the directory to writable.
The code is as follows: function make_dir ($ path ){
If (! File_exists ($ path) {// creation if the object does not exist
$ Mk = @ mkdir ($ path, 0777); // permission
@ Chmod ($ path, 0777 );
}
Return true;
}
The read_filetext () function gets the image content. Use fopen to open the image file and fread to read the content of the image file.
The code is as follows:
Function read_filetext ($ filepath ){
$ Filepath = trim ($ filepath );
$ Htmlfp = @ fopen ($ filepath, "r ");
// Remote
If (strstr ($ filepath ,"://")){
While ($ data = @ fread ($ htmlfp, 500000 )){
$ String. = $ data;
}
}
// Local
Else {
$ String = @ fread ($ htmlfp, @ filesize ($ filepath ));
}
@ Fclose ($ htmlfp );
Return $ string;
}
The write_filetext () function writes the image content fputs into the file to save the image file.
The code is as follows:
Function write_filetext ($ filepath, $ string ){
// $ String = stripSlashes ($ string );
$ Fp = @ fopen ($ filepath, "w ");
@ Fputs ($ fp, $ string );
@ Fclose ($ fp );
}
The get_filename () function is used to obtain the image name. you can also customize the file name to save.
The code is as follows:
Function get_filename ($ filepath ){
$ Fr = explode ("/", $ filepath );
$ Count = count ($ fr)-1;
Return $ fr [$ count];
}
Then, combine several functions, call them in the save_pic () function, and return the saved image path.
The code is as follows:
Function save_pic ($ url, $ savepath = ''){
// Processing address
$ Url = trim ($ url );
$ Url = str_replace ("", "% 20", $ url );
// Read the file
$ String = read_filetext ($ url );
If (empty ($ string )){
Echo 'file not readable '; exit;
}
// File name
$ Filename = get_filename ($ url );
// Storage directory
Make_dir ($ savepath); // create a storage directory
// File address
$ Filepath = $ savepath. $ filename;
// Write an object
Write_filetext ($ filepath, $ string );
Return $ filepath;
}
The last step is to call the save_pic () function to save the image. We use the following code for testing.
The code is as follows:
// Target Image address
$ Pic = "http://img0.pconline.com.cn/pconline/1205/06/2776119_end1_thumb.jpg ";
// Save the Directory
$ Savepath = "images /";
Echo save_pic ($ pic, $ savepath );
In practical applications, we may collect the content of a website, such as product information, including pictures of anti-Leech and save them to the website server. At this time, we can use the regular expression matching page content to find the matching images on the page, and then download them to the website server to complete image collection. The following code is for testing only:
The code is as follows:
Function get_pic ($ cont, $ path ){
$ Pattern_src = '/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Gif | \. jpg]) [\ '| \ "]. *? [\/]?> /';
$ Num = preg_match_all ($ pattern_src, $ cont, $ match_src );
$ Pic_arr = $ match_src [1]; // Obtain the image array
Foreach ($ pic_arr as $ pic_item) {// cyclically retrieve the address of each graph
Save_pic ($ pic_item, $ path); // download and save the image
Echo "[OK]...! ";
}
}
Then, we analyze the page content, find the subject content, and call get_pic () to save the image.
The code is as follows:
// We collect pictures of the previous report on mobile phone content page of the Pacific computer network.
$ Url = "http://gz.pconline.com.cn/321/3215791.html ";
$ Content = file_get_contents ($ url); // Obtain the webpage content
$ Preg = '#
(.*)
# IUs ';
Preg_match_all ($ preg, $ content, $ arr );
$ Cont = $ arr [1] [0];
Get_pic ($ cont, 'IMG /');
The above code can be used to collect images, but some scenarios have not been taken into account. for example, if the target website has been redirected for more than 302 times, the target website has been protected against multiple types of attacks, let's try it for those who like it.