ImageUrlBuilder::isUrl()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4

Importance

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