The PHP code is as follows:
Copy 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, 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 + +) {
/* Converts 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 full path
if (!strpos (' a '. $matches [1][$i], ' http ')} {
Because '/' is the 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 pictures
$img _arr = Array_unique ($matches [1]);
Materialized 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; Set Picture extension
Public $quality; Picture quality (0~100,100 best, default 75 or so)
Download method (choose GD Library picture to download)
Public function Download () {
Get Remote picture information
$info = @getimagesize ($this->source);
Get picture extension
$mime = $info [' MIME '];
$type = substr (STRRCHR ($mime, '/'), 1);
Different picture types choose different pictures to build 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 or not to set an 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);
}
To generate a 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>
Run the results as shown in figure:
Download the picture in this example, save in the current directory under the PIC folder!