上代碼之前,我想吐槽一下七牛雲的DEMO, 寫的確實不咋地。
廢話不多說,咱們開始吧。
項目框架TP5.1 , 之前別人寫了一點,懶得改七牛雲的SDK目錄,強迫症患者將就着看看吧,
qiniu的phpSDK 放在了vendor文件夾下。
1. 持久化處理上傳及生成縮略圖。 由於上傳的圖片較大 20M + , 所以採用了持久化處理上傳,putfile 20M的圖片是不讓你上傳的。
newFilename 就是保存在七牛雲上的原圖key,
oldFilePath 服務器上圖片的地址,服務器緩存中的地址
persistentops : 對圖片進行的操作,如果有多個操作則用 , 隔開。
persistentNotifyUrl 是異步返回的通知地址, 在這個方法中我們要進行的操作就是保存縮略圖到服務器並刪除七牛雲上的縮略圖。
完成的操作:設置了原圖的key, 將原圖保存後,生成 200*200的縮略圖保存在七牛雲。
private function uploadImgPost($newFilename, $oldFilepath){
set_time_limit(0);
$t1 = microtime(true);
require_once APP_PATH . '/../vendor/qiniu/autoload.php';
// 構建鑑權對象
$auth = new Auth($this->accessKey, $this->secretKey);
//生成保存文件的key
$explode = explode(".", $newFilename);
$thumbKey = $explode[0] . "_thumb" .".". $explode[1];
$entry = $this->bucket .":". $thumbKey;
$encodedEntryURI = \Qiniu\base64_urlSafeEncode($entry);
$array = [
'persistentOps'=>'imageView2/2/w/200/h/200/q/70/ignore-error/1/|saveas/'.$encodedEntryURI, //處理方式 200*200 縮略圖, 並保存縮略圖
'persistentNotifyUrl'=> url('appindex/Files/uploadImgNotify', '', true, true),
];
//生成上傳Token
$token = $auth->uploadToken($this->bucket, null, 3600, $array );
// 初始化 UploadManager 對象並進行文件的上傳
$uploadMgr = new UploadManager();
// 調用 UploadManager 的 putFile 方法進行文件的上傳
list($ret, $err) = $uploadMgr->putFile($token, $newFilename, $oldFilepath);
if ($err !== null) {
return ['code' => -1, 'msg' =>'上傳失敗'];
} else {
$t2 = microtime(true);
file_put_contents('time.log', "七牛雲上傳時間:" . ($t2 - $t1) . "秒", FILE_APPEND );
//返回圖片的完整URL
$newFilepath = $this->httpPre . $this->domain . "/" . $ret['key'];
//保存在本地的縮略圖
$thumbUrl = $this->getThumbUrlFromQiniuKey($ret['key']);
return ['code' => 1, 'msg' => '上傳成功', 'data' => ['url'=> $newFilepath, 'previewUrl' => $thumbUrl]];
}
}
以下代碼爲異步方法
deleteFile 方法就不貼出來了,七牛雲上有, 很簡單。
最後返回一個json字符串的通知,否則多媒體隊列會顯示失敗。
/**
* 上傳圖片後, 保存生成的縮略圖到本地,並刪除保存在空間中的縮略圖
* Function uploadImgNotify
* @author mselect <[email protected]>
* @DateTime 2019/5/16
*/
public function uploadImgNotify(){
$arr = $this->request->param();
if( !empty($arr['code']) && $arr['code'] == 0 ){
//成功
$items = $arr['items'];
$path = ROOT_PATH . $this->thumbSavePath;
if(!is_dir($path)){
mkdir($path, 0777, true);
}
foreach($items as $item){
if(strpos($item['cmd'], 'saveas') !== false){
//將資源下載,然後刪除
$url = $this->httpPre . $this->domain . '/' . $item['key'];
$back = file_put_contents($path . $item['key'] , file_get_contents($url));
if($back <=0 ){
unlink($path . $item['key']);
}else {
//將七牛雲上的縮略圖文件刪除
$this->deleteFile($item['key']);
}
}
}
$resp = array('ret' => 'succeed');
}else {
$resp = array('ret' => 'failed');
}
return json($resp);
}
2. 多文件打包zip 下載
keys 是保存了所有要打包的文件的key 數組
zipKey 爲保存在七牛雲上的壓縮包key
完成工作: 將keys數組中的所有文件打包成zip , 保存在七牛雲上,異步通知方法將壓縮包下載。
pipeline 爲自己創建的多媒體隊列名稱
key = test.zip 這個是個坑, 這個文件是個默認文件,你需要上傳一個test.zip (隨便上傳一個很小的壓縮包)到七牛雲。
/**
*
* 將作品的中所有水印圖片壓縮成zip下載
* Function mkzip
* @author mselect <[email protected]>
* @DateTime 2019/5/13
* @param $keys array 要壓縮的文件
*/
public function mkzip($keys, $zipKey){
require_once APP_PATH . '/../vendor/qiniu/autoload.php';
$auth = new Auth($this->accessKey, $this->secretKey);
$pipeline = 'mkzip';
$key = "test.zip"; //d5b37d559fbec90b0cdc_water
$pfop = new PersistentFop($auth, null);
$fops = 'mkzip/2';
foreach($keys as $value){
$fops .= '/url/' . \Qiniu\base64_urlSafeEncode($value);
}
$zipKey = $zipKey . ".zip";
$fops .= '|saveas/' . \Qiniu\base64_urlSafeEncode($this->bucket . ":" . $zipKey);
$notify_url = url('appindex/Files/mkzipNotify', '', true, true);
$force = false;
list($id, $err) = $pfop->execute($this->bucket, $key, $fops, $pipeline, $notify_url, $force);
file_put_contents('imgwater.log', "處理隊列ID: $id , 回調地址:$notify_url \r\n", FILE_APPEND);
if($err !==null){
return ['code' => -1, 'msg' => '打包失敗了'];
}else {
//輪詢隊列執行完成後才返回
while(true){
$url = "http://api.qiniu.com/status/get/prefop?id=" . $id;
$json = file_get_contents($url);
$arr = json_decode($json, true);
if($arr['code'] == 0 ){
break;
}else if($arr['code'] == 3 || $arr['code'] == 4){
file_put_contents('imgwater.log', $arr['desc'] . "\r\n", FILE_APPEND);
return ['code' => -1, 'msg'=> $arr['desc']];
}
}
return ['code' => 1, 'msg' => '壓縮成功', 'data' => ['fileKey' => $this->httpPre . $this->domain . "/" . $zipKey]];
}
}
下載壓縮包的方法與
以上異步通知方法類似,不再贅述。
以上方法如有問題,請及時聯繫我