Php code for capturing and saving website images

Source: Internet
Author: User
Tags website server
Php captures and saves the implementation code of website images ,. Php captures and saves the implementation code of website images. this program captures the source code of the webpage, obtains, analyzes the image links, and merges the same image links, php code for capturing and saving website images,

This program allows you to capture the source code of a webpage, obtain and analyze image links, and combine the same image links to capture images. Use php's powerful network content processing function to capture all images on a specified website and save them to the current directory. the following code is used:

<? Php/* complete webpage content capture function */function get_img_url ($ site_name) {$ site_fd = fopen ($ site_name, "r"); $ site_content = ""; while (! Feof ($ site_fd) {$ site_content. = fread ($ site_fd, 1024);}/* use a regular expression to obtain the image link */$ reg_tag = '//'; $ ret = preg_match_all ($ reg_tag, $ site_content, $ match_result); fclose ($ site_fd); return $ match_result [1];}/* modify the image link */function revise_site ($ site_list, $ base_site) {foreach ($ site_list as $ site_item) {if (preg_match ('/^ http/', $ site_item) {$ return_list [] = $ site_item ;} else {$ return_list [] = $ base _ Site. "/". $ site_item;} return $ return_list;}/* get the image name and save it in the specified position */function get_pic_file ($ pic_url_array, $ pos) {$ reg_tag = '/. *\/(. *?) $/'; $ Count = 0; foreach ($ pic_url_array as $ pic_item) {$ ret = preg_match_all ($ reg_tag, $ pic_item, $ t_pic_name); $ pic_name = $ pos. $ t_pic_name [1] [0]; $ pic_url = $ pic_item; print ("Downloading ". $ pic_url. ""); $ img_read_fd = fopen ($ pic_url, "r"); $ img_write_fd = fopen ($ pic_name, "w"); $ img_content = ""; while (! Feof ($ img_read_fd) {$ img_content. = fread ($ img_read_fd, 1024);} fwrite ($ img_write_fd, $ img_content); fclose ($ img_read_fd); fclose ($ img_write_fd ); print ("[OK]");} return 0;} function main () {/* webpage address of the image to be crawled */$ site_name =" http://www.bkjia.com/sheying/391528.html "; $ Img_url = get_img_url ($ site_name); $ img_url_revised = revise_site ($ img_url, $ site_name); $ img_url_unique = array_unique ($ img_url_revised ); // unique array get_pic_file ($ img_url_unique ,". /") ;}main () ;?>

This program also needs to be improved. if the images are in different directories on the website server but the file names are the same, the images may be different at this time, but at the end of saving, the subsequent images will overwrite the previously saved images,SolutionIt is to retrieve whether the file name already exists in the current directory before each save. If yes, rename the image to be saved.

The above is the method for capturing and saving website images in php shared with you, and the bug fixes in practice. I hope this article will be helpful for your learning.

Capture, this program implements the web page source code capture, image link acquisition, analysis, and the same picture link merge function, achieve the image capture...

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.