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

ImageUrlBuilder::waterText()   C

Complexity

Conditions 13
Paths 129

Size

Total Lines 65
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 36
c 1
b 0
f 0
nc 129
nop 9
dl 0
loc 65
rs 5.4525

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 ( 
78
            ! is_null($format) 
79
            && in_array($format, $this->formatArr)
80
        ) {
81
            $thumbStr .= 'format/' . $format . '/';
82
        }
83
84
        // 拼接渐进显示
85
        if ( 
86
            ! is_null($interlace) 
87
            && in_array(intval($interlace), [0, 1], true)
88
        ) {
89
            $thumbStr .= 'interlace/' . $interlace . '/';
90
        }
91
92
        // 拼接图片质量
93
        if ( 
94
            ! is_null($quality)
95
            && intval($quality) >= 0
96
            && intval($quality) <= 100
97
        ) {
98
            $thumbStr .= 'q/' . $quality . '/';
99
        }
100
101
        $thumbStr .= 'ignore-error/' . $ignoreError . '/';
102
103
        // 如果有query_string用|线分割实现多参数
104
        return $url . ($this->hasQuery($url) ? '|' : '?') . $thumbStr;
105
    }
106
107
    /**
108
     * 图片水印
109
     *
110
     * @param  string $url 图片链接
111
     * @param  string $image 水印图片链接
112
     * @param  numeric $dissolve 透明度
113
     * @param  string $gravity 水印位置
114
     * @param  numeric $dx 横轴边距
115
     * @param  numeric $dy 纵轴边距
116
     * @param  numeric $watermarkScale 自适应原图的短边比例
117
     * @link   http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html
118
     * @return string
119
     * @author Sherlock Ren <[email protected]>
120
     */
121
    public function waterImg(
122
        $url, 
123
        $image, 
124
        $dissolve = 100,
125
        $gravity = 'SouthEast', 
126
        $dx = null, 
127
        $dy = null,
128
        $watermarkScale = null
129
    ) {
130
        // url合法效验
131
        if ( ! $this->isUrl($url)) {
132
            return $url;
133
        }
134
135
        $waterStr = 'watermark/1/image/' . \Qiniu\base64_urlSafeEncode($image) . '/';
136
137
        // 拼接水印透明度
138
        if (
139
            is_numeric($dissolve)
140
            && $dissolve <= 100
141
        ) {
142
            $waterStr .= 'dissolve/' . $dissolve . '/';
143
        }       
144
145
        // 拼接水印位置
146
        if (in_array($gravity, $this->gravityArr, true)) {
147
            $waterStr .= 'gravity/' . $gravity . '/';
148
        }       
149
150
        // 拼接横轴边距
151
        if (
152
            ! is_null($dx)
153
            && is_numeric($dx)
154
        ) {
155
            $waterStr .= 'dx/' . $dx . '/';
156
        }
157
158
        // 拼接纵轴边距
159
        if (
160
            ! is_null($dy)
161
            && is_numeric($dy)
162
        ) {
163
            $waterStr .= 'dy/' . $dy . '/';
164
        }
165
166
        // 拼接自适应原图的短边比例
167
        if (
168
            ! is_null($watermarkScale)
169
            && is_numeric($watermarkScale)
170
            && $watermarkScale > 0
171
            && $watermarkScale < 1
172
        ) {
173
            $waterStr .= 'ws/' . $watermarkScale . '/';
174
        }
175
176
        // 如果有query_string用|线分割实现多参数
177
        return $url . ($this->hasQuery($url) ? '|' : '?') . $waterStr;
178
    }
179
180
    /**
181
     * 文字水印
182
     *
183
     * @param  string $url 图片链接
184
     * @param  string $text 文字
185
     * @param  string $font 文字字体
186
     * @param  string $fontSize 文字字号
187
     * @param  string $fontColor 文字颜色
188
     * @param  numeric $dissolve 透明度
189
     * @param  string $gravity 水印位置
190
     * @param  numeric $dx 横轴边距
191
     * @param  numeric $dy 纵轴边距
192
     * @link   http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html#text-watermark
193
     * @return string
194
     * @author Sherlock Ren <[email protected]>
195
     */
196
    public function waterText(
197
        $url, 
198
        $text, 
199
        $font = '黑体', 
200
        $fontSize = 0, 
201
        $fontColor = null, 
202
        $dissolve = 100,
203
        $gravity = 'SouthEast', 
204
        $dx = null, 
205
        $dy = null
206
    ) {
207
        // url合法效验
208
        if ( ! $this->isUrl($url)) {
209
            return $url;
210
        }
211
212
        $waterStr = 'watermark/2/text/' 
213
            . \Qiniu\base64_urlSafeEncode($text) . '/font/'
214
            . \Qiniu\base64_urlSafeEncode($font) . '/';
215
216
        // 拼接文字大小
217
        if (is_int($fontSize)) {
218
            $waterStr .= 'fontsize/' . $fontSize . '/';
219
        }
220
221
        // 拼接文字颜色
222
        if (
223
            ! is_null($fontColor)
224
            && $fontColor
225
        ) {
226
            $waterStr .= 'fill/' . \Qiniu\base64_urlSafeEncode($fontColor) . '/';
227
        }
228
229
        // 拼接水印透明度
230
        if (
231
            is_numeric($dissolve)
232
            && $dissolve <= 100
233
        ) {
234
            $waterStr .= 'dissolve/' . $dissolve . '/';
235
        }       
236
237
        // 拼接水印位置
238
        if (in_array($gravity, $this->gravityArr, true)) {
239
            $waterStr .= 'gravity/' . $gravity . '/';
240
        }       
241
242
        // 拼接横轴边距
243
        if (
244
            ! is_null($dx)
245
            && is_numeric($dx)
246
        ) {
247
            $waterStr .= 'dx/' . $dx . '/';
248
        }
249
250
        // 拼接纵轴边距
251
        if (
252
            ! is_null($dy)
253
            && is_numeric($dy)
254
        ) {
255
            $waterStr .= 'dy/' . $dy . '/';
256
        }
257
258
        // 如果有query_string用|线分割实现多参数
259
        return $url . ($this->hasQuery($url) ? '|' : '?') . $waterStr;
260
    }
261
262
    /**
263
     * 效验url合法性
264
     *
265
     * @param  string $url url链接
266
     * @return string
267
     * @author Sherlock Ren <[email protected]>
268
     */
269
    protected function isUrl($url)
270
    {
271
        $urlArr = parse_url($url);
272
273
        return $urlArr['scheme']
274
            && in_array($urlArr['scheme'], ['http', 'https'])
275
            && $urlArr['host']
276
            && $urlArr['path'];
277
    }
278
279
    /**
280
     * 检测是否有query
281
     *
282
     * @param  string $url url链接
283
     * @return string
284
     * @author Sherlock Ren <[email protected]>
285
     */
286
    protected function hasQuery($url)
287
    {
288
        $urlArr = parse_url($url);
289
290
        return ! empty($urlArr['query']);
291
    }
292
}
293