不少小夥伴的博客,網站都是用圖牀來實現的,那麼如今不少穩定的圖牀接口都被作了防盜鏈處理,例如百度、阿里、京東、小米、搜狗等。php
因此咱們應該怎麼避開防盜鏈直接使用圖片呢?html
當客戶端(瀏覽器)向服務器請求內容的時候,會提交一個header,這個header中包含了如:瀏覽器信息、cookie等內容,那麼有一個叫referer的東東,也包含在這裏面。瀏覽器
referer是幹啥用的呢?服務器
它就是告訴服務器,這個請求的來源是誰,好比:從頁面A跳轉到頁面B,那麼頁面B收到的referer就是頁面A。cookie
可是在圖片身上和這個有點不一樣,圖片是在html頁面加載完畢後才加載的,因此圖片收到的referer不是網頁的上一個頁面,而是當前頁面。curl
說這麼多,不要被說繞了,簡單點就是:對於圖片而言,收到的referer就是引用圖片的這個網頁的網址。編輯器
那麼如今的不少網站是如何利用referer來進行防圖片盜鏈的呢?學習
三種狀況下容許引用圖片:網站
一、須要有一個服務器
二、代碼使用phpui
<?php class ImgBridge{ private $water=''; private $imgUrl=''; private $referer=''; private $ua='MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'; private $imgCode=''; private $imgHeader=''; private $imgBody=''; private $imgType=''; public function \_\_construct($config=array()){ foreach($config as $key=>$value){ $this->$key=$value; } } public function getImg($imgUrl){ $this->imgUrl=$imgUrl; /\*\* 處理url \*/ if(substr($this->imgUrl,0,7)!=='http://' && substr($this->imgUrl,0,8)!=='https://'){ $this->imgUrl='http://'.$this->imgUrl; } /\*\* 解析url中的host \*/ $url\_array=parse\_url($this->imgUrl); /\*\* 設置referer \*/ $this->referer=$this->referer==""?'http://'.$url\_array\['host'\]:$this->referer; /\*\*開始獲取 \*/ $this->urlOpen(); $this->imgBody; /\*\*處理錯誤 \*/ if($this->imgCode!=200){ $this->error(1); exit(); } /\*\*獲取圖片格式 \*/ preg\_match("/Content-Type: image\\/(.+?)\\n/sim",$this->imgHeader,$result); /\*\*看看是否是圖片 \*/ if(!isset($result\[1\])){ $this->error(2); exit(); }else{ $this->imgType=$result\[1\]; } /\*\* 輸出內容 \*/ $this->out(); } private function out(){ /\*\* gif 不處理,直接出圖 \*/ if($this->imgType=='gif'){ header("Content-Type: image/gif"); echo $this->imgBody; exit(); } header("Content-Type: image/png"); /\*\* 其餘類型的,加水印 \*/ $im=imagecreatefromstring($this->imgBody); $white = imagecolorallocate($im, 255, 255, 255); /\*加上水印\*/ if($this->water){ imagettftext($im, 12, 0, 20, 20, $white, "/fonts/hwxh.ttf", $this->water); } imagepng($im); } private function error($err){ header("Content-Type: image/jpeg"); $im=imagecreatefromstring(file\_get\_contents('./default.jpg')); imagejpeg($im); } private function urlOpen() { $ch = curl\_init(); curl\_setopt($ch, CURLOPT\_URL, $this->imgUrl); curl\_setopt($ch, CURLOPT\_USERAGENT, $this->ua); curl\_setopt ($ch,CURLOPT\_REFERER,$this->referer); curl\_setopt($ch, CURLOPT\_RETURNTRANSFER, 1); curl\_setopt($ch, CURLOPT\_HEADER, 1); /\*\*跳轉也要 \*/ curl\_setopt($ch, CURLOPT\_FOLLOWLOCATION, true); /\*\* 支持https \*/ $opt\[CURLOPT\_SSL\_VERIFYHOST\] = 2; $opt\[CURLOPT\_SSL\_VERIFYPEER\] = FALSE; curl\_setopt\_array($ch, $opt); $response = curl\_exec($ch); $this->imgCode=curl\_getinfo($ch, CURLINFO\_HTTP\_CODE) ; if ($this->imgCode == '200') { $headerSize = curl\_getinfo($ch, CURLINFO\_HEADER\_SIZE); $this->imgHeader = substr($response, 0, $headerSize); $this->imgBody = substr($response, $headerSize); return ; } curl\_close($ch); } } $img=new ImgBridge(array('water'=>'')); $img->getImg(strstr($\_SERVER\["QUERY\_STRING"\], "http"));
代碼命名爲dl.php
那麼直接能夠訪問
http://域名/dl.php?url=防盜鏈圖片地址
http://www.likeyunba.com/2.php?url=
請不要拿個人直接用,個人不會長期放着的,只保留短暫1-2個月用於給大家體驗。
我用135編輯器上傳一張圖片,得到圖片地址
https://image.135editor.com/files/users/740/7407329/201912/zTeFAx8R_Cmea.jpg
加上反向代理,破解防盜鏈處理
http://www.likeyunba.com/2.php?url=https://image.135editor.com/files/users/740/7407329/201912/zTeFAx8R_Cmea.jpg
HTML格式
<img src="http://www.likeyunba.com/2.php?url=https://image.135editor.com/files/users/740/7407329/201912/zTeFAx8R_Cmea.jpg" width="500" />
原文:如何繞過圖片防盜鏈,在本身網站引用別人網站的圖片?
不懂能夠找我學習
WeChat:face6009
網站:likeyunba.com