php中使用gd庫實現下載網頁中所有圖片

來源:互聯網
上載者:User

   這篇文章主要介紹了php中使用gd庫實現下載網頁中所有圖片,本文直接給出實現代碼,需要的朋友可以參考下

  在前期的php教程就講了php gd庫可以實現遠程圖片的下載,但是那隻是下載了一張圖片,原理是一樣的,要想下載一個網頁的所有圖片只要使用Regex進行判斷,找出所有的圖片url就可以進行迴圈下載了,我特地參照網路資源編寫了gd庫圖片下載類!

  php代碼如下:

  ?

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 <?php header("Content-type:text/html ; charset=utf-8"); if (!empty($_POST['submit'])){ $url = $_POST['url']; //為了擷取相對路徑的圖片所做的操作 $url_fields = parse_url($url); $main_url = $url_fields['host']; $base_url = substr($url,0,strrpos($url, '/')+1); //擷取網頁內容 //設定Proxy 伺服器 $opts = array('http'=>array('request_fulluri'=>true)); $context = stream_context_create($opts); $content = file_get_contents($url,false,$context); //匹配img標籤,將所有匹配字串儲存到數組$matches $reg = "/<img.*?src="(.*?)".*?>/i"; preg_match_all($reg, $content, $matches); $count = count($matches[0]); for ($i=0; $i<$count; $i++){ /*將所有圖片的url轉換為小寫 *$matches[1][$i] = strtolower($matches[1][$i]); */ //如果圖片為相對路徑就轉化為全路徑 if (!strpos('a'.$matches[1][$i], 'http')){ //因為'/'是第0個位置 if (strpos('a'.$matches[1][$i], '/')){ $matches[1][$i] = 'http://'.$main_url.$matches[1][$i]; }else{ $matches[1][$i] = $base_url.$matches[1][$i]; } } } //過濾重複的圖片 $img_arr = array_unique($matches[1]); //執行個體化圖片下載類 $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 "下載完成!哈哈,簡單吧!"; } class DownImage{ public $source;//遠程圖片URL public $save_address;//儲存本地地址 public $set_extension; //設定圖片副檔名 public $quality; //圖片的品質(0~100,100最佳,預設75左右) //下載方法(選用GD庫圖片下載) public function download(){ //擷取遠程圖片資訊 $info = @getimagesize($this->source); //擷取圖片副檔名 $mime = $info['mime']; $type = substr(strrchr($mime, '/'), 1); //不同的圖片類型選擇不同的圖片產生和儲存函數 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'; } //根據是否設定副檔名來合成本地檔案名稱 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); }   //產生本地檔案路徑 $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=""> 遠程url地址:<input type="text" name="url" size=30 /> <input type="submit" name="submit" value="下載該頁面所有圖片" /> </form>

  運行結果如圖:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.