- 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.
- 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.
- 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.
- 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.
- 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;
- }
In the last step, call the save_pic () function to save the image and use the following code for testing.
- // 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 practice, the content of a website, such as product information, may be collected, including anti-Leech images, and stored on the website server. At this time, you can use the regular expression matching page content to find the matched images on the page, and then download them to the website server to complete image collection. Test example:
- 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, analyze the page content, find the subject content, and call get_pic () to save the image.
- // Collect the picture on the previous mobile phone report 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, you can study it on your own. |