123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
- namespace Qiniu\Processing;
- use Qiniu;
- final class ImageUrlBuilder
- {
-
- protected $modeArr = array(0, 1, 2, 3, 4, 5);
-
- protected $formatArr = array('psd', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'bmp');
-
- protected $gravityArr = array('NorthWest', 'North', 'NorthEast',
- 'West', 'Center', 'East', 'SouthWest', 'South', 'SouthEast');
-
- public function thumbnail(
- $url,
- $mode,
- $width,
- $height,
- $format = null,
- $interlace = null,
- $quality = null,
- $ignoreError = 1
- ) {
-
- if (!$this->isUrl($url)) {
- return $url;
- }
-
- if (!in_array(intval($mode), $this->modeArr, true)) {
- return $url;
- }
- if (!$width || !$height) {
- return $url;
- }
- $thumbStr = 'imageView2/' . $mode . '/w/' . $width . '/h/' . $height . '/';
-
- if (!is_null($format)
- && in_array($format, $this->formatArr)
- ) {
- $thumbStr .= 'format/' . $format . '/';
- }
-
- if (!is_null($interlace)
- && in_array(intval($interlace), array(0, 1), true)
- ) {
- $thumbStr .= 'interlace/' . $interlace . '/';
- }
-
- if (!is_null($quality)
- && intval($quality) >= 0
- && intval($quality) <= 100
- ) {
- $thumbStr .= 'q/' . $quality . '/';
- }
- $thumbStr .= 'ignore-error/' . $ignoreError . '/';
-
- return $url . ($this->hasQuery($url) ? '|' : '?') . $thumbStr;
- }
-
- public function waterImg(
- $url,
- $image,
- $dissolve = 100,
- $gravity = 'SouthEast',
- $dx = null,
- $dy = null,
- $watermarkScale = null
- ) {
-
- if (!$this->isUrl($url)) {
- return $url;
- }
- $waterStr = 'watermark/1/image/' . \Qiniu\base64_urlSafeEncode($image) . '/';
-
- if (is_numeric($dissolve)
- && $dissolve <= 100
- ) {
- $waterStr .= 'dissolve/' . $dissolve . '/';
- }
-
- if (in_array($gravity, $this->gravityArr, true)) {
- $waterStr .= 'gravity/' . $gravity . '/';
- }
-
- if (!is_null($dx)
- && is_numeric($dx)
- ) {
- $waterStr .= 'dx/' . $dx . '/';
- }
-
- if (!is_null($dy)
- && is_numeric($dy)
- ) {
- $waterStr .= 'dy/' . $dy . '/';
- }
-
- if (!is_null($watermarkScale)
- && is_numeric($watermarkScale)
- && $watermarkScale > 0
- && $watermarkScale < 1
- ) {
- $waterStr .= 'ws/' . $watermarkScale . '/';
- }
-
- return $url . ($this->hasQuery($url) ? '|' : '?') . $waterStr;
- }
-
- public function waterText(
- $url,
- $text,
- $font = '黑体',
- $fontSize = 0,
- $fontColor = null,
- $dissolve = 100,
- $gravity = 'SouthEast',
- $dx = null,
- $dy = null
- ) {
-
- if (!$this->isUrl($url)) {
- return $url;
- }
- $waterStr = 'watermark/2/text/'
- . \Qiniu\base64_urlSafeEncode($text) . '/font/'
- . \Qiniu\base64_urlSafeEncode($font) . '/';
-
- if (is_int($fontSize)) {
- $waterStr .= 'fontsize/' . $fontSize . '/';
- }
-
- if (!is_null($fontColor)
- && $fontColor
- ) {
- $waterStr .= 'fill/' . \Qiniu\base64_urlSafeEncode($fontColor) . '/';
- }
-
- if (is_numeric($dissolve)
- && $dissolve <= 100
- ) {
- $waterStr .= 'dissolve/' . $dissolve . '/';
- }
-
- if (in_array($gravity, $this->gravityArr, true)) {
- $waterStr .= 'gravity/' . $gravity . '/';
- }
-
- if (!is_null($dx)
- && is_numeric($dx)
- ) {
- $waterStr .= 'dx/' . $dx . '/';
- }
-
- if (!is_null($dy)
- && is_numeric($dy)
- ) {
- $waterStr .= 'dy/' . $dy . '/';
- }
-
- return $url . ($this->hasQuery($url) ? '|' : '?') . $waterStr;
- }
-
- protected function isUrl($url)
- {
- $urlArr = parse_url($url);
- return $urlArr['scheme']
- && in_array($urlArr['scheme'], array('http', 'https'))
- && $urlArr['host']
- && $urlArr['path'];
- }
-
- protected function hasQuery($url)
- {
- $urlArr = parse_url($url);
- return !empty($urlArr['query']);
- }
- }
|