Passed
Push — master ( a2ba4c...af1bf0 )
by Vicens
03:10
created

Image   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 128
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDataUrl() 0 4 1
A getBase64() 0 4 1
A html() 0 12 3
A response() 0 6 1
A getImage() 0 5 1
A getContext() 0 4 1
A save() 0 6 1
A output() 0 6 1
A getContent() 0 6 1
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
}