gd庫圖片下載類實現(xiàn)下載網(wǎng)頁所有圖片的php代碼
更新時間:2012年08月20日 22:02:35 作者:
在前期的php教程就講了php gd庫可以實現(xiàn)遠程圖片的下載,但是那只是下載了一張圖片,原理是一樣的,要想下載一個網(wǎng)頁的所有圖片只要使用正則表達式進行判斷,找出所有的圖片url就可以進行循環(huán)下載了,我特地參照網(wǎng)絡資源編寫了gd庫圖片下載類!
php代碼如下:
<?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);
//獲取網(wǎng)頁內(nèi)容
//設置代理服務器
$opts = array('http'=>array('request_fulluri'=>true));
$context = stream_context_create($opts);
$content = file_get_contents($url,false,$context);
//匹配img標簽,將所有匹配字符串保存到數(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]);
*/
//如果圖片為相對路徑就轉(zhuǎn)化為全路徑
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; //圖片的質(zhì)量(0~100,100最佳,默認75左右)
//下載方法(選用GD庫圖片下載)
public function download(){
//獲取遠程圖片信息
$info = @getimagesize($this->source);
//獲取圖片擴展名
$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ù)是否設置擴展名來合成本地文件名
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>
運行結果如圖:
復制代碼 代碼如下:
<?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);
//獲取網(wǎng)頁內(nèi)容
//設置代理服務器
$opts = array('http'=>array('request_fulluri'=>true));
$context = stream_context_create($opts);
$content = file_get_contents($url,false,$context);
//匹配img標簽,將所有匹配字符串保存到數(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]);
*/
//如果圖片為相對路徑就轉(zhuǎn)化為全路徑
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; //圖片的質(zhì)量(0~100,100最佳,默認75左右)
//下載方法(選用GD庫圖片下載)
public function download(){
//獲取遠程圖片信息
$info = @getimagesize($this->source);
//獲取圖片擴展名
$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ù)是否設置擴展名來合成本地文件名
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>
運行結果如圖:
下載的圖片本例中保存在當前目錄的pic文件夾下!
您可能感興趣的文章:
- php使用ffmpeg獲取視頻信息并截圖的實現(xiàn)方法
- Linux環(huán)境下php實現(xiàn)給網(wǎng)站截圖的方法
- php實現(xiàn)粘貼截圖并完成上傳功能
- php 獲取SWF動畫截圖示例代碼
- THINKPHP+JS實現(xiàn)縮放圖片式截圖的實現(xiàn)
- 用PHP代碼在網(wǎng)頁上生成圖片
- php中使用gd庫實現(xiàn)下載網(wǎng)頁中所有圖片
- PHP 抓取網(wǎng)頁圖片并且另存為的實現(xiàn)代碼
- 用php實現(xiàn)的獲取網(wǎng)頁中的圖片并保存到本地的代碼
- php通過執(zhí)行CutyCapt命令實現(xiàn)網(wǎng)頁截圖的方法
相關文章
linux系統(tǒng)下php安裝mbstring擴展的二種方法
這篇文章主要介紹了linux系統(tǒng)環(huán)境下,php安裝mbstring擴展的二種方法,大家參考使用吧2014-01-01php中重定向網(wǎng)頁跳轉(zhuǎn)方法總結案例教程
這篇文章主要介紹了php中重定向網(wǎng)頁跳轉(zhuǎn)方法總結案例教程,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08thinkPHP5.1框架路由::get、post請求簡單用法示例
這篇文章主要介紹了thinkPHP5.1框架路由::get、post請求簡單用法,結合實例形式分析了thinkPHP5.1路由get、post請求基本使用方法,需要的朋友可以參考下2019-05-05PHP隨手筆記整理之PHP腳本和JAVA連接mysql數(shù)據(jù)庫
這篇文章主要介紹了PHP隨手筆記整理之PHP腳本和JAVA連接mysql數(shù)據(jù)庫的相關資料,需要的朋友可以參考下2015-11-11Mac版PhpStorm之XAMPP整合apache服務器配置的圖文教程詳解
選擇在PhpStorm集成apache服務器,但是很多朋友不知道是如何操作的,下面小編分步驟通過圖文的形式給大家介紹Mac版PhpStorm之XAMPP整合apache服務器配置的教程,感興趣的朋友一起看看吧2016-10-10