php中使用gd庫實(shí)現(xiàn)下載網(wǎng)頁中所有圖片
在前期的php教程就講了php gd庫可以實(shí)現(xiàn)遠(yuǎn)程圖片的下載,但是那只是下載了一張圖片,原理是一樣的,要想下載一個(gè)網(wǎng)頁的所有圖片只要使用正則表達(dá)式進(jìn)行判斷,找出所有的圖片url就可以進(jìn)行循環(huán)下載了,我特地參照網(wǎng)絡(luò)資源編寫了gd庫圖片下載類!
php代碼如下:
<?php
header("Content-type:text/html ; charset=utf-8");
if (!empty($_POST['submit'])){
$url = $_POST['url'];
//為了獲取相對(duì)路徑的圖片所做的操作
$url_fields = parse_url($url);
$main_url = $url_fields['host'];
$base_url = substr($url,0,strrpos($url, '/')+1);
//獲取網(wǎng)頁內(nèi)容
//設(shè)置代理服務(wù)器
$opts = array('http'=>array('request_fulluri'=>true));
$context = stream_context_create($opts);
$content = file_get_contents($url,false,$context);
//匹配img標(biāo)簽,將所有匹配字符串保存到數(shù)組$matches
$reg = "/<img.*?src=\"(.*?)\".*?>/i";
preg_match_all($reg, $content, $matches);
$count = count($matches[0]);
for ($i=0; $i<$count; $i++){
/*將所有圖片的url轉(zhuǎn)換為小寫
*$matches[1][$i] = strtolower($matches[1][$i]);
*/
//如果圖片為相對(duì)路徑就轉(zhuǎn)化為全路徑
if (!strpos('a'.$matches[1][$i], 'http')){
//因?yàn)?/'是第0個(gè)位置
if (strpos('a'.$matches[1][$i], '/')){
$matches[1][$i] = 'http://'.$main_url.$matches[1][$i];
}else{
$matches[1][$i] = $base_url.$matches[1][$i];
}
}
}
//過濾重復(fù)的圖片
$img_arr = array_unique($matches[1]);
//實(shí)例化圖片下載類
$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 "下載完成!哈哈,簡(jiǎn)單吧!";
}
class DownImage{
public $source;//遠(yuǎn)程圖片URL
public $save_address;//保存本地地址
public $set_extension; //設(shè)置圖片擴(kuò)展名
public $quality; //圖片的質(zhì)量(0~100,100最佳,默認(rèn)75左右)
//下載方法(選用GD庫圖片下載)
public function download(){
//獲取遠(yuǎn)程圖片信息
$info = @getimagesize($this->source);
//獲取圖片擴(kuò)展名
$mime = $info['mime'];
$type = substr(strrchr($mime, '/'), 1);
//不同的圖片類型選擇不同的圖片生成和保存函數(shù)
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';
}
//根據(jù)是否設(shè)置擴(kuò)展名來合成本地文件名
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="">
遠(yuǎn)程url地址:<input type="text" name="url" size=30 />
<input type="submit" name="submit" value="下載該頁面所有圖片" />
</form>
運(yùn)行結(jié)果如圖:

- 使用ThinkPHP自帶的Http類下載遠(yuǎn)程圖片到本地的實(shí)現(xiàn)代碼
- php讓圖片可以下載的代碼
- PHP多線程批量采集下載美女圖片的實(shí)現(xiàn)代碼(續(xù))
- php實(shí)現(xiàn)改變圖片直接打開為下載的方法
- 抓取并下載CSS中所有圖片文件的php代碼
- PHP批量采集下載美女圖片的實(shí)現(xiàn)代碼
- PHP通過正則表達(dá)式下載圖片到本地的實(shí)現(xiàn)代碼
- PHP使用pcntl_fork實(shí)現(xiàn)多進(jìn)程下載圖片的方法
- php中通過正則表達(dá)式下載內(nèi)容中的遠(yuǎn)程圖片的函數(shù)代碼
- PHP實(shí)現(xiàn)圖片批量打包下載功能
相關(guān)文章
主流PHP框架的優(yōu)缺點(diǎn)對(duì)比分析
這篇文章主要介紹了幾款今年比較熱門的主流PHP框架的優(yōu)缺點(diǎn)對(duì)比分析,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴參考下。2014-12-12
PHP jQuery表單,帶驗(yàn)證具體實(shí)現(xiàn)方法
這篇文章主要介紹了PHP jQuery表單,帶驗(yàn)證具體實(shí)現(xiàn)方法,需要的朋友可以參考下2014-02-02
PHP根據(jù)IP地址獲取所在城市具體實(shí)現(xiàn)
這篇文章主要介紹了PHP根據(jù)IP地址獲取所在城市具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-11-11
laravel框架語言包拓展實(shí)現(xiàn)方法分析
這篇文章主要介紹了laravel框架語言包拓展實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了laravel語言包的具體配置與使用方法,需要的朋友可以參考下2019-11-11
laravel 字段格式化 modle 字段類型轉(zhuǎn)換方法
今天小編就為大家分享一篇laravel 字段格式化 modle 字段類型轉(zhuǎn)換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09

