Home >Backend Development >PHP Tutorial >Use gd library in php to download all pictures in the webpage_PHP tutorial
This article mainly introduces the use of the gd library in php to download all the pictures in the webpage. This article directly gives the implementation code. , friends in need can refer to it
In the early PHP tutorial, I mentioned that the PHP gd library can realize remote image downloading, but it only downloads one image. The principle is the same. If you want to download all the images on a web page, just use regular expressions to judge. , find all the image URLs and then download them in a loop. I specially wrote the gd library image download class with reference to network resources!
The php code is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
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); //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 and save all matching strings to the array $matches $reg = "/ preg_match_all($reg, $content, $matches); $count = count($matches[0]); for ($i=0; $i<$count; $i ){ /*Convert all image urls to lowercase *$matches[1][$i] = strtolower($matches[1][$i]); */ //If the image is a relative path, convert it to a full path if (!strpos('a'.$matches[1][$i], 'http')){ //Because '/' is the 0th 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 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 completed! Haha, easy!"; } class DownImage{ public $source;//Remote image URL public $save_address;//Save local address public $set_extension; //Set image extension public $quality; //Quality of the image (0~100, 100 is the best, default is around 75) //Download method (select GD library image download) public function download(){ //Get remote picture information $info = @getimagesize($this->source); //Get image extension $mime = $info['mime']; $type = substr(strrchr($mime, '/'), 1); //Select different image generation and saving 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->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'; } //Synthesize the local file name according to 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; } } ?> |
The running results are as shown below: