Gd library image download class php code for downloading all images on the webpage

Source: Internet
Author: User
Tags imagejpeg

The php code is as follows:
Copy codeThe Code is as follows:
<? Php
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;
}
}
?>
<Form method = "POST" action = "">
Remote url: <input type = "text" name = "url" size = 30/>
<Input type = "submit" name = "submit" value = "Download all images on this page"/>
</Form>

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.