- Header ('content-Type: text/html; charset = utf-8 ');
- /**
- * A class used to capture images
- */
- Class download_image {
- Public $ _ save_path = NULL; // save path strength of the image
- Public $ _ limit_size = NULL; // the maximum image size
- Public static $ _ img_url_old = array (); // store the URL of the captured image
- Public static $ _ a_page_url = array (); // stores captured pages
- Public function _ construct ($ _ save_path, $ _ limit_size ){
- $ This-> _ save_path =$ _ save_path;
- $ This-> _ limit_size = $ _ limit_size;
- }
- Public function get_all_page_image ($ site_url ){
- If ($ site_url = ''){
- Return false;
- }
- If (! In_array ($ site_url, self: $ _ a_page_url) {// determines whether the current page has been crawled.
- Self: $ _ a_page_url [] = $ site_url; // Save the hyperlink to a static array
- } Else {
- Return; // jump out if it has been captured
- }
- $ This-> download_the_page_image ($ site_url );
- $ Content = @ file_get_contents ($ site_url );
- $ A_page_url = "|] + href = ['\"]? ([^ '\ "?] +) ['\ ">] | U ";
- $ All_url = array ();
- Preg_match_all ($ a_page_url, $ content, $ all_url, PREG_SET_ORDER );
- If ($ all_url! = NULL ){
- Foreach ($ all_url as $ key => $ val ){
- /**
- * Static hyperlinks prevent endless loops
- * Representation of the link on the current page ('','#','/')
- */
- If (trim ($ val [1])! = ''&&! In_array ($ val [1], self: $ _ a_page_url )&&! In_array ($ val [1], array ('#', '/', $ site_url ))){
- Self: $ _ a_page_url [] = $ val [1]; // write the required hyperlink to the static array.
- }
- }
- }
- If (self: $ _ a_page_url! = NULL ){
- Foreach (self ::$ _ a_page_url as $ keys => $ vals ){
- If (strpos ($ vals, 'http: // ') = false) {// when the hyperlink does not contain http: //, you cannot directly access
- // When the image link address is a relative address, it is a patchwork address.
- $ A_domain_url = substr ($ site_url, 0, strpos ($ site_url, '/', 8) + 1 );
- $ A_img_url = $ a_domain_url. $ vals;
- }
- // Recursive call to access every hyperlink page
- $ This-> get_all_page_image ($ a_img_url );
- }
- }
- }
- /**
- * Download all image links on the current page
- * @ Param $ site_url <页面地址>
- */
- Public function download_the_page_image ($ site_url ){
- // Obtain all content on the current link page
- $ Img_pattern = NULL;
- $ Content = @ file_get_contents ($ site_url );
- $ Img_pattern = "|] + src = ['\"]? ([^ '\ "?] +) ['\ ">] | U ";
- // Globally match all image links
- $ Img_out = array ();
- Preg_match_all ($ img_pattern, $ content, $ img_out, PREG_SET_ORDER );
- Echo ''. $ site_url. 'found'. count ($ img_out). 'image ';
- // Print_r ($ img_out [1]);
- Foreach ($ img_out as $ key => $ val ){
- // Echo htmlspecialchars ($ val [1]).'
';
- $ This-> save_one_image ($ site_url, $ val [1]);
- }
- }
- Public function save_one_image ($ site_url, $ img_url ){
- If (strpos ($ img_url, 'http: // ') === false ){
- // When the image link address is a relative address, it is a patchwork address.
- $ Domain_url = substr ($ site_url, 0, strpos ($ site_url, '/', 8) + 1 );
- $ Img_url = $ domain_url. $ img_url;
- }
- $ Pic_name = basename ($ img_url); // Obtain the image name
- If (in_array ($ img_url, self: $ _ img_url_old )){
- Echo $ img_url. 'the image has been captured!
';
- Return;
- }
- // Get the image content and write a string
- $ Img_data = @ file_get_contents ($ img_url );
- If (strlen ($ img_data) <$ this-> _ limit_size) {// The image size is within the limit.
- $ Img_boo = @ file_put_contents ($ this-> _ save_path.md5 (microtime (). $ pic_name, $ img_data );
- If ($ img_boo ){
- Echo $ img_url. 'the image is saved successfully!
';
- Self: $ _ img_url_old [] = $ img_url;
- } Else {
- Echo $ img_url. 'an error occurred while saving the image!
';
- }
- } Else {
- Echo $ img_url. 'the image size is out of the limit!
';
- }
- }
- }
- Set_time_limit (0 );
- $ Download_images = new download_image ('surces _ Img/', 1024*1024*100 );
- $ Download_images-> get_all_page_image ('http: // www.baidu.com /');
- ?>
|