Qiniu.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. use Qiniu\Storage\ArgusManager;
  15. use Qiniu\Storage\BucketManager;
  16. use Qiniu\Config as ServerConfig;
  17. class Qiniu {
  18. private $qiniu = null;
  19. public $config = array();
  20. private $error = null;
  21. /**
  22. * 基本配置
  23. * @param type $conf
  24. * <p>
  25. * array(
  26. * 'accessKeyId' => '',
  27. * 'accessKeySecret' => '',
  28. * 'endpoint' => '',
  29. * 'bucket' => ''
  30. );
  31. * </p>
  32. */
  33. public function __construct($conf = array()) {
  34. $c = config('qiniu');
  35. foreach($c as $k=>$v){
  36. if(isset($conf[$k])){
  37. $this->config[$k] = $conf[$k];
  38. } else {
  39. $this->config[$k] = $v;
  40. }
  41. }
  42. try {
  43. $this->qiniu = new \Qiniu\Auth($this->config['accessKeyId'],$this->config['accessKeySecret']);
  44. } catch (Exception $ex) {
  45. $this->error = $ex->getMessage();
  46. $this->log_result($ex->getMessage(), $this->daatadir.'error');
  47. }
  48. }
  49. /**
  50. * 图片删除
  51. * @param type $img
  52. */
  53. public function imageDelete($img=""){
  54. $img = str_replace("http://upload.myjie.cn/","", $img);
  55. $img = str_replace("https://upload.myjie.cn/","", $img);
  56. $config = new ServerConfig();
  57. $bucketManager = new BucketManager($this->qiniu, $config);
  58. $err = $bucketManager->delete($this->config['bucket'], $img);
  59. }
  60. /**
  61. * 图片鉴别
  62. * @param type $img
  63. * @param type $scenes pulp黄,terror暴,politician敏感,ads广告
  64. * @return boolean
  65. */
  66. public function imageCensor($img="",$scenes=["pulp","terror","politician","ads"]){
  67. try{
  68. $config = new ServerConfig();
  69. $argusManager = new ArgusManager($this->qiniu, $config);
  70. $body=[
  71. "data"=>[
  72. "uri"=>$img
  73. ],
  74. "params"=>[
  75. "scenes"=>$scenes
  76. ]
  77. ];
  78. list($ret, $err) = $argusManager->censorImage(json_encode($body));
  79. if ($err !== null) {
  80. return false;
  81. } else {
  82. if(empty($ret) || empty($ret["code"]) || empty($ret["result"]) || $ret["code"]!=200){
  83. return false;
  84. }
  85. return $ret["result"];
  86. }
  87. }catch(Exception $ex){
  88. return false;
  89. }
  90. }
  91. /**
  92. * 获取qiuniu信息
  93. * @global \OSS\OssClient $_A
  94. */
  95. public function getqiniu() {
  96. return $this->qiniu;
  97. }
  98. /**
  99. * 获取bucket信息
  100. * @return type
  101. */
  102. public function getBucketName(){
  103. return $this->config['bucket'];
  104. }
  105. /**
  106. * 获取预地址
  107. */
  108. public function getYuFile($path,$exp = 'jpg'){
  109. $object = $this->mk_str().'.'.$exp;
  110. $os_path = $path.DIRECTORY_SEPARATOR.date('Y').DIRECTORY_SEPARATOR.date('m').DIRECTORY_SEPARATOR.date('d').DIRECTORY_SEPARATOR;
  111. return $os_path.$object;
  112. }
  113. /**
  114. * 发送短信
  115. * @param $template_id
  116. * @param $mobiles
  117. * @param null $parameters
  118. */
  119. public function sendSms($mobiles,$template_id,$parameters = null){
  120. $sms = new \Qiniu\Sms\Sms($this->qiniu);
  121. $rAr = [];
  122. try {
  123. $d = $sms->sendMessage($template_id, [$mobiles], $parameters);
  124. if($d[1] instanceof Qiniu\Http\Error) {
  125. $dObj = $d[1];
  126. $rAr['Code'] = 'error';
  127. $rAr['Message'] = $dObj->message();
  128. } else {
  129. $rAr['Code'] = 'OK';
  130. $rAr['Message'] = '';
  131. }
  132. }catch (\Exception $e) {
  133. $rAr['Code'] = 'error';
  134. $rAr['Message'] = $e;
  135. }
  136. return $rAr;
  137. }
  138. /**
  139. * 上传文件 | 内容写入
  140. * @param type $path 目录
  141. * @param type $content 内容
  142. * @param type $type 后缀
  143. */
  144. public function updateFileContent($path,$content,$type = 'jpg',$file = ''){
  145. $type = strtolower($type);
  146. if($type == 'jpeg') $type = 'jpg';
  147. $object = '';
  148. if(empty($file)) {
  149. $object = $this->mk_str().'.'.$type;
  150. $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
  151. } else {
  152. $os_path = $path.'/' . $file;
  153. }
  154. $token = $this->qiniu->uploadToken($this->config['bucket']);
  155. $uploadMgr = new \Qiniu\Storage\UploadManager();
  156. list($ret, $err) = $uploadMgr->put($token, $os_path.$object, $content);
  157. $r['status'] = 0;
  158. if ($err !== null) {
  159. $r['status'] = 0;
  160. $this->error = $err;
  161. } else{
  162. $r['status'] = 1;
  163. $r['url'] = $this->config['endpoint'].$ret['key'];
  164. }
  165. return $r;
  166. }
  167. /**
  168. * 生成上传token
  169. */
  170. public function createUploadToken($bucket=null){
  171. $bucket = empty($bucket) ? $this->config['bucket'] : $bucket;
  172. $token = $this->qiniu->uploadToken($bucket);
  173. return $token;
  174. }
  175. // /**
  176. // * 上传文件
  177. // * @param type $path 目录
  178. // * @param type $name 上传文件名称【后缀记录】
  179. // * @param type $files 上传实际文件
  180. // * @param type $ext 限制后缀名
  181. // * @return boolean
  182. // */
  183. // public function updateFile($path,$name,$files,$ext = array('jpg','png','gif','webp')) {
  184. // if (empty($files)) {
  185. // $this->error = '没有上传的文件!';
  186. // return false;
  187. // }
  188. //
  189. // $file['ext'] = strtolower(pathinfo($name, PATHINFO_EXTENSION));
  190. //
  191. // if($file['ext'] == 'jpeg'){
  192. // $file['ext'] = 'jpg';
  193. // }
  194. //
  195. //
  196. // if(!in_array($file['ext'], $ext)){
  197. // $this->error = '上传格式发生错误!';
  198. // return false;
  199. // }
  200. //
  201. // if(!file_exists($files)){
  202. // $this->error = '上传文件不存在!';
  203. // return false;
  204. // }
  205. //
  206. //
  207. //
  208. // $object = $this->mk_str().'.'.$file['ext'];
  209. //
  210. // $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
  211. //
  212. // $token = $this->qiniu->uploadToken($this->config['bucket']);
  213. //
  214. // $uploadMgr = new \Qiniu\Storage\UploadManager();
  215. //
  216. // list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
  217. //
  218. //
  219. // $r['status'] = 0;
  220. // if ($err !== null) {
  221. // $r['status'] = 0;
  222. // $this->error = $err;
  223. // } else{
  224. // $r['status'] = 1;
  225. // $r['key'] = $ret['key'];
  226. // $r['url'] = $this->config['endpoint'].$ret['key'];
  227. // }
  228. // return $r;
  229. // }
  230. /**
  231. * 上传文件
  232. * @param type $path 目录
  233. * @param type $name 上传文件名称【后缀记录】
  234. * @param type $files 上传实际文件
  235. * @param type $ext 限制后缀名
  236. * @return boolean
  237. */
  238. public function updateFile($path, $name, $files, $ext = array('jpg', 'png', 'gif', 'webp', 'mp3', 'mp4'))
  239. {
  240. if (empty($files)) {
  241. $this->error = '没有上传的文件!';
  242. return false;
  243. }
  244. $file['ext'] = strtolower(pathinfo($name, PATHINFO_EXTENSION));
  245. if ($file['ext'] == 'jpeg') {
  246. $file['ext'] = 'jpg';
  247. }
  248. if (!in_array($file['ext'], $ext)) {
  249. $this->error = '上传格式发生错误!';
  250. return false;
  251. }
  252. if (!file_exists($files)) {
  253. $this->error = '上传文件不存在!';
  254. return false;
  255. }
  256. $object = $this->mk_str() . '.' . $file['ext'];
  257. $os_path = $path . '/' . date('Y') . '/' . date('m') . '/' . date('d') . '/';
  258. $token = $this->qiniu->uploadToken($this->config['bucket']);
  259. $uploadMgr = new \Qiniu\Storage\UploadManager();
  260. list($ret, $err) = $uploadMgr->putFile($token, $os_path . $object, $files);
  261. $r['status'] = 0;
  262. if ($err !== null) {
  263. $r['status'] = 0;
  264. $this->error = $err;
  265. } else {
  266. $r['status'] = 1;
  267. $r['key'] = $ret['key'];
  268. $r['url'] = $this->config['endpoint'] . $ret['key'];
  269. }
  270. return $r;
  271. }
  272. /**
  273. * 上传文件[带压缩]
  274. * @param $path 保存带头前缀
  275. * @param $name 文件名
  276. * @param $files 上传文件
  277. * @param int $type 压缩情况
  278. * @param array $ext 上传后缀
  279. */
  280. public function updateFileThum($path,$name,$files,$type = 0,$ext = array('jpg','png','gif','zip','mp4','webp')){
  281. $thum = array(
  282. 0 => '320',
  283. 1 => '100',
  284. 2 => '800'
  285. );
  286. if (empty($files)) {
  287. $this->error = '没有上传的文件!';
  288. return false;
  289. }
  290. $file['ext'] = strtolower(pathinfo($name, PATHINFO_EXTENSION));
  291. if(!in_array($file['ext'], $ext)){
  292. $this->error = '上传格式发生错误!';
  293. return false;
  294. }
  295. if(!file_exists($files)){
  296. $this->error = '上传文件不存在!';
  297. return false;
  298. }
  299. //文件名
  300. $file_str = $this->mk_str();
  301. $object = $file_str.'.'.$file['ext'];
  302. $thum_obj = $file_str .'_' . $thum[$type] . '.'.$file['ext'];
  303. $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
  304. $token = $this->qiniu->uploadToken($this->config['bucket']);
  305. $uploadMgr = new UploadManager();
  306. list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
  307. if (empty($err)) {
  308. $jh_ar = array('jpg','png','gif');
  309. if(in_array($file['ext'], $jh_ar)) {
  310. //进行持久化操作
  311. $this->Fop($os_path . $object, $os_path . $thum_obj, $thum[$type]);
  312. }
  313. $r['status'] = 1;
  314. $r['url'] = $this->config['endpoint'] . $ret['key'];
  315. } else{
  316. $r['status'] = 0;
  317. $this->error = $err;
  318. }
  319. return $r;
  320. }
  321. /**
  322. * 上传文件【】
  323. **/
  324. public function TupdateFile($newFile,$name,$files){
  325. if (empty($files)) {
  326. $this->error = '没有上传的文件!';
  327. return false;
  328. }
  329. $token = $this->qiniu->uploadToken($this->config['bucket']);
  330. $uploadMgr = new \Qiniu\Storage\UploadManager();
  331. list($ret, $err) = $uploadMgr->putFile($token, $newFile, $files);
  332. $r['status'] = 0;
  333. if ($err !== null) {
  334. $r['status'] = 0;
  335. $this->error = $err;
  336. } else{
  337. $r['status'] = 1;
  338. $r['url'] = $this->config['endpoint'].$ret['key'];
  339. }
  340. return $r;
  341. }
  342. /**
  343. * 带路径文件夹文件上传
  344. * @param type $path
  345. * @param type $name
  346. * @param type $files
  347. * @param type $ext
  348. * @return boolean|string
  349. */
  350. public function pathFileUpload($path,$name,$files,$ext = array('svga')){
  351. if (empty($files)) {
  352. $this->error = '没有上传的文件!';
  353. return false;
  354. }
  355. $file['ext'] = strtolower(pathinfo($name, PATHINFO_EXTENSION));
  356. if(!in_array($file['ext'], $ext)){
  357. $this->error = '上传格式发生错误!';
  358. return false;
  359. }
  360. if(!file_exists($files)){
  361. $this->error = '上传文件不存在!';
  362. return false;
  363. }
  364. $object = $this->mk_str().'.'.$file['ext'];
  365. $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
  366. $token = $this->qiniu->uploadToken($this->config['bucket']);
  367. $uploadMgr = new UploadManager();
  368. list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
  369. $r = [];
  370. $r['status'] = 0;
  371. if ($err !== null) {
  372. $r['status'] = 0;
  373. $this->error = $err;
  374. } else{
  375. $r['status'] = 1;
  376. $r['url'] = $this->config['endpoint'].$ret['key'];
  377. }
  378. return $r;
  379. }
  380. /**
  381. * 持久化操作
  382. * @param $key
  383. * @param $savekey
  384. * @param $daima
  385. */
  386. public function Fop($key,$savekey,$type = 320){
  387. if($type == 320){
  388. $daima = 'imageView2/1/w/320/h/320/q/100|imageslim';
  389. }
  390. $pipeline = 'mg_51';
  391. $pfop = new \Qiniu\Processing\PersistentFop($this->qiniu,$this->config['bucket'],$pipeline,"http://www.mmgg.com/api", false);
  392. $fops = $daima."|saveas/" . \Qiniu\base64_urlSafeEncode($this->config['bucket'] . ":".$savekey);
  393. list($id, $err) = $pfop->execute($key, $fops);
  394. }
  395. /**
  396. * @param $obj
  397. */
  398. public function watermark($obj){
  399. $jh_ar = array('jpg','png','gif');
  400. $expAr = explode('.',$obj);
  401. $img = '';
  402. $exp = end($expAr);
  403. if(in_array($exp, $jh_ar)) {
  404. $fileAr = explode('/',$obj);
  405. $fileName = end($fileAr);
  406. $imgPath = str_replace($fileName,'',$obj);
  407. $img = md5('watermark'.$fileName).'.'.$exp;
  408. //进行持久化操作
  409. $this->FopSyin($obj, $imgPath.$img);
  410. }
  411. return $imgPath.$img;
  412. }
  413. //
  414. /**
  415. * 持久化操作
  416. * @param $key
  417. * @param $savekey
  418. * @param $daima
  419. */
  420. public function FopSyin($key,$savekey){
  421. $daima = "imageView2/0/interlace/1/q/100|watermark/1/image/aHR0cHM6Ly9pbWcuZ3VhaW1laWRhLmNvbS9zeWluX2xvZ2luLnBuZw==/dissolve/64/gravity/SouthEast/dx/10/dy/10|imageslim";
  422. $pipeline = 'task0013';
  423. $pfop = new \Qiniu\Processing\PersistentFop($this->qiniu,$this->config['bucket'],$pipeline,"http://www.guaimeida.com/", false);
  424. $fops = $daima."|saveas/" . \Qiniu\base64_urlSafeEncode($this->config['bucket'] . ":".$savekey);
  425. list($id, $err) = $pfop->execute($key, $fops);
  426. var_dump($err);
  427. }
  428. private function mk_str(){
  429. $d = sha1((string)uniqid((string)mt_rand(),true));
  430. $str = '';
  431. for($i = 0;$i<strlen($d);$i++){
  432. $v = $d[$i];
  433. if(ctype_alpha($d)){
  434. $rnd_true = rand(0, 1);
  435. if($rnd_true){
  436. $v = strtolower($d[$i]);
  437. } else {
  438. $v = strtoupper($d[$i]);
  439. }
  440. }
  441. $str .= $v;
  442. }
  443. //防止内存加载不变
  444. $time = substr((string)time(),0,-6);
  445. $tarray = array('A','B','C','D','E','F','G','H','M','T');
  446. $vstr = '';
  447. for($i = 0;$i<strlen($time);$i++){
  448. if($time[$i] % 2 == 0){
  449. $vstr .= 'X';
  450. } else {
  451. $vstr .= $tarray[rand(0, 9)];
  452. }
  453. }
  454. $str .= '-'.$vstr;
  455. return $str;
  456. }
  457. /**
  458. * 获取错误
  459. * @return type
  460. */
  461. public function getError(){
  462. return $this->error;
  463. }
  464. /**
  465. * 写入日志
  466. * @param type $word
  467. * @param type $name
  468. */
  469. private function log_result($word, $name) {
  470. $fp = fopen($name . "_log.txt", "a");
  471. flock($fp, LOCK_EX);
  472. fwrite($fp, $word . "\n");
  473. flock($fp, LOCK_UN);
  474. fclose($fp);
  475. }
  476. }