欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」php破解图床防盗链方法

作者 : 自学哈 本文共2985个字,预计阅读时间需要8分钟 2022-10-8 共160人阅读
也想出现在这里? 联系我们

php代码

命名为dl.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"));

使用方法

http://域名/dl.php?url=图床链接

示例

https://www.zixueha.com/wp-content/uploads/2022/10/1665238813-e381ad4d46cd068.jpg

加上反向代理,破解防盗链处理

http://域名/dl.php?url=https://www.zixueha.com/wp-content/uploads/2022/10/1665238813-e381ad4d46cd068.jpg

HTML格式

<img src="http://域名/dl.php?url=https://www.zixueha.com/wp-content/uploads/2022/10/1665238813-e381ad4d46cd068.jpg"/>
本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」php破解图床防盗链方法
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号