-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathimgBridge2019.php
114 lines (105 loc) · 3.8 KB
/
imgBridge2019.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* 何德何能,这个项目居然这么多朋友关注
* 由于之前的方法是N年前的了,今天我重新编写了本代码,对原来的方法进行了一些改进,如下:
* 1.增加对gif的支持
* 2.对源图片的显示
* 3.非常强悍,哪怕url有跳转,也能正常显示最终的图片
* 4.gif不支持水印,其他格式没问题
* 5.支持https了
* 6.对有错的、不存在的图片,进行友好的提示
* by 44886.com 2019-08-26
*/
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"));