Qiniu.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. $file['ext'] = strtolower(pathinfo($name, PATHINFO_EXTENSION));
  189. if($file['ext'] == 'jpeg'){
  190. $file['ext'] = 'jpg';
  191. }
  192. if(!in_array($file['ext'], $ext)){
  193. $this->error = '上传格式发生错误!';
  194. return false;
  195. }
  196. if(!file_exists($files)){
  197. $this->error = '上传文件不存在!';
  198. return false;
  199. }
  200. $object = $this->mk_str().'.'.$file['ext'];
  201. $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
  202. $token = $this->qiniu->uploadToken($this->config['bucket']);
  203. $uploadMgr = new \Qiniu\Storage\UploadManager();
  204. list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
  205. $r['status'] = 0;
  206. if ($err !== null) {
  207. $r['status'] = 0;
  208. $this->error = $err;
  209. } else{
  210. $r['status'] = 1;
  211. $r['key'] = $ret['key'];
  212. $r['url'] = $this->config['endpoint'].$ret['key'];
  213. }
  214. return $r;
  215. }
  216. /**
  217. * 上传文件[带压缩]
  218. * @param $path 保存带头前缀
  219. * @param $name 文件名
  220. * @param $files 上传文件
  221. * @param int $type 压缩情况
  222. * @param array $ext 上传后缀
  223. */
  224. public function updateFileThum($path,$name,$files,$type = 0,$ext = array('jpg','png','gif','zip','mp4','webp')){
  225. $thum = array(
  226. 0 => '320',
  227. 1 => '100',
  228. 2 => '800'
  229. );
  230. if (empty($files)) {
  231. $this->error = '没有上传的文件!';
  232. return false;
  233. }
  234. $file['ext'] = strtolower(pathinfo($name, PATHINFO_EXTENSION));
  235. if(!in_array($file['ext'], $ext)){
  236. $this->error = '上传格式发生错误!';
  237. return false;
  238. }
  239. if(!file_exists($files)){
  240. $this->error = '上传文件不存在!';
  241. return false;
  242. }
  243. //文件名
  244. $file_str = $this->mk_str();
  245. $object = $file_str.'.'.$file['ext'];
  246. $thum_obj = $file_str .'_' . $thum[$type] . '.'.$file['ext'];
  247. $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
  248. $token = $this->qiniu->uploadToken($this->config['bucket']);
  249. $uploadMgr = new UploadManager();
  250. list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
  251. if (empty($err)) {
  252. $jh_ar = array('jpg','png','gif');
  253. if(in_array($file['ext'], $jh_ar)) {
  254. //进行持久化操作
  255. $this->Fop($os_path . $object, $os_path . $thum_obj, $thum[$type]);
  256. }
  257. $r['status'] = 1;
  258. $r['url'] = $this->config['endpoint'] . $ret['key'];
  259. } else{
  260. $r['status'] = 0;
  261. $this->error = $err;
  262. }
  263. return $r;
  264. }
  265. /**
  266. * 上传文件【】
  267. **/
  268. public function TupdateFile($newFile,$name,$files){
  269. if (empty($files)) {
  270. $this->error = '没有上传的文件!';
  271. return false;
  272. }
  273. $token = $this->qiniu->uploadToken($this->config['bucket']);
  274. $uploadMgr = new \Qiniu\Storage\UploadManager();
  275. list($ret, $err) = $uploadMgr->putFile($token, $newFile, $files);
  276. $r['status'] = 0;
  277. if ($err !== null) {
  278. $r['status'] = 0;
  279. $this->error = $err;
  280. } else{
  281. $r['status'] = 1;
  282. $r['url'] = $this->config['endpoint'].$ret['key'];
  283. }
  284. return $r;
  285. }
  286. /**
  287. * 带路径文件夹文件上传
  288. * @param type $path
  289. * @param type $name
  290. * @param type $files
  291. * @param type $ext
  292. * @return boolean|string
  293. */
  294. public function pathFileUpload($path,$name,$files,$ext = array('svga')){
  295. if (empty($files)) {
  296. $this->error = '没有上传的文件!';
  297. return false;
  298. }
  299. $file['ext'] = strtolower(pathinfo($name, PATHINFO_EXTENSION));
  300. if(!in_array($file['ext'], $ext)){
  301. $this->error = '上传格式发生错误!';
  302. return false;
  303. }
  304. if(!file_exists($files)){
  305. $this->error = '上传文件不存在!';
  306. return false;
  307. }
  308. $object = $this->mk_str().'.'.$file['ext'];
  309. $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
  310. $token = $this->qiniu->uploadToken($this->config['bucket']);
  311. $uploadMgr = new UploadManager();
  312. list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
  313. $r = [];
  314. $r['status'] = 0;
  315. if ($err !== null) {
  316. $r['status'] = 0;
  317. $this->error = $err;
  318. } else{
  319. $r['status'] = 1;
  320. $r['url'] = $this->config['endpoint'].$ret['key'];
  321. }
  322. return $r;
  323. }
  324. /**
  325. * 持久化操作
  326. * @param $key
  327. * @param $savekey
  328. * @param $daima
  329. */
  330. public function Fop($key,$savekey,$type = 320){
  331. if($type == 320){
  332. $daima = 'imageView2/1/w/320/h/320/q/100|imageslim';
  333. }
  334. $pipeline = 'mg_51';
  335. $pfop = new \Qiniu\Processing\PersistentFop($this->qiniu,$this->config['bucket'],$pipeline,"http://www.mmgg.com/api", false);
  336. $fops = $daima."|saveas/" . \Qiniu\base64_urlSafeEncode($this->config['bucket'] . ":".$savekey);
  337. list($id, $err) = $pfop->execute($key, $fops);
  338. }
  339. /**
  340. * @param $obj
  341. */
  342. public function watermark($obj){
  343. $jh_ar = array('jpg','png','gif');
  344. $expAr = explode('.',$obj);
  345. $img = '';
  346. $exp = end($expAr);
  347. if(in_array($exp, $jh_ar)) {
  348. $fileAr = explode('/',$obj);
  349. $fileName = end($fileAr);
  350. $imgPath = str_replace($fileName,'',$obj);
  351. $img = md5('watermark'.$fileName).'.'.$exp;
  352. //进行持久化操作
  353. $this->FopSyin($obj, $imgPath.$img);
  354. }
  355. return $imgPath.$img;
  356. }
  357. //
  358. /**
  359. * 持久化操作
  360. * @param $key
  361. * @param $savekey
  362. * @param $daima
  363. */
  364. public function FopSyin($key,$savekey){
  365. $daima = "imageView2/0/interlace/1/q/100|watermark/1/image/aHR0cHM6Ly9pbWcuZ3VhaW1laWRhLmNvbS9zeWluX2xvZ2luLnBuZw==/dissolve/64/gravity/SouthEast/dx/10/dy/10|imageslim";
  366. $pipeline = 'task0013';
  367. $pfop = new \Qiniu\Processing\PersistentFop($this->qiniu,$this->config['bucket'],$pipeline,"http://www.guaimeida.com/", false);
  368. $fops = $daima."|saveas/" . \Qiniu\base64_urlSafeEncode($this->config['bucket'] . ":".$savekey);
  369. list($id, $err) = $pfop->execute($key, $fops);
  370. var_dump($err);
  371. }
  372. private function mk_str(){
  373. $d = sha1((string)uniqid((string)mt_rand(),true));
  374. $str = '';
  375. for($i = 0;$i<strlen($d);$i++){
  376. $v = $d[$i];
  377. if(ctype_alpha($d)){
  378. $rnd_true = rand(0, 1);
  379. if($rnd_true){
  380. $v = strtolower($d[$i]);
  381. } else {
  382. $v = strtoupper($d[$i]);
  383. }
  384. }
  385. $str .= $v;
  386. }
  387. //防止内存加载不变
  388. $time = substr((string)time(),0,-6);
  389. $tarray = array('A','B','C','D','E','F','G','H','M','T');
  390. $vstr = '';
  391. for($i = 0;$i<strlen($time);$i++){
  392. if($time[$i] % 2 == 0){
  393. $vstr .= 'X';
  394. } else {
  395. $vstr .= $tarray[rand(0, 9)];
  396. }
  397. }
  398. $str .= '-'.$vstr;
  399. return $str;
  400. }
  401. /**
  402. * 获取错误
  403. * @return type
  404. */
  405. public function getError(){
  406. return $this->error;
  407. }
  408. /**
  409. * 写入日志
  410. * @param type $word
  411. * @param type $name
  412. */
  413. private function log_result($word, $name) {
  414. $fp = fopen($name . "_log.txt", "a");
  415. flock($fp, LOCK_EX);
  416. fwrite($fp, $word . "\n");
  417. flock($fp, LOCK_UN);
  418. fclose($fp);
  419. }
  420. }