Use PHP to collect remote images. When we need to collect the content of a webpage on the network, if the images on the target website are anti-Leech, the images we directly collect are not available on our own website. When we need to collect the content of a webpage on the network, if the images on the target website are anti-Leech, the images we directly collect are not available on our own 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. Function make_dir ($ path) {if (! File_exists ($ path) {// Create $ mk = @ mkdir ($ path, 0777) if the file does not exist; // 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 gets the image name, or you can 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); // Store Directory make_dir ($ savepath); // create a storage Directory // file address $ filepath = $ savepath. $ filename; // write the file 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. // Target Image address $ pic = "/program/UploadPic/2013-4/201343155341353. jpg "; // Save the directory $ savepath =" images/"; echo save_pic ($ pic, $ savepath); in actual applications, we may collect the content of a site, for example, the product information, including images for collecting anti-Leech protection, is stored on 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: function get_pic ($ cont, $ path) {$ pattern_src = '/<[imgIMG]. *? Src =/picture/allimg/130409/123450 BK-0.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 save_pic ($ pic_item, $ path) of each graph; // 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. // 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); // get 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.
Bytes. That...