In the previous PHP tutorial on the PHP GD Library can achieve remote picture download, but that just downloaded a picture, the principle is the same, to download a Web page of all the pictures as long as the use of regular expressions to judge, find all the picture URL can be recycled to download, I specially refer to the network resources to write the GD library picture download Class! The
PHP code is as follows:
<?php header ("content-type:text/html;
Charset=utf-8 ");
if (!empty ($_post[' submit ')) {$url = $_post[' url '];
To get a picture of the relative path $url _fields = Parse_url ($url);
$main _url = $url _fields[' host '];
$base _url = substr ($url, 0,strrpos ($url, '/') +1);
Get Web content/Set 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 + +) {/* to 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). $matches [1][$i], ' http ') {//Because '/' is 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 duplicate picture $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 $quali Ty Picture quality (0~100,100 best, default 75 or so)//download method (choose GD Library picture download) public function download () {//Get remote picture information $info = @getimagesize ($this->s
Ource);
Gets the picture extension $mime = $info [' MIME '];
$type = substr (STRRCHR ($mime, '/'), 1);
Different picture types choose different pictures to generate and save function 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 ';
The local filename if (isset ($this->set_extension)) {$ext = STRRCHR ($this->source, ".") is synthesized based on whether or not to set the extension;
$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=" SUBM It "name=" submit "value=" Download all pictures of this page/> </form>
Run the results as shown in figure: