Completed
Pull Request — master (#188)
by
unknown
21:37
created

ImageUrlBuilder::thumbnail()   C

Complexity

Conditions 13
Paths 19

Size

Total Lines 54
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
eloc 28
c 2
b 0
f 0
nc 19
nop 8
dl 0
loc 54
rs 6.7593

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