网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

图片防盗链破解处理 用反向代理解决图片防盗链问题

GG网络技术分享 2025-03-18 16:19 8


采集的图片太多了,自己的空间放不下,怎么办,

有的网站图片开启了防盗链,用referrerpolicy="no-referrer" 也不显示,那就用php处理,记得加rel="nofollow"属性。

根目录建一个php文件,比如daili.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://域名/daili.php?url=原始图片地址,即可。

进阶:

访问 /upload01/2024-8/f88f5249.jpg,跳转到访问 dl.php?url=http://img.xxx.com/upload/2024-8/f88f5249.jpg,跳转规则如下:

location ~* ^/uploads/(.*\.(jpg|jpeg|png|gif|bmp|webp|tiff))$ {

rewrite ^/uploads/(.*\.(jpg|jpeg|png|gif|bmp|webp|tiff))$ /dl.php?url=http://pic.2265.com/upload/$1 redirect;

}

注:必须有后缀,否则跳转不成功;另外特别注意,原始的文件夹是否真的存在upload01,并且里面储存了图片,这样就不能使用upload01,要换一个虚拟的文件夹名。

 

代理二:

<?php

function fetch_image($url) {

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HEADER, 1);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// Set User-Agent and other headers without Referer

$headers = [

'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',

'Accept: image/webp,image/apng,image/*,*/*;q=0.8',

'Accept-Language: en-US,en;q=0.9',

'Cache-Control: no-cache',

'Origin: http://www.hhxiazai.com'

];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);

$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);

$headers = substr($response, 0, $header_size);

$body = substr($response, $header_size);

curl_close($ch);

return [$http_code, $headers, $body];

}

$url = $_GET['url'];

if (filter_var($url, FILTER_VALIDATE_URL)) {

list($http_code, $headers, $image) = fetch_image($url);

if ($http_code == 200) {

preg_match('/Content-Type:\s*(.*?)(\r\n|\r|\n)/i', $headers, $matches);

$content_type = isset($matches[1]) ? trim($matches[1]) : "image/png";

header("Content-Type: $content_type");

echo $image;

} else {

echo "Image not found or unable to fetch. HTTP Code: $http_code\n";

echo "Response headers:\n";

echo $headers;

}

} else {

echo "Invalid URL";

}

?>

其他代理备用:

http://collect34.longsunhd.com/source/plugin/yzs1013_pldr/getimg.php?url=

https://images.weserv.nl/?url=

https://img.noobzone.ru/getimg.php?url=

https://i0.wp.com/

https://i1.wp.com/

https://i2.wp.com/

https://i3.wp.com/

https://cdn.cdnjson.com/

https://cors.zme.ink/

https://www.codenong.com/getimg_baidu.php?url=

https://image.baidu.com/search/down?url=

标签: 也不 转到

提交需求或反馈

Demand feedback