123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- <?php
- declare (strict_types=1);
- namespace library\utils;
- // +----------------------------------------------------------------------
- // | [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2018-2020 rights reserved.
- // +----------------------------------------------------------------------
- // | Author: TABLE ME
- // +----------------------------------------------------------------------
- // | Date: 2020-08-31 15:18
- // +----------------------------------------------------------------------
- use Qiniu\Storage\UploadManager;
- use Qiniu\Storage\ArgusManager;
- use Qiniu\Storage\BucketManager;
- use Qiniu\Config as ServerConfig;
- class Qiniu {
- private $qiniu = null;
- public $config = array();
- private $error = null;
- /**
- * 基本配置
- * @param type $conf
- * <p>
- * array(
- * 'accessKeyId' => '',
- * 'accessKeySecret' => '',
- * 'endpoint' => '',
- * 'bucket' => ''
- );
- * </p>
- */
- public function __construct($conf = array()) {
- $c = config('qiniu');
- foreach($c as $k=>$v){
- if(isset($conf[$k])){
- $this->config[$k] = $conf[$k];
- } else {
- $this->config[$k] = $v;
- }
- }
- try {
- $this->qiniu = new \Qiniu\Auth($this->config['accessKeyId'],$this->config['accessKeySecret']);
- } catch (Exception $ex) {
- $this->error = $ex->getMessage();
- $this->log_result($ex->getMessage(), $this->daatadir.'error');
- }
- }
- /**
- * 图片删除
- * @param type $img
- */
- public function imageDelete($img=""){
- $img = str_replace("http://upload.myjie.cn/","", $img);
- $img = str_replace("https://upload.myjie.cn/","", $img);
- $config = new ServerConfig();
- $bucketManager = new BucketManager($this->qiniu, $config);
- $err = $bucketManager->delete($this->config['bucket'], $img);
-
- }
- /**
- * 图片鉴别
- * @param type $img
- * @param type $scenes pulp黄,terror暴,politician敏感,ads广告
- * @return boolean
- */
- public function imageCensor($img="",$scenes=["pulp","terror","politician","ads"]){
- try{
- $config = new ServerConfig();
- $argusManager = new ArgusManager($this->qiniu, $config);
- $body=[
- "data"=>[
- "uri"=>$img
- ],
- "params"=>[
- "scenes"=>$scenes
- ]
- ];
- list($ret, $err) = $argusManager->censorImage(json_encode($body));
- if ($err !== null) {
- return false;
- } else {
- if(empty($ret) || empty($ret["code"]) || empty($ret["result"]) || $ret["code"]!=200){
- return false;
- }
- return $ret["result"];
- }
- }catch(Exception $ex){
- return false;
- }
-
- }
- /**
- * 获取qiuniu信息
- * @global \OSS\OssClient $_A
- */
- public function getqiniu() {
- return $this->qiniu;
- }
- /**
- * 获取bucket信息
- * @return type
- */
- public function getBucketName(){
- return $this->config['bucket'];
- }
- /**
- * 获取预地址
- */
- public function getYuFile($path,$exp = 'jpg'){
- $object = $this->mk_str().'.'.$exp;
- $os_path = $path.DIRECTORY_SEPARATOR.date('Y').DIRECTORY_SEPARATOR.date('m').DIRECTORY_SEPARATOR.date('d').DIRECTORY_SEPARATOR;
- return $os_path.$object;
- }
- /**
- * 发送短信
- * @param $template_id
- * @param $mobiles
- * @param null $parameters
- */
- public function sendSms($mobiles,$template_id,$parameters = null){
- $sms = new \Qiniu\Sms\Sms($this->qiniu);
- $rAr = [];
- try {
- $d = $sms->sendMessage($template_id, [$mobiles], $parameters);
- if($d[1] instanceof Qiniu\Http\Error) {
- $dObj = $d[1];
- $rAr['Code'] = 'error';
- $rAr['Message'] = $dObj->message();
- } else {
- $rAr['Code'] = 'OK';
- $rAr['Message'] = '';
- }
- }catch (\Exception $e) {
- $rAr['Code'] = 'error';
- $rAr['Message'] = $e;
- }
- return $rAr;
- }
- /**
- * 上传文件 | 内容写入
- * @param type $path 目录
- * @param type $content 内容
- * @param type $type 后缀
- */
- public function updateFileContent($path,$content,$type = 'jpg',$file = ''){
- $type = strtolower($type);
- if($type == 'jpeg') $type = 'jpg';
- $object = '';
- if(empty($file)) {
- $object = $this->mk_str().'.'.$type;
- $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
- } else {
- $os_path = $path.'/' . $file;
- }
- $token = $this->qiniu->uploadToken($this->config['bucket']);
- $uploadMgr = new \Qiniu\Storage\UploadManager();
- list($ret, $err) = $uploadMgr->put($token, $os_path.$object, $content);
- $r['status'] = 0;
- if ($err !== null) {
- $r['status'] = 0;
- $this->error = $err;
- } else{
- $r['status'] = 1;
- $r['url'] = $this->config['endpoint'].$ret['key'];
- }
- return $r;
- }
- /**
- * 生成上传token
- */
- public function createUploadToken($bucket=null){
- $bucket = empty($bucket) ? $this->config['bucket'] : $bucket;
- $token = $this->qiniu->uploadToken($bucket);
- return $token;
- }
- /**
- * 上传文件
- * @param type $path 目录
- * @param type $name 上传文件名称【后缀记录】
- * @param type $files 上传实际文件
- * @param type $ext 限制后缀名
- * @return boolean
- */
- public function updateFile($path,$name,$files,$ext = array('jpg','png','gif','webp')) {
- if (empty($files)) {
- $this->error = '没有上传的文件!';
- return false;
- }
- $file['ext'] = strtolower(pathinfo($name, PATHINFO_EXTENSION));
- if($file['ext'] == 'jpeg'){
- $file['ext'] = 'jpg';
- }
- if(!in_array($file['ext'], $ext)){
- $this->error = '上传格式发生错误!';
- return false;
- }
- if(!file_exists($files)){
- $this->error = '上传文件不存在!';
- return false;
- }
- $object = $this->mk_str().'.'.$file['ext'];
- $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
- $token = $this->qiniu->uploadToken($this->config['bucket']);
- $uploadMgr = new \Qiniu\Storage\UploadManager();
- list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
- $r['status'] = 0;
- if ($err !== null) {
- $r['status'] = 0;
- $this->error = $err;
- } else{
- $r['status'] = 1;
- $r['key'] = $ret['key'];
- $r['url'] = $this->config['endpoint'].$ret['key'];
- }
- return $r;
- }
- /**
- * 上传文件[带压缩]
- * @param $path 保存带头前缀
- * @param $name 文件名
- * @param $files 上传文件
- * @param int $type 压缩情况
- * @param array $ext 上传后缀
- */
- public function updateFileThum($path,$name,$files,$type = 0,$ext = array('jpg','png','gif','zip','mp4','webp')){
- $thum = array(
- 0 => '320',
- 1 => '100',
- 2 => '800'
- );
- if (empty($files)) {
- $this->error = '没有上传的文件!';
- return false;
- }
- $file['ext'] = strtolower(pathinfo($name, PATHINFO_EXTENSION));
- if(!in_array($file['ext'], $ext)){
- $this->error = '上传格式发生错误!';
- return false;
- }
- if(!file_exists($files)){
- $this->error = '上传文件不存在!';
- return false;
- }
- //文件名
- $file_str = $this->mk_str();
- $object = $file_str.'.'.$file['ext'];
- $thum_obj = $file_str .'_' . $thum[$type] . '.'.$file['ext'];
- $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
- $token = $this->qiniu->uploadToken($this->config['bucket']);
- $uploadMgr = new UploadManager();
- list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
- if (empty($err)) {
- $jh_ar = array('jpg','png','gif');
- if(in_array($file['ext'], $jh_ar)) {
- //进行持久化操作
- $this->Fop($os_path . $object, $os_path . $thum_obj, $thum[$type]);
- }
- $r['status'] = 1;
- $r['url'] = $this->config['endpoint'] . $ret['key'];
- } else{
- $r['status'] = 0;
- $this->error = $err;
- }
- return $r;
- }
- /**
- * 上传文件【】
- **/
- public function TupdateFile($newFile,$name,$files){
- if (empty($files)) {
- $this->error = '没有上传的文件!';
- return false;
- }
- $token = $this->qiniu->uploadToken($this->config['bucket']);
- $uploadMgr = new \Qiniu\Storage\UploadManager();
- list($ret, $err) = $uploadMgr->putFile($token, $newFile, $files);
- $r['status'] = 0;
- if ($err !== null) {
- $r['status'] = 0;
- $this->error = $err;
- } else{
- $r['status'] = 1;
- $r['url'] = $this->config['endpoint'].$ret['key'];
- }
- return $r;
- }
- /**
- * 带路径文件夹文件上传
- * @param type $path
- * @param type $name
- * @param type $files
- * @param type $ext
- * @return boolean|string
- */
- public function pathFileUpload($path,$name,$files,$ext = array('svga')){
- if (empty($files)) {
- $this->error = '没有上传的文件!';
- return false;
- }
- $file['ext'] = strtolower(pathinfo($name, PATHINFO_EXTENSION));
- if(!in_array($file['ext'], $ext)){
- $this->error = '上传格式发生错误!';
- return false;
- }
- if(!file_exists($files)){
- $this->error = '上传文件不存在!';
- return false;
- }
- $object = $this->mk_str().'.'.$file['ext'];
- $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
- $token = $this->qiniu->uploadToken($this->config['bucket']);
- $uploadMgr = new UploadManager();
- list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
- $r = [];
- $r['status'] = 0;
- if ($err !== null) {
- $r['status'] = 0;
- $this->error = $err;
- } else{
- $r['status'] = 1;
- $r['url'] = $this->config['endpoint'].$ret['key'];
- }
- return $r;
- }
- /**
- * 持久化操作
- * @param $key
- * @param $savekey
- * @param $daima
- */
- public function Fop($key,$savekey,$type = 320){
- if($type == 320){
- $daima = 'imageView2/1/w/320/h/320/q/100|imageslim';
- }
- $pipeline = 'mg_51';
- $pfop = new \Qiniu\Processing\PersistentFop($this->qiniu,$this->config['bucket'],$pipeline,"http://www.mmgg.com/api", false);
- $fops = $daima."|saveas/" . \Qiniu\base64_urlSafeEncode($this->config['bucket'] . ":".$savekey);
- list($id, $err) = $pfop->execute($key, $fops);
- }
- /**
- * @param $obj
- */
- public function watermark($obj){
- $jh_ar = array('jpg','png','gif');
- $expAr = explode('.',$obj);
- $img = '';
- $exp = end($expAr);
- if(in_array($exp, $jh_ar)) {
- $fileAr = explode('/',$obj);
- $fileName = end($fileAr);
- $imgPath = str_replace($fileName,'',$obj);
- $img = md5('watermark'.$fileName).'.'.$exp;
- //进行持久化操作
- $this->FopSyin($obj, $imgPath.$img);
- }
- return $imgPath.$img;
- }
- //
- /**
- * 持久化操作
- * @param $key
- * @param $savekey
- * @param $daima
- */
- public function FopSyin($key,$savekey){
- $daima = "imageView2/0/interlace/1/q/100|watermark/1/image/aHR0cHM6Ly9pbWcuZ3VhaW1laWRhLmNvbS9zeWluX2xvZ2luLnBuZw==/dissolve/64/gravity/SouthEast/dx/10/dy/10|imageslim";
- $pipeline = 'task0013';
- $pfop = new \Qiniu\Processing\PersistentFop($this->qiniu,$this->config['bucket'],$pipeline,"http://www.guaimeida.com/", false);
- $fops = $daima."|saveas/" . \Qiniu\base64_urlSafeEncode($this->config['bucket'] . ":".$savekey);
- list($id, $err) = $pfop->execute($key, $fops);
- var_dump($err);
- }
- private function mk_str(){
- $d = sha1((string)uniqid((string)mt_rand(),true));
- $str = '';
- for($i = 0;$i<strlen($d);$i++){
- $v = $d[$i];
- if(ctype_alpha($d)){
- $rnd_true = rand(0, 1);
- if($rnd_true){
- $v = strtolower($d[$i]);
- } else {
- $v = strtoupper($d[$i]);
- }
- }
- $str .= $v;
- }
- //防止内存加载不变
- $time = substr((string)time(),0,-6);
- $tarray = array('A','B','C','D','E','F','G','H','M','T');
- $vstr = '';
- for($i = 0;$i<strlen($time);$i++){
- if($time[$i] % 2 == 0){
- $vstr .= 'X';
- } else {
- $vstr .= $tarray[rand(0, 9)];
- }
- }
- $str .= '-'.$vstr;
- return $str;
- }
- /**
- * 获取错误
- * @return type
- */
- public function getError(){
- return $this->error;
- }
- /**
- * 写入日志
- * @param type $word
- * @param type $name
- */
- private function log_result($word, $name) {
- $fp = fopen($name . "_log.txt", "a");
- flock($fp, LOCK_EX);
- fwrite($fp, $word . "\n");
- flock($fp, LOCK_UN);
- fclose($fp);
- }
- }
|