本文实例讲述了php实现图片防盗链破解操作。分享给大家供大家参考,具体如下:

很多小伙伴的博客,网站都是用图床来实现的,那么现在很多稳定的图床接口都被做了防盗链处理,例如百度、阿里、京东、小米、搜狗等。

所以我们应该怎么避开防盗链直接使用图片呢?

1 防盗的原理是什么?

当客户端(浏览器)向服务器请求内容的时候,会提交一个header,这个header中包含了如:浏览器信息、cookie等内容,那么有一个叫referer的东东,也包含在这里面。

referer是干啥用的呢?

它就是告诉服务器,这个请求的来源是谁,比如:从页面a跳转到页面b,那么页面b收到的referer就是页面a。

但是在图片身上和这个有点不同,图片是在html页面加载完毕后才加载的,所以图片收到的referer不是网页的上一个页面,而是当前页面。

说这么多,不要被说绕了,简单点就是:对于图片而言,收到的referer就是引用图片的这个网页的网址。

那么现在的很多网站是如何利用referer来进行防图片盗链的呢?

三种情况下允许引用图片:

  1. 本网站。
  2. 无referer信息的情况。(服务器认为是从浏览器直接访问的图片url,所以这种情况下能正常访问)
  3. 白名单网址。

开始做防盗链处理

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\];
    }
    /\*\* 输出内容 \*/
    $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″ />