Completed
Pull Request — master (#188)
by
unknown
03:57
created

ImageUrlBuilder::waterText()   C

Complexity

Conditions 13
Paths 129

Size

Total Lines 61
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
eloc 32
c 2
b 0
f 0
nc 129
nop 9
dl 0
loc 61
rs 5.6886

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
namespace Qiniu\Processing;
3
4
use Qiniu;
5
6
/**
7
 * 主要涉及图片链接拼接
8
 *
9
 * @link http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html
10
 */
11
final class ImageUrlBuilder
12
{
13
    /**
14
     * mode合法范围值
15
     *
16
     * @var array
17
     */
18
    protected $modeArr = [0, 1, 2, 3, 4, 5];
19
20
    /**
21
     * format合法值
22
     *
23
     * @var array
24
     */
25
    protected $formatArr = ['psd', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'bmp'];
26
27
    /**
28
     * 水印图片位置合法值
29
     *
30
     * @var array
31
     */
32
    protected $gravityArr = ['NorthWest', 'North', 'NorthEast', 'West', 'Center', 'East', 'SouthWest', 'South', 'SouthEast'];
33
34
    /**
35
     * 缩略图链接拼接
36
     *
37
     * @param  string $url 图片链接
38
     * @param  int $mode 缩略模式
39
     * @param  int $width 宽度
40
     * @param  int $height 长度
41
     * @param  string $format 输出类型
42
     * @param  int $quality 图片质量
43
     * @param  int $interlace 是否支持渐进显示
44
     * @param  int $ignoreError 忽略结果
45
     * @return string
46
     * @link http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html
47
     * @author Sherlock Ren <[email protected]>
48
     */
49
    public function thumbnail(
50
        $url,
51
        $mode,
52
        $width,
53
        $height,
54
        $format = null,
55
        $interlace = null,
56
        $quality = null,
57
        $ignoreError = 1
58
    ) {
59
    
60
        // url合法效验
61
        if (! $this->isUrl($url)) {
62
            return $url;
63
        }
64
65
        // 参数合法性效验
66
        if (! in_array(intval($mode), $this->modeArr, true)) {
67
            return $url;
68
        }
69
70
        if (! $width || ! $height) {
71
            return $url;
72
        }
73
74
        $thumbStr = 'imageView2/' . $mode . '/w/' . $width . '/h/' . $height . '/';
75
76
        // 拼接输出格式
77
        if (! is_null($format)
78
            && in_array($format, $this->formatArr)
79
        ) {
80
            $thumbStr .= 'format/' . $format . '/';
81
        }
82
83
        // 拼接渐进显示
84
        if (! is_null($interlace)
85
            && in_array(intval($interlace), [0, 1], true)
86
        ) {
87
            $thumbStr .= 'interlace/' . $interlace . '/';
88
        }
89
90
        // 拼接图片质量
91
        if (! is_null($quality)
92
            && intval($quality) >= 0
93
            && intval($quality) <= 100
94
        ) {
95
            $thumbStr .= 'q/' . $quality . '/';
96
        }
97
98
        $thumbStr .= 'ignore-error/' . $ignoreError . '/';
99
100
        // 如果有query_string用|线分割实现多参数
101
        return $url . ($this->hasQuery($url) ? '|' : '?') . $thumbStr;
102
    }
103
104
    /**
105
     * 图片水印
106
     *
107
     * @param  string $url 图片链接
108
     * @param  string $image 水印图片链接
109
     * @param  numeric $dissolve 透明度
110
     * @param  string $gravity 水印位置
111
     * @param  numeric $dx 横轴边距
112
     * @param  numeric $dy 纵轴边距
113
     * @param  numeric $watermarkScale 自适应原图的短边比例
114
     * @link   http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html
115
     * @return string
116
     * @author Sherlock Ren <[email protected]>
117
     */
118
    public function waterImg(
119
        $url,
120
        $image,
121
        $dissolve = 100,
122
        $gravity = 'SouthEast',
123
        $dx = null,
124
        $dy = null,
125
        $watermarkScale = null
126
    ) {
127
        // url合法效验
128
        if (! $this->isUrl($url)) {
129
            return $url;
130
        }
131
132
        $waterStr = 'watermark/1/image/' . \Qiniu\base64_urlSafeEncode($image) . '/';
133
134
        // 拼接水印透明度
135
        if (is_numeric($dissolve)
136
            && $dissolve <= 100
137
        ) {
138
            $waterStr .= 'dissolve/' . $dissolve . '/';
139
        }
140
141
        // 拼接水印位置
142
        if (in_array($gravity, $this->gravityArr, true)) {
143
            $waterStr .= 'gravity/' . $gravity . '/';
144
        }
145
146
        // 拼接横轴边距
147
        if (! is_null($dx)
148
            && is_numeric($dx)
149
        ) {
150
            $waterStr .= 'dx/' . $dx . '/';
151
        }
152
153
        // 拼接纵轴边距
154
        if (! is_null($dy)
155
            && is_numeric($dy)
156
        ) {
157
            $waterStr .= 'dy/' . $dy . '/';
158
        }
159
160
        // 拼接自适应原图的短边比例
161
        if (! is_null($watermarkScale)
162
            && is_numeric($watermarkScale)
163
            && $watermarkScale > 0
164
            && $watermarkScale < 1
165
        ) {
166
            $waterStr .= 'ws/' . $watermarkScale . '/';
167
        }
168
169
        // 如果有query_string用|线分割实现多参数
170
        return $url . ($this->hasQuery($url) ? '|' : '?') . $waterStr;
171
    }
172
173
    /**
174
     * 文字水印
175
     *
176
     * @param  string $url 图片链接
177
     * @param  string $text 文字
178
     * @param  string $font 文字字体
179
     * @param  string $fontSize 文字字号
180
     * @param  string $fontColor 文字颜色
181
     * @param  numeric $dissolve 透明度
182
     * @param  string $gravity 水印位置
183
     * @param  numeric $dx 横轴边距
184
     * @param  numeric $dy 纵轴边距
185
     * @link   http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html#text-watermark
186
     * @return string
187
     * @author Sherlock Ren <[email protected]>
188
     */
189
    public function waterText(
190
        $url,
191
        $text,
192
        $font = '黑体',
193
        $fontSize = 0,
194
        $fontColor = null,
195
        $dissolve = 100,
196
        $gravity = 'SouthEast',
197
        $dx = null,
198
        $dy = null
199
    ) {
200
        // url合法效验
201
        if (! $this->isUrl($url)) {
202
            return $url;
203
        }
204
205
        $waterStr = 'watermark/2/text/'
206
            . \Qiniu\base64_urlSafeEncode($text) . '/font/'
207
            . \Qiniu\base64_urlSafeEncode($font) . '/';
208
209
        // 拼接文字大小
210
        if (is_int($fontSize)) {
211
            $waterStr .= 'fontsize/' . $fontSize . '/';
212
        }
213
214
        // 拼接文字颜色
215
        if (! is_null($fontColor)
216
            && $fontColor
217
        ) {
218
            $waterStr .= 'fill/' . \Qiniu\base64_urlSafeEncode($fontColor) . '/';
219
        }
220
221
        // 拼接水印透明度
222
        if (is_numeric($dissolve)
223
            && $dissolve <= 100
224
        ) {
225
            $waterStr .= 'dissolve/' . $dissolve . '/';
226
        }
227
228
        // 拼接水印位置
229
        if (in_array($gravity, $this->gravityArr, true)) {
230
            $waterStr .= 'gravity/' . $gravity . '/';
231
        }
232
233
        // 拼接横轴边距
234
        if (! is_null($dx)
235
            && is_numeric($dx)
236
        ) {
237
            $waterStr .= 'dx/' . $dx . '/';
238
        }
239
240
        // 拼接纵轴边距
241
        if (! is_null($dy)
242
            && is_numeric($dy)
243
        ) {
244
            $waterStr .= 'dy/' . $dy . '/';
245
        }
246
247
        // 如果有query_string用|线分割实现多参数
248
        return $url . ($this->hasQuery($url) ? '|' : '?') . $waterStr;
249
    }
250
251
    /**
252
     * 效验url合法性
253
     *
254
     * @param  string $url url链接
255
     * @return string
256
     * @author Sherlock Ren <[email protected]>
257
     */
258
    protected function isUrl($url)
259
    {
260
        $urlArr = parse_url($url);
261
262
        return $urlArr['scheme']
263
            && in_array($urlArr['scheme'], ['http', 'https'])
264
            && $urlArr['host']
265
            && $urlArr['path'];
266
    }
267
268
    /**
269
     * 检测是否有query
270
     *
271
     * @param  string $url url链接
272
     * @return string
273
     * @author Sherlock Ren <[email protected]>
274
     */
275
    protected function hasQuery($url)
276
    {
277
        $urlArr = parse_url($url);
278
279
        return ! empty($urlArr['query']);
280
    }
281
}
282