GD Library image download class implement PHP code to download all pictures of the Web page

Source: Internet
Author: User
Tags imagejpeg
The PHP code is as follows:
Copy the code code as follows:
<?php
Header ("content-type:text/html; Charset=utf-8 ");
if (!empty ($_post[' submit ')) {
$url = $_post[' url '];
Action to get a picture of a relative path
$url _fields = Parse_url ($url);
$main _url = $url _fields[' host ');
$base _url = substr ($url, 0,strrpos ($url, '/') +1);
Get Web page Content
Setting up a 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 to 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 URL of all pictures to lowercase
* $matches [1][$i] = Strtolower ($matches [1][$i]);
*/
If the picture is a relative path, it is converted to a full path
if (!strpos (' a '. $matches [1][$i], ' http ')} {
Because '/' is a No. 0 position
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 for duplicate images
$img _arr = Array_unique ($matches [1]);
Instantiate a picture 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, simple! ";
}
Class downimage{
public $source;//Remote picture URL
Public $save _address;//Save Local address
Public $set _extension; To set the picture name extension
Public $quality; Picture quality (0~100,100 best, default 75 or so)
Download method (choose GD library image download)
Public function Download () {
Get Remote picture information
$info = @getimagesize ($this->source);
Get the picture name extension
$mime = $info [' MIME '];
$type = substr (STRRCHR ($mime, '/'), 1);
Different picture types choose different picture generation and save functions
Switch ($type) {
Case ' JPEG ':
$img _create_func = ' imagecreatefromjpeg ';
$img _save_func = ' imagejpeg ';
$new _img_ext = ' jpg ';
$image _quality = isset ($this->quality)? $this->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 ';
}
To synthesize a local file name based on whether the extension is set
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 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 address: <input type= "text" name= "url" size=30/>
<input type= "Submit" name= "submit" value= "Download all pictures of this page"/>
</form>
  • 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.