Qiniu.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. declare (strict_types=1);
  3. namespace library\utils;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-08-31 15:18
  12. // +----------------------------------------------------------------------
  13. use Qiniu\Storage\UploadManager;
  14. class Qiniu {
  15. private $qiniu = null;
  16. public $config = array();
  17. private $error = null;
  18. /**
  19. * 基本配置
  20. * @param type $conf
  21. * <p>
  22. * array(
  23. * 'accessKeyId' => '',
  24. * 'accessKeySecret' => '',
  25. * 'endpoint' => '',
  26. * 'bucket' => ''
  27. );
  28. * </p>
  29. */
  30. public function __construct($conf = array()) {
  31. $c = config('qiniu');
  32. foreach($c as $k=>$v){
  33. if(isset($conf[$k])){
  34. $this->config[$k] = $conf[$k];
  35. } else {
  36. $this->config[$k] = $v;
  37. }
  38. }
  39. try {
  40. $this->qiniu = new \Qiniu\Auth($this->config['accessKeyId'],
  41. $this->config['accessKeySecret']);
  42. } catch (Exception $ex) {
  43. $this->error = $ex->getMessage();
  44. $this->log_result($ex->getMessage(), $this->daatadir.'error');
  45. }
  46. }
  47. /**
  48. * 获取qiuniu信息
  49. * @global \OSS\OssClient $_A
  50. */
  51. public function getqiniu() {
  52. return $this->qiniu;
  53. }
  54. /**
  55. * 获取bucket信息
  56. * @return type
  57. */
  58. public function getBucketName(){
  59. return $this->config['bucket'];
  60. }
  61. /*
  62. * 获取预地址
  63. **/
  64. public function getYuFile($path,$exp = 'jpg'){
  65. $object = $this->mk_str().'.'.$exp;
  66. $os_path = $path.DIRECTORY_SEPARATOR.date('Y').DIRECTORY_SEPARATOR.date('m').DIRECTORY_SEPARATOR.date('d').DIRECTORY_SEPARATOR;
  67. return $os_path.$object;
  68. }
  69. /**
  70. * 发送短信
  71. * @param $template_id
  72. * @param $mobiles
  73. * @param null $parameters
  74. */
  75. public function sendSms($mobiles,$template_id,$parameters = null){
  76. $sms = new \Qiniu\Sms\Sms($this->qiniu);
  77. $rAr = [];
  78. try {
  79. $d = $sms->sendMessage($template_id, [$mobiles], $parameters);
  80. if($d[1] instanceof Qiniu\Http\Error) {
  81. $dObj = $d[1];
  82. $rAr['Code'] = 'error';
  83. $rAr['Message'] = $dObj->message();
  84. } else {
  85. $rAr['Code'] = 'OK';
  86. $rAr['Message'] = '';
  87. }
  88. }catch (\Exception $e) {
  89. $rAr['Code'] = 'error';
  90. $rAr['Message'] = $e;
  91. }
  92. return $rAr;
  93. }
  94. /**
  95. * 上传文件 | 内容写入
  96. * @param type $path 目录
  97. * @param type $content 内容
  98. * @param type $type 后缀
  99. */
  100. public function updateFileContent($path,$content,$type = 'jpg',$file = ''){
  101. $type = strtolower($type);
  102. if($type == 'jpeg') $type = 'jpg';
  103. $object = '';
  104. if(empty($file)) {
  105. $object = $this->mk_str().'.'.$type;
  106. $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
  107. } else {
  108. $os_path = $path.'/' . $file;
  109. }
  110. $token = $this->qiniu->uploadToken($this->config['bucket']);
  111. $uploadMgr = new \Qiniu\Storage\UploadManager();
  112. list($ret, $err) = $uploadMgr->put($token, $os_path.$object, $content);
  113. $r['status'] = 0;
  114. if ($err !== null) {
  115. $r['status'] = 0;
  116. $this->error = $err;
  117. } else{
  118. $r['status'] = 1;
  119. $r['url'] = $this->config['endpoint'].$ret['key'];
  120. }
  121. return $r;
  122. }
  123. /**
  124. * 上传文件
  125. * @param type $path 目录
  126. * @param type $name 上传文件名称【后缀记录】
  127. * @param type $files 上传实际文件
  128. * @param type $ext 限制后缀名
  129. * @return boolean
  130. */
  131. public function updateFile($path,$name,$files,$ext = array('jpg','png','gif')) {
  132. if (empty($files)) {
  133. $this->error = '没有上传的文件!';
  134. return false;
  135. }
  136. $file['ext'] = strtolower(pathinfo($name, PATHINFO_EXTENSION));
  137. if($file['ext'] == 'jpeg'){
  138. $file['ext'] = 'jpg';
  139. }
  140. if(!in_array($file['ext'], $ext)){
  141. $this->error = '上传格式发生错误!';
  142. return false;
  143. }
  144. if(!file_exists($files)){
  145. $this->error = '上传文件不存在!';
  146. return false;
  147. }
  148. $object = $this->mk_str().'.'.$file['ext'];
  149. $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
  150. $token = $this->qiniu->uploadToken($this->config['bucket']);
  151. $uploadMgr = new \Qiniu\Storage\UploadManager();
  152. list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
  153. $r['status'] = 0;
  154. if ($err !== null) {
  155. $r['status'] = 0;
  156. $this->error = $err;
  157. } else{
  158. $r['status'] = 1;
  159. $r['url'] = $this->config['endpoint'].$ret['key'];
  160. }
  161. return $r;
  162. }
  163. /**
  164. * 上传文件[带压缩]
  165. * @param $path 保存带头前缀
  166. * @param $name 文件名
  167. * @param $files 上传文件
  168. * @param int $type 压缩情况
  169. * @param array $ext 上传后缀
  170. */
  171. public function updateFileThum($path,$name,$files,$type = 0,$ext = array('jpg','png','gif','zip','mp4')){
  172. $thum = array(
  173. 0 => '320',
  174. 1 => '100',
  175. 2 => '800'
  176. );
  177. if (empty($files)) {
  178. $this->error = '没有上传的文件!';
  179. return false;
  180. }
  181. $file['ext'] = strtolower(pathinfo($name, PATHINFO_EXTENSION));
  182. if(!in_array($file['ext'], $ext)){
  183. $this->error = '上传格式发生错误!';
  184. return false;
  185. }
  186. if(!file_exists($files)){
  187. $this->error = '上传文件不存在!';
  188. return false;
  189. }
  190. //文件名
  191. $file_str = $this->mk_str();
  192. $object = $file_str.'.'.$file['ext'];
  193. $thum_obj = $file_str .'_' . $thum[$type] . '.'.$file['ext'];
  194. $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
  195. $token = $this->qiniu->uploadToken($this->config['bucket']);
  196. $uploadMgr = new UploadManager();
  197. list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
  198. if (empty($err)) {
  199. $jh_ar = array('jpg','png','gif');
  200. if(in_array($file['ext'], $jh_ar)) {
  201. //进行持久化操作
  202. $this->Fop($os_path . $object, $os_path . $thum_obj, $thum[$type]);
  203. }
  204. $r['status'] = 1;
  205. $r['url'] = $this->config['endpoint'] . $ret['key'];
  206. } else{
  207. $r['status'] = 0;
  208. $this->error = $err;
  209. }
  210. return $r;
  211. }
  212. /**
  213. * 上传文件【】
  214. **/
  215. public function TupdateFile($newFile,$name,$files){
  216. if (empty($files)) {
  217. $this->error = '没有上传的文件!';
  218. return false;
  219. }
  220. $token = $this->qiniu->uploadToken($this->config['bucket']);
  221. $uploadMgr = new \Qiniu\Storage\UploadManager();
  222. list($ret, $err) = $uploadMgr->putFile($token, $newFile, $files);
  223. $r['status'] = 0;
  224. if ($err !== null) {
  225. $r['status'] = 0;
  226. $this->error = $err;
  227. } else{
  228. $r['status'] = 1;
  229. $r['url'] = $this->config['domain'].$ret['key'];
  230. }
  231. return $r;
  232. }
  233. /**
  234. * 持久化操作
  235. * @param $key
  236. * @param $savekey
  237. * @param $daima
  238. */
  239. public function Fop($key,$savekey,$type = 320){
  240. if($type == 320){
  241. $daima = 'imageView2/1/w/320/h/320/q/100|imageslim';
  242. }
  243. $pipeline = 'mg_51';
  244. $pfop = new \Qiniu\Processing\PersistentFop($this->qiniu,$this->config['bucket'],$pipeline,"http://www.mmgg.com/api", false);
  245. $fops = $daima."|saveas/" . \Qiniu\base64_urlSafeEncode($this->config['bucket'] . ":".$savekey);
  246. list($id, $err) = $pfop->execute($key, $fops);
  247. }
  248. /**
  249. * @param $obj
  250. */
  251. public function watermark($obj){
  252. $jh_ar = array('jpg','png','gif');
  253. $expAr = explode('.',$obj);
  254. $img = '';
  255. $exp = end($expAr);
  256. if(in_array($exp, $jh_ar)) {
  257. $fileAr = explode('/',$obj);
  258. $fileName = end($fileAr);
  259. $imgPath = str_replace($fileName,'',$obj);
  260. $img = md5('watermark'.$fileName).'.'.$exp;
  261. //进行持久化操作
  262. $this->FopSyin($obj, $imgPath.$img);
  263. }
  264. return $imgPath.$img;
  265. }
  266. //
  267. /**
  268. * 持久化操作
  269. * @param $key
  270. * @param $savekey
  271. * @param $daima
  272. */
  273. public function FopSyin($key,$savekey){
  274. $daima = "imageView2/0/interlace/1/q/100|watermark/1/image/aHR0cHM6Ly9pbWcuZ3VhaW1laWRhLmNvbS9zeWluX2xvZ2luLnBuZw==/dissolve/64/gravity/SouthEast/dx/10/dy/10|imageslim";
  275. $pipeline = 'task0013';
  276. $pfop = new \Qiniu\Processing\PersistentFop($this->qiniu,$this->config['bucket'],$pipeline,"http://www.guaimeida.com/", false);
  277. $fops = $daima."|saveas/" . \Qiniu\base64_urlSafeEncode($this->config['bucket'] . ":".$savekey);
  278. list($id, $err) = $pfop->execute($key, $fops);
  279. var_dump($err);
  280. }
  281. private function mk_str(){
  282. $d = sha1((string)uniqid((string)mt_rand(),true));
  283. $str = '';
  284. for($i = 0;$i<strlen($d);$i++){
  285. $v = $d[$i];
  286. if(ctype_alpha($d)){
  287. $rnd_true = rand(0, 1);
  288. if($rnd_true){
  289. $v = strtolower($d[$i]);
  290. } else {
  291. $v = strtoupper($d[$i]);
  292. }
  293. }
  294. $str .= $v;
  295. }
  296. //防止内存加载不变
  297. $time = substr((string)time(),0,-6);
  298. $tarray = array('A','B','C','D','E','F','G','H','M','T');
  299. $vstr = '';
  300. for($i = 0;$i<strlen($time);$i++){
  301. if($time[$i] % 2 == 0){
  302. $vstr .= 'X';
  303. } else {
  304. $vstr .= $tarray[rand(0, 9)];
  305. }
  306. }
  307. $str .= '-'.$vstr;
  308. return $str;
  309. }
  310. /**
  311. * 获取错误
  312. * @return type
  313. */
  314. public function getError(){
  315. return $this->error;
  316. }
  317. /**
  318. * 写入日志
  319. * @param type $word
  320. * @param type $name
  321. */
  322. private function log_result($word, $name) {
  323. $fp = fopen($name . "_log.txt", "a");
  324. flock($fp, LOCK_EX);
  325. fwrite($fp, $word . "\n");
  326. flock($fp, LOCK_UN);
  327. fclose($fp);
  328. }
  329. }