1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @description 验证码图片类 |
4
|
|
|
* @author vicens <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Vicens\Captcha; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class Image |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* 已生成的图片 |
17
|
|
|
* |
18
|
|
|
* @var resource |
19
|
|
|
*/ |
20
|
|
|
protected $image; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
public function __construct($image) |
24
|
|
|
{ |
25
|
|
|
$this->image = $image; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* 获取 base64的数据,用做image标签的src |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getDataUrl() |
34
|
|
|
{ |
35
|
|
|
return 'data:image/jpeg;base64,' . $this->getBase64(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 获取base64 |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getBase64() |
44
|
|
|
{ |
45
|
|
|
return base64_encode($this->getContent()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* 作为img标签输出 |
50
|
|
|
* |
51
|
|
|
* @param int|null $width img宽 |
52
|
|
|
* @param int|null $height img高 |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function html($width = null, $height = null) |
56
|
|
|
{ |
57
|
|
|
$html = '<img src="' . $this->getDataUrl() . '"" alt="captcha"'; |
58
|
|
|
if (is_null($width)) { |
59
|
|
|
$html .= ' width="' . $width . '"'; |
60
|
|
|
} |
61
|
|
|
if (is_null($height)) { |
62
|
|
|
$html .= ' height="' . $height . '"'; |
63
|
|
|
} |
64
|
|
|
$html .= '>'; |
65
|
|
|
return $html; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* 返回Response响应 |
70
|
|
|
* |
71
|
|
|
* @return Response |
72
|
|
|
*/ |
73
|
|
|
public function response() |
74
|
|
|
{ |
75
|
|
|
|
76
|
|
|
return Response::create($this->getContent(), 200, array('Content-type' => 'image/jpeg')); |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* 获取图片 |
82
|
|
|
* |
83
|
|
|
* @return resource |
84
|
|
|
*/ |
85
|
|
|
public function getImage() |
86
|
|
|
{ |
87
|
|
|
|
88
|
|
|
return $this->image; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* 获取图片 |
93
|
|
|
* |
94
|
|
|
* @return resource |
95
|
|
|
*/ |
96
|
|
|
public function getContext() |
97
|
|
|
{ |
98
|
|
|
return $this->getImage(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* 保存为图片 |
103
|
|
|
* |
104
|
|
|
* @param string $filename 文件名 |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function save($filename) |
108
|
|
|
{ |
109
|
|
|
$this->output($filename); |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* 直接输出 |
116
|
|
|
* |
117
|
|
|
* @param string|null $filename 文件名 |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function output($filename = null) |
121
|
|
|
{ |
122
|
|
|
imagejpeg($this->getContext(), $filename); |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* 获取输出内容 |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getContent() |
133
|
|
|
{ |
134
|
|
|
ob_start(); |
135
|
|
|
$this->output(); |
136
|
|
|
return ob_get_clean(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |