1: Download Qiniu Cloud SDK
composer require qiniu/php-sdk
Two: php implements Qiniu cloud demo
<?php
namespace common\helpers;
use common\models\Config;
use common\models\VideoApiLog;
use Qiniu\Auth;
use Qiniu\Processing\PersistentFop;
use Qiniu\Storage\UploadManager;
use Yii;
use yii\helpers\ArrayHelper;
use common\helpers\Universal;
use yii\helpers\FileHelper;
use yii\httpclient\Client;
use yii\web\ServerErrorHttpException;
use function Qiniu\base64_urlSafeEncode;
/**
* 七牛云处理
*
* @author wangjian
* @since 0.1
*/
class QiNiu
{
/**
* @var string accessKey
*/
public $accessKey;
/**
* @var string accessKey
*/
public $secretKey;
/**
* @var string bucket
*/
public $bucket;
/**
* @var string url
*/
public $url;
/**
* @var string pipeline
*/
public $pipeline;
//进程状态
const STATUS_SUCCESS = 0;//成功
const STATUS_WAIT = 1;//等待
const STATUS_GO = 2;//执行中
const STATUS_ERROR = 3;//失败
/**
* 初始化
*/
public function __construct()
{
$this->accessKey = "";
$this->secretKey = "";
$this->bucket = "";
$this->url = "";
$this->pipeline = "";
}
/**
* 获取上传token
*/
public function getToken()
{
$auth = new Auth($this->accessKey, $this->secretKey);
// 生成上传Token
$token = $auth->uploadToken($this->bucket);
return $token;
}
/**
* 上传
*/
public function upload($file, $saveFileName = null)
{
$token = $this->getToken();
$uploadMgr = new UploadManager();
if ($saveFileName === null) {
$pathinfo = is_array($file) ? pathinfo($file['name']) : $file;
$extension = $pathinfo['extension'];
$saveFileName = Yii::$app->getSecurity()->generateRandomString() . '-' . date('His',time()) .".".$extension;
}
//上传到七牛云后 保存的文件名
$tmpName = is_array($file) ? $file['tmp_name'] : $file;
list($result, $err) = $uploadMgr->putFile($token,$saveFileName,$tmpName);
if ($err !== null) {
return false;
}
return [
'file' => $saveFileName,
'url' => $this->url . $saveFileName,
];
}
/**
* 获取已上传音视频元信息
*/
public function getAvinfo($file)
{
$client = new Client();
$response = $client->createRequest()
->setMethod('post')
->setUrl($this->url . $file . '?avinfo') // 请求地址
->send();
if ($response->isOk) {
return $response->data;
}
return [];
}
/**
* 获取进程处理状态
*/
public function persistentStatus($persistentId)
{
$auth = new Auth($this->accessKey, $this->secretKey);
$config = new \Qiniu\Config();
$pfop = new PersistentFop($auth, $config);
list($result, $error) = $pfop->status($persistentId);
if ($error != null)
{
return self::STATUS_ERROR;
}
$code = isset($result['code']) ? $result['code'] : 3;
return $code;
}
/**
* 获取视频指定帧图片
*/
public function getVideoImage($file, $saveFile = null, $offset = 1)
{
$auth = new Auth($this->accessKey, $this->secretKey);
$config = new \Qiniu\Config();
$pfop = new PersistentFop($auth, $config);
if ($saveFile == null) {
$fileName = basename($file);
$pathinfo = pathinfo($fileName);
$saveFile = $pathinfo['filename'] . '.jpg';
}
$entry = base64_urlSafeEncode("$this->bucket:$saveFile");
$fops = "vframe/jpg/offset/". $offset ."|saveas/$entry";
$pipeline = null;
if ($this->pipeline) {
$pipelineArr = explode(',', $this->pipeline);
$pipeline = $pipelineArr[array_rand($pipelineArr,1)];
}
list($id, $err) = $pfop->execute($this->bucket, $file, $fops, $pipeline);
if ($err != null)
{
return false;
}
//查询进程状态
$persistentStatus = false;
$code = self::STATUS_ERROR;
while ($persistentStatus === false) {
sleep(1);
$code = $this->persistentStatus($id);
if (in_array($code,[self::STATUS_SUCCESS, self::STATUS_ERROR])) {
$persistentStatus = true;
}
}
if ($code == self::STATUS_SUCCESS) {
return [
'url' => $this->url . $saveFile,
'file' => $saveFile
];
}
return false;
}
/**
* 视频处理
*/
public function avthumb($file, $saveFileName = null, $param = [])
{
if (empty($param)) {
return false;
}
$auth = new Auth($this->accessKey, $this->secretKey);
$config = new \Qiniu\Config();
$pfop = new PersistentFop($auth, $config);
$pathinfo = pathinfo($file);
$extension = strtolower($pathinfo['extension']);
if ($saveFileName === null) {
$saveFileName = Yii::$app->getSecurity()->generateRandomString() . '-' . date('His',time()) .".".$extension;
}
$entry = base64_urlSafeEncode("$this->bucket:$saveFileName");
$fops = "avthumb/" . $extension;
//裁剪
$star = isset($param['star']) ? $param['star'] : 0;
$end = isset($param['end']) ? $param['end'] : 0;
$star = intval($star);
$end = intval($end);
if (!((empty($star) && empty($end)) || ($star >= $end))) {
//需要裁剪
$duration = $end - $star;
if (!empty($duration)) {
$fops .= "/ss/". $star ."/t/" . $duration;/*截取片段*/
}
}
//文字水印
$wmText = isset($param['wmText']) ? $param['wmText'] : '';
if ($wmText) {
$fops .= "/wmText/". base64_urlSafeEncode($wmText);
//透明度
$wmAlpha = isset($param['wmAlpha']) ? $param['wmAlpha'] : '1.0';
$fops .= "/wmAlpha/". $wmAlpha;
//字体
$wmFont = isset($param['wmFont']) ? $param['wmFont'] : '宋体';
$wmFont = base64_urlSafeEncode($wmFont);
$fops .= "/wmFont/". $wmFont;
//水印颜色
$wmFontColor = isset($param['wmFontColor']) ? $param['wmFontColor'] : '#ffffff';
$wmFontColor = base64_urlSafeEncode($wmFontColor);
$fops .= "/wmFontColor/". $wmFontColor;
//水印文字大小
$wmFontSize = isset($param['wmFontSize']) ? $param['wmFontSize'] : '30';
$fops .= "/wmFontSize/". $wmFontSize;
//水印文字位置
$wmGravityText = isset($param['wmGravityText']) ? $param['wmGravityText'] : 'SouthWest';//方位
$wmOffsetX = isset($param['wmOffsetX']) ? $param['wmOffsetX'] : '0';//X偏移量
$wmOffsetY = isset($param['wmOffsetY']) ? $param['wmOffsetY'] : '0';//Y偏移量
$fops .= "/wmGravityText/". $wmGravityText . "/wmOffsetX/" . $wmOffsetX . "/wmOffsetY/" . $wmOffsetY;
}
$audio = isset($param['audio']) ? $param['audio'] : '';
//音频
if ($audio) {
$fops .= "/multiArep/" . base64_urlSafeEncode('kodo://'. $this->bucket .'/' . $audio);
}
$fops .= "|saveas/$entry";
$pipeline = null;
if ($this->pipeline) {
$pipelineArr = explode(',', $this->pipeline);
$pipeline = $pipelineArr[array_rand($pipelineArr,1)];
}
list($persistentId, $err) = $pfop->execute($this->bucket, $file, $fops, $pipeline);
if ($err != null)
{
return false;
}
//查询进程状态
$persistentStatus = false;
$code = self::STATUS_ERROR;
while ($persistentStatus === false) {
sleep(1);
$code = $this->persistentStatus($persistentId);
if (in_array($code,[self::STATUS_SUCCESS, self::STATUS_ERROR])) {
$persistentStatus = true;
}
}
if ($code == self::STATUS_SUCCESS) {
return [
'file' => $saveFileName,
'url' => $this->url . $saveFileName,
'duration' => $duration,
];
}
return false;
}
}
Replace the accessKey, secretKey, bucket, url, pipeline parameters with the information of your own Qiniu Cloud account
Three: simple implementation
1: Realize upload
$qiniu = new QiNiu();
$qiniu->upload($_FILES['file']);
2: Get the uploaded audio and video information
$file = '';//上传七牛云后文件名
$qiniu = new QiNiu();
$qiniu->getAvinfo($file);
3: Get the specified frame picture of the video
$file = '';//上传七牛云后视频名称
$qiniu = new QiNiu();
$qiniu->getVideoImage($file);//其他参数参考demo
4: Video processing (cropping, text watermarking, adding audio)
$file = '';//上传七牛云后视频名称
$qiniu = new QiNiu();
$qiniu->avthumb($file);//其他参数参考demo
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。