First commit
This commit is contained in:
282
app/Common/extend/qiniu/src/Qiniu/Processing/ImageUrlBuilder.php
Executable file
282
app/Common/extend/qiniu/src/Qiniu/Processing/ImageUrlBuilder.php
Executable file
@@ -0,0 +1,282 @@
|
||||
<?php
|
||||
namespace Qiniu\Processing;
|
||||
|
||||
use Qiniu;
|
||||
|
||||
/**
|
||||
* 主要涉及图片链接拼接
|
||||
*
|
||||
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html
|
||||
*/
|
||||
final class ImageUrlBuilder
|
||||
{
|
||||
/**
|
||||
* mode合法范围值
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $modeArr = array(0, 1, 2, 3, 4, 5);
|
||||
|
||||
/**
|
||||
* format合法值
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $formatArr = array('psd', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'bmp');
|
||||
|
||||
/**
|
||||
* 水印图片位置合法值
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $gravityArr = array('NorthWest', 'North', 'NorthEast',
|
||||
'West', 'Center', 'East', 'SouthWest', 'South', 'SouthEast');
|
||||
|
||||
/**
|
||||
* 缩略图链接拼接
|
||||
*
|
||||
* @param string $url 图片链接
|
||||
* @param int $mode 缩略模式
|
||||
* @param int $width 宽度
|
||||
* @param int $height 长度
|
||||
* @param string $format 输出类型
|
||||
* @param int $quality 图片质量
|
||||
* @param int $interlace 是否支持渐进显示
|
||||
* @param int $ignoreError 忽略结果
|
||||
* @return string
|
||||
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html
|
||||
* @author Sherlock Ren <sherlock_ren@icloud.com>
|
||||
*/
|
||||
public function thumbnail(
|
||||
$url,
|
||||
$mode,
|
||||
$width,
|
||||
$height,
|
||||
$format = null,
|
||||
$interlace = null,
|
||||
$quality = null,
|
||||
$ignoreError = 1
|
||||
) {
|
||||
|
||||
// url合法效验
|
||||
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 . '/';
|
||||
|
||||
// 如果有query_string用|线分割实现多参数
|
||||
return $url . ($this->hasQuery($url) ? '|' : '?') . $thumbStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片水印
|
||||
*
|
||||
* @param string $url 图片链接
|
||||
* @param string $image 水印图片链接
|
||||
* @param numeric $dissolve 透明度
|
||||
* @param string $gravity 水印位置
|
||||
* @param numeric $dx 横轴边距
|
||||
* @param numeric $dy 纵轴边距
|
||||
* @param numeric $watermarkScale 自适应原图的短边比例
|
||||
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html
|
||||
* @return string
|
||||
* @author Sherlock Ren <sherlock_ren@icloud.com>
|
||||
*/
|
||||
public function waterImg(
|
||||
$url,
|
||||
$image,
|
||||
$dissolve = 100,
|
||||
$gravity = 'SouthEast',
|
||||
$dx = null,
|
||||
$dy = null,
|
||||
$watermarkScale = null
|
||||
) {
|
||||
// url合法效验
|
||||
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 . '/';
|
||||
}
|
||||
|
||||
// 如果有query_string用|线分割实现多参数
|
||||
return $url . ($this->hasQuery($url) ? '|' : '?') . $waterStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文字水印
|
||||
*
|
||||
* @param string $url 图片链接
|
||||
* @param string $text 文字
|
||||
* @param string $font 文字字体
|
||||
* @param string $fontSize 文字字号
|
||||
* @param string $fontColor 文字颜色
|
||||
* @param numeric $dissolve 透明度
|
||||
* @param string $gravity 水印位置
|
||||
* @param numeric $dx 横轴边距
|
||||
* @param numeric $dy 纵轴边距
|
||||
* @link http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html#text-watermark
|
||||
* @return string
|
||||
* @author Sherlock Ren <sherlock_ren@icloud.com>
|
||||
*/
|
||||
public function waterText(
|
||||
$url,
|
||||
$text,
|
||||
$font = '黑体',
|
||||
$fontSize = 0,
|
||||
$fontColor = null,
|
||||
$dissolve = 100,
|
||||
$gravity = 'SouthEast',
|
||||
$dx = null,
|
||||
$dy = null
|
||||
) {
|
||||
// url合法效验
|
||||
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 . '/';
|
||||
}
|
||||
|
||||
// 如果有query_string用|线分割实现多参数
|
||||
return $url . ($this->hasQuery($url) ? '|' : '?') . $waterStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 效验url合法性
|
||||
*
|
||||
* @param string $url url链接
|
||||
* @return string
|
||||
* @author Sherlock Ren <sherlock_ren@icloud.com>
|
||||
*/
|
||||
protected function isUrl($url)
|
||||
{
|
||||
$urlArr = parse_url($url);
|
||||
|
||||
return $urlArr['scheme']
|
||||
&& in_array($urlArr['scheme'], array('http', 'https'))
|
||||
&& $urlArr['host']
|
||||
&& $urlArr['path'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测是否有query
|
||||
*
|
||||
* @param string $url url链接
|
||||
* @return string
|
||||
* @author Sherlock Ren <sherlock_ren@icloud.com>
|
||||
*/
|
||||
protected function hasQuery($url)
|
||||
{
|
||||
$urlArr = parse_url($url);
|
||||
|
||||
return !empty($urlArr['query']);
|
||||
}
|
||||
}
|
||||
60
app/Common/extend/qiniu/src/Qiniu/Processing/Operation.php
Executable file
60
app/Common/extend/qiniu/src/Qiniu/Processing/Operation.php
Executable file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Qiniu\Processing;
|
||||
|
||||
use Qiniu\Http\Client;
|
||||
use Qiniu\Http\Error;
|
||||
|
||||
final class Operation
|
||||
{
|
||||
|
||||
private $auth;
|
||||
private $token_expire;
|
||||
private $domain;
|
||||
|
||||
public function __construct($domain, $auth = null, $token_expire = 3600)
|
||||
{
|
||||
$this->auth = $auth;
|
||||
$this->domain = $domain;
|
||||
$this->token_expire = $token_expire;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 对资源文件进行处理
|
||||
*
|
||||
* @param $key 待处理的资源文件名
|
||||
* @param $fops string|array fop操作,多次fop操作以array的形式传入。
|
||||
* eg. imageView2/1/w/200/h/200, imageMogr2/thumbnail/!75px
|
||||
*
|
||||
* @return array 文件处理后的结果及错误。
|
||||
*
|
||||
* @link http://developer.qiniu.com/docs/v6/api/reference/fop/
|
||||
*/
|
||||
public function execute($key, $fops)
|
||||
{
|
||||
$url = $this->buildUrl($key, $fops);
|
||||
$resp = Client::get($url);
|
||||
if (!$resp->ok()) {
|
||||
return array(null, new Error($url, $resp));
|
||||
}
|
||||
if ($resp->json() !== null) {
|
||||
return array($resp->json(), null);
|
||||
}
|
||||
return array($resp->body, null);
|
||||
}
|
||||
|
||||
public function buildUrl($key, $fops, $protocol = 'http')
|
||||
{
|
||||
if (is_array($fops)) {
|
||||
$fops = implode('|', $fops);
|
||||
}
|
||||
|
||||
$url = $protocol . "://$this->domain/$key?$fops";
|
||||
if ($this->auth !== null) {
|
||||
$url = $this->auth->privateDownloadUrl($url, $this->token_expire);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
94
app/Common/extend/qiniu/src/Qiniu/Processing/PersistentFop.php
Executable file
94
app/Common/extend/qiniu/src/Qiniu/Processing/PersistentFop.php
Executable file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
namespace Qiniu\Processing;
|
||||
|
||||
use Qiniu\Config;
|
||||
use Qiniu\Http\Client;
|
||||
use Qiniu\Http\Error;
|
||||
use Qiniu\Processing\Operation;
|
||||
|
||||
/**
|
||||
* 持久化处理类,该类用于主动触发异步持久化操作.
|
||||
*
|
||||
* @link http://developer.qiniu.com/docs/v6/api/reference/fop/pfop/pfop.html
|
||||
*/
|
||||
final class PersistentFop
|
||||
{
|
||||
/**
|
||||
* @var 账号管理密钥对,Auth对象
|
||||
*/
|
||||
private $auth;
|
||||
|
||||
/*
|
||||
* @var 配置对象,Config 对象
|
||||
* */
|
||||
private $config;
|
||||
|
||||
|
||||
public function __construct($auth, $config = null)
|
||||
{
|
||||
$this->auth = $auth;
|
||||
if ($config == null) {
|
||||
$this->config = new Config();
|
||||
} else {
|
||||
$this->config = $config;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对资源文件进行异步持久化处理
|
||||
* @param $bucket 资源所在空间
|
||||
* @param $key 待处理的源文件
|
||||
* @param $fops string|array 待处理的pfop操作,多个pfop操作以array的形式传入。
|
||||
* eg. avthumb/mp3/ab/192k, vframe/jpg/offset/7/w/480/h/360
|
||||
* @param $pipeline 资源处理队列
|
||||
* @param $notify_url 处理结果通知地址
|
||||
* @param $force 是否强制执行一次新的指令
|
||||
*
|
||||
*
|
||||
* @return array 返回持久化处理的persistentId, 和返回的错误。
|
||||
*
|
||||
* @link http://developer.qiniu.com/docs/v6/api/reference/fop/
|
||||
*/
|
||||
public function execute($bucket, $key, $fops, $pipeline = null, $notify_url = null, $force = false)
|
||||
{
|
||||
if (is_array($fops)) {
|
||||
$fops = implode(';', $fops);
|
||||
}
|
||||
$params = array('bucket' => $bucket, 'key' => $key, 'fops' => $fops);
|
||||
\Qiniu\setWithoutEmpty($params, 'pipeline', $pipeline);
|
||||
\Qiniu\setWithoutEmpty($params, 'notifyURL', $notify_url);
|
||||
if ($force) {
|
||||
$params['force'] = 1;
|
||||
}
|
||||
$data = http_build_query($params);
|
||||
$scheme = "http://";
|
||||
if ($this->config->useHTTPS === true) {
|
||||
$scheme = "https://";
|
||||
}
|
||||
$url = $scheme . Config::API_HOST . '/pfop/';
|
||||
$headers = $this->auth->authorization($url, $data, 'application/x-www-form-urlencoded');
|
||||
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
$response = Client::post($url, $data, $headers);
|
||||
if (!$response->ok()) {
|
||||
return array(null, new Error($url, $response));
|
||||
}
|
||||
$r = $response->json();
|
||||
$id = $r['persistentId'];
|
||||
return array($id, null);
|
||||
}
|
||||
|
||||
public function status($id)
|
||||
{
|
||||
$scheme = "http://";
|
||||
|
||||
if ($this->config->useHTTPS === true) {
|
||||
$scheme = "https://";
|
||||
}
|
||||
$url = $scheme . Config::API_HOST . "/status/get/prefop?id=$id";
|
||||
$response = Client::get($url);
|
||||
if (!$response->ok()) {
|
||||
return array(null, new Error($url, $response));
|
||||
}
|
||||
return array($response->json(), null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user