溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP如何實現(xiàn)圖片防盜鏈破解

發(fā)布時間:2020-08-12 09:27:56 來源:億速云 閱讀:231 作者:小新 欄目:編程語言

這篇文章主要介紹了PHP如何實現(xiàn)圖片防盜鏈破解,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

很多小伙伴的博客,網(wǎng)站都是用圖床來實現(xiàn)的,那么現(xiàn)在很多穩(wěn)定的圖床接口都被做了防盜鏈處理,例如百度、阿里、京東、小米、搜狗等。

所以我們應該怎么避開防盜鏈直接使用圖片呢?

1 防盜的原理是什么?

當客戶端(瀏覽器)向服務器請求內(nèi)容的時候,會提交一個header,這個header中包含了如:瀏覽器信息、cookie等內(nèi)容,那么有一個叫referer的東東,也包含在這里面。

referer是干啥用的呢?

它就是告訴服務器,這個請求的來源是誰,比如:從頁面A跳轉到頁面B,那么頁面B收到的referer就是頁面A。

但是在圖片身上和這個有點不同,圖片是在html頁面加載完畢后才加載的,所以圖片收到的referer不是網(wǎng)頁的上一個頁面,而是當前頁面。

說這么多,不要被說繞了,簡單點就是:對于圖片而言,收到的referer就是引用圖片的這個網(wǎng)頁的網(wǎng)址。

那么現(xiàn)在的很多網(wǎng)站是如何利用referer來進行防圖片盜鏈的呢?

三種情況下允許引用圖片:

  1. 本網(wǎng)站。
  2. 無referer信息的情況。(服務器認為是從瀏覽器直接訪問的圖片URL,所以這種情況下能正常訪問)
  3. 白名單網(wǎng)址。

開始做防盜鏈處理

1、需要有一個服務器
2、代碼使用php

<?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\];
    }
    /\*\* 輸出內(nèi)容 \*/
    $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" />

感謝你能夠認真閱讀完這篇文章,希望小編分享PHP如何實現(xiàn)圖片防盜鏈破解內(nèi)容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!

向AI問一下細節(jié)

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

AI