溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php寫一個(gè)能實(shí)現(xiàn)下載網(wǎng)頁(yè)所有圖片的圖片下載類

發(fā)布時(shí)間:2021-09-07 09:07:06 來(lái)源:億速云 閱讀:116 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“php寫一個(gè)能實(shí)現(xiàn)下載網(wǎng)頁(yè)所有圖片的圖片下載類”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“php寫一個(gè)能實(shí)現(xiàn)下載網(wǎng)頁(yè)所有圖片的圖片下載類”吧!

php代碼如下:

復(fù)制代碼 代碼如下:


<?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)頁(yè)內(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)?#39;/'是第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];
}
}
}
//過(guò)濾重復(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庫(kù)圖片下載)
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ò)展名來(lái)合成本地文件名
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="下載該頁(yè)面所有圖片" />
</form>


運(yùn)行結(jié)果如圖:

php寫一個(gè)能實(shí)現(xiàn)下載網(wǎng)頁(yè)所有圖片的圖片下載類下載的圖片本例中保存在當(dāng)前目錄的pic文件夾下!

到此,相信大家對(duì)“php寫一個(gè)能實(shí)現(xiàn)下載網(wǎng)頁(yè)所有圖片的圖片下載類”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI