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);
/\\\\*\\\\* 設(shè)置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);
/\\\\*\\\\*跳轉(zhuǎn)也要 \\\\*/
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" />
相關(guān)推薦:編程視頻課程
更多關(guān)于云服務(wù)器,域名注冊,虛擬主機的問題,請訪問西部數(shù)碼官網(wǎng):m.ps-sw.cn