Gd Library image download Class php code for downloading all images on the webpage

Source: Internet
Author: User
Tags imagejpeg
In the previous php Tutorial, the phpgd library can be used to download remote images. However, it only downloads an image in the same principle, to download all the images on a Web page, you only need to use regular expressions to judge and find out all the image URLs. The php code is as follows:
Copy codeThe code is as follows:
Header ("Content-type: text/html; charset = utf-8 ");
If (! Empty ($ _ POST ['submit ']) {
$ Url = $ _ POST ['URL'];
// Operations to obtain images with relative paths
$ Url_fields = parse_url ($ url );
$ Main_url = $ url_fields ['host'];
$ Base_url = substr ($ url, 0, strrpos ($ url, '/') + 1 );
// Obtain the webpage content
// Sets the proxy server
$ Opts = array ('http' => array ('request _ fulluri '=> true ));
$ Context = stream_context_create ($ opts );
$ Content = file_get_contents ($ url, false, $ context );
// Match the img tag and save all matching strings to the array $ matches
$ Reg = "// I ";
Preg_match_all ($ reg, $ content, $ matches );
$ Count = count ($ matches [0]);
For ($ I = 0; $ I <$ count; $ I ++ ){
/* Convert the URLs of all images to lowercase letters
* $ Matches [1] [$ I] = strtolower ($ matches [1] [$ I]);
*/
// Convert the image to the full path if it is a relative path
If (! Strpos ('A'. $ matches [1] [$ I], 'http ')){
// Because '/' is 0th locations
If (strpos ('A'. $ matches [1] [$ I], '/') {
$ Matches [1] [$ I] = 'http: // '. $ main_url. $ matches [1] [$ I];
} Else {
$ Matches [1] [$ I] = $ base_url. $ matches [1] [$ I];
}
}
}
// Filter duplicate images
$ Img_arr = array_unique ($ matches [1]);
// Instantiate the image download class
$ GetImg = new DownImage ();
$ Url_count = count ($ img_arr );
For ($ I = 0; $ I <$ url_count; $ I ++ ){
$ GetImg-> source = $ img_arr [$ I];
$ GetImg-> save_address = './pic /';
$ File = $ getImg-> download ();
}
Echo "download complete! Haha, that's easy! ";
}
Class DownImage {
Public $ source; // remote image URL
Public $ save_address; // save the local address
Public $ set_extension; // sets the image extension.
Public $ quality; // image quality (0 ~ 100,100 is the best. the default value is about 75)
// Download method (using GD Library image download)
Public function download (){
// Obtain remote image information
$ Info = @ getimagesize ($ this-> source );
// Obtain the image extension.
$ Mime = $ info ['Mime '];
$ Type = substr (strrchr ($ mime, '/'), 1 );
// Select different image generation and storage functions for different Image types
Switch ($ type ){
Case 'jpeg ':
$ Img_create_func = 'imagecreatefromjpeg ';
$ Img_save_func = 'imagejpeg ';
$ New_img_ext = 'jpg ';
$ Image_quality = isset ($ this-> quality )? $ This-& gt; quality: 100;
Break;
Case 'PNG ':
$ Img_create_func = 'imagecreatefrompng ';
$ Img_save_func = 'imagepng ';
$ New_img_ext = 'PNG ';
Break;
Case 'bmp ':
$ Img_create_func = 'imagecreatefrombmp ';
$ Img_save_func = 'imagebmp ';
$ New_img_ext = 'bmp ';
Break;
Case 'GIF ':
$ Img_create_func = 'imagecreatefromgif ';
$ Img_save_func = 'imagegif ';
$ New_img_ext = 'GIF ';
Break;
Case 'vnd. wap. wbmp ':
$ Img_create_func = 'imagecreatefromwbmp ';
$ Img_save_func = 'imagewbmp ';
$ New_img_ext = 'bmp ';
Break;
Case 'xbm ':
$ Img_create_func = 'imagecreatefromxbm ';
$ Img_save_func = 'imagexbm ';
$ New_img_ext = 'xbm ';
Break;
Default:
$ Img_create_func = 'imagecreatefromjpeg ';
$ Img_save_func = 'imagejpeg ';
$ New_img_ext = 'jpg ';
}
// Merge local file names based on whether to set the extension.
If (isset ($ this-> set_extension )){
$ Ext = strrchr ($ this-> source ,".");
$ Strlen = strlen ($ ext );
$ Newname = basename (substr ($ this-> source, 0,-$ strlen). '.'. $ new_img_ext;
} Else {
$ Newname = basename ($ this-> source );
}

// Generate the local file path
$ Save_address = $ this-> save_address. $ newname;
$ Img = @ $ img_create_func ($ this-> source );
If (isset ($ image_quality )){
$ Save_img =@$ img_save_func ($ img, $ save_address, $ image_quality );
} Else {
$ Save_img =@$ img_save_func ($ img, $ save_address );
}
Return $ save_img;
}
}
?>


Running result

The downloaded image is saved in the pic folder of the current directory in this example!

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.