"> ');
本站使用了 Pjax 等基于 JavaScript 的开发技术,但您的浏览器已禁用 JavaScript,请开启 JavaScript 以保证网站正常显示!

破解图片防盗链

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=防盗链图片地址

原文:https://segmentfault.com/a/1190000021435532


正在加载...

推广

 继续浏览关于 的文章

 本文最后更新于:2024/07/03 10:28:07,可能因经年累月而与现状有所差异

 引用转载请注明:龙氏小哥 > 搬砖笔记 > 破解图片防盗链

发现共鸣


Warning: curl_setopt() expects parameter 1 to be resource, null given in /www/wwwroot/blog.longshi.org/usr/themes/armx/functions.php on line 1531
如是说:一阴一阳之谓道,乐天知命故不忧。

发表评论

'); pageInit(); NProgress.done(); var url = window.location.href; if(url.indexOf("comment")>-1 ){ $('html,body').animate({scrollTop: $('#comments').offset().top /*+ Ch*/ }, 500); } if(typeof Blazy != 'undefined' && Blazy instanceof Function){ $(document).ready(function(){ var blazy = new Blazy(); }); } } }); InstantClick.on('wait', function(){ //点击链接但未加载 NProgress.start(); NProgress.inc(); }); // InstantClick.on('receive',function(url){ //页面已预加载 // url // body // title // }); // InstantClick.on('fetch',function(url){ //页面开始预加载 // }); InstantClick.init('mousedown'); } pageInit();