Qiniu.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace crmeb\services\storage;
  3. use crmeb\services\SystemConfigService;
  4. use Qiniu\Auth;
  5. use Qiniu\Storage\BucketManager;
  6. use Qiniu\Storage\UploadManager;
  7. use Qiniu\Config;
  8. use think\facade\Cache;
  9. /**
  10. * TODO 七牛云上传
  11. * Class Qiniu
  12. */
  13. class Qiniu
  14. {
  15. protected static $accessKey;
  16. protected static $secretKey;
  17. protected static $auth = null;
  18. //TODO 空间域名 Domain
  19. protected static $uploadUrl;
  20. //TODO 存储空间名称 公开空间
  21. protected static $storageName;
  22. /**
  23. * TODO 初始化
  24. * @return null|Auth
  25. * @throws \Exception
  26. */
  27. protected static function autoInfo(){
  28. if(($storageName = Cache::get('storageName')) && ($uploadUrl = Cache::get('uploadUrl')) && ($accessKey = Cache::get('accessKey')) && ($secretKey = Cache::get('secretKey'))){
  29. self::$accessKey = $accessKey;
  30. self::$secretKey = $secretKey;
  31. self::$uploadUrl = $uploadUrl;
  32. self::$storageName = $storageName;
  33. }else{
  34. self::$accessKey = trim(SystemConfigService::get('accessKey'));
  35. self::$secretKey = trim(SystemConfigService::get('secretKey'));
  36. self::$uploadUrl = trim(SystemConfigService::get('uploadUrl')).'/';
  37. self::$storageName = trim(SystemConfigService::get('storage_name'));
  38. Cache::set('accessKey',self::$accessKey);
  39. Cache::set('secretKey',self::$secretKey);
  40. Cache::set('uploadUrl',self::$uploadUrl);
  41. Cache::set('storageName',self::$storageName);
  42. }
  43. if(!self::$accessKey || !self::$secretKey || !self::$uploadUrl || !self::$storageName){
  44. exception('请设置 secretKey 和 accessKey 和 空间域名 和 存储空间名称');
  45. }
  46. if(self::$auth == null) self::$auth = new Auth(self::$accessKey,self::$secretKey);
  47. return self::$auth;
  48. }
  49. /**
  50. * TODO 获取上传图片视频token和domain
  51. * @return array
  52. */
  53. public static function getToKenAndDomainI(){
  54. $token = self::autoInfo()->uploadToken(self::$storageName);
  55. $domain = self::$uploadUrl;
  56. $fileName = md5(rand(1000,9999). date('YmdHis') . rand(0, 9999));
  57. return compact('token','domain','fileName');
  58. }
  59. /**
  60. * TODO 图片上传 名称
  61. * @param string $filename
  62. * @return array
  63. * @throws \Exception
  64. */
  65. public static function uploadImage($filename = 'image'){
  66. $request = app('request');
  67. $file = $request->file($filename);
  68. $filePath = $file->getRealPath();
  69. $ext = $file->getOriginalExtension();
  70. $key = substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
  71. $token = self::autoInfo()->uploadToken(self::$storageName);
  72. try{
  73. $uploadMgr = new UploadManager();
  74. return $uploadMgr->putFile($token, $key, $filePath);
  75. }catch (\Exception $e){
  76. return $e->getMessage();
  77. }
  78. }
  79. /**
  80. * TODO 图片上传 内容
  81. * @param $key
  82. * @param $content
  83. * @return array|string
  84. * @throws \Exception
  85. */
  86. public static function uploadImageStream($key, $content){
  87. $token = self::autoInfo()->uploadToken(self::$storageName, $key);
  88. try{
  89. $uploadMgr = new UploadManager();
  90. return $uploadMgr->put($token, $key, $content);
  91. }catch (\Exception $e){
  92. return $e->getMessage();
  93. }
  94. }
  95. /**
  96. * TODO 图片下载链接
  97. * @param $key uploadImage() 返回的key
  98. * @param string $type 是否设置图片大小
  99. * @param string $imageUrl 图片访问链接
  100. * @param int $time 图片链接最后的访问时间
  101. * @return array
  102. */
  103. public static function imageUrl($key, $type = '', $imageUrl = '', $time = 0){
  104. return ['code'=>200,'name'=>$key,'dir'=>self::$uploadUrl.$key,'thumb_path'=>self::$uploadUrl.$key,'time'=>time()];
  105. // $imageValue = !strlen(trim($type)) ? self::$uploadUrl.$key : self::$uploadUrl.$key.self::getType($type);
  106. // if($time > time() && !strlen(trim($imageUrl))) return ['code'=>100,'dir'=>$imageUrl,'thumb_path'=>$imageUrl,'time'=>$time];
  107. // $imageUrl = self::autoInfo()->privateDownloadUrl($imageValue);
  108. // return ['code'=>200,'name'=>$key,'dir'=>$imageUrl,'thumb_path'=>$imageUrl,'time'=>bcadd(time(),3600,0)];
  109. }
  110. /**
  111. * TODO 获取图片时转换图片大小
  112. * @param $imageType
  113. * @return string
  114. */
  115. public static function getType($imageType){
  116. $type = '';
  117. switch ($imageType){
  118. case "8x6":
  119. $type='?imageView2/1/w/800/h/600';
  120. break;
  121. }
  122. return $type;
  123. }
  124. /**
  125. * TODO 删除资源
  126. * @param $key
  127. * @param $bucket
  128. * @return mixed
  129. */
  130. public static function delete($key){
  131. $bucketManager = new BucketManager(self::autoInfo(),new Config());
  132. return $bucketManager->delete(self::$storageName, $key);
  133. }
  134. }