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