-
- header('Content-Type:text/html;charset=utf-8');
- /**
- * A class for grabbing pictures
- */
- class download_image {
- public $_save_path = NULL; //The way to save pictures
- public $_limit_size = NULL; //Limit the size of pictures
- public static $_img_url_old = array(); //Storage the captured image link address
- public static $_a_page_url = array(); / /Storage the crawled page
- 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 ) ) { //Determine whether the current page has been crawled
- self:: $_a_page_url[] = $site_url; //Save the hyperlink into a static array
- } else {
- return; //If it has been crawled, jump out
- }
- $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 to prevent entering an infinite loop
- * Exit the current page link representation ('', '#', '/')
- */
- 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 hyperlinks that meet the requirements into 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://, it cannot be accessed directly
- // When the image link address is a relative address, the address is reassembled
- $a_domain_url = substr( $site_url, 0, strpos( $site_url, '/' ,8 ) + 1 );
- $a_img_url = $a_domain_url.$vals;
- }
- //Recursive call, access each hyperlink page
- $this->get_all_page_image( $a_img_url );
- }
- }
- }
-
- /**
- * Download all image links under the current page
- * @param $site_url
- */
- public function download_the_page_image( $site_url ) {
- // Get all the contents of the current link address page
- $img_pattern = NULL;
- $content = @file_get_contents( $site_url );
- $img_pattern = "|]+src=['\" ]?([^ '\"?]+)['\" >]|U";
- //Globally match all Picture link in
- $img_out = array();
- preg_match_all( $img_pattern, $content, $img_out, PREG_SET_ORDER );
- echo '
'. $site_url . 'Total found' . count($img_out ) . 'Picture';
- //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, the address is reassembled
- $domain_url = substr( $site_url, 0, strpos( $site_url, '/ ',8 ) + 1 );
- $img_url = $domain_url.$img_url;
- }
- $pic_name = basename( $img_url ); //Get the picture name
-
- if ( in_array( $img_url, self::$_img_url_old ) ) {
- echo $img_url .'This image has been captured!
';
- return;
- }
- //Get the image content and write it into a string
- $img_data = @file_get_contents( $img_url );
- if ( strlen($img_data) < $this->_limit_size ) { //The image size is within the limit range Inside
- $img_boo = @file_put_contents( $this->_save_path.md5(microtime()).$pic_name, $img_data );
- if ( $img_boo ) {
- echo $img_url .'The picture was saved successfully!
';
- self::$_img_url_old[] = $img_url;
- } else {
- echo $img_url .'Picture saving failed!
';
- }
- } else {
- echo $img_url .'The image size is within the limit outside!
';
- }
- }
- }
- set_time_limit(0);
- $download_images = new download_image('surces_Img/',1024*1024*100);
- $download_images-> get_all_page_image('http://www.baidu.com/');
- ?>
-
-
Copy code
|