• 七牛云上传图片


    Qiniu.php
    <?php
    
    namespace app\common\service;
    
    use Qiniu\Auth;
    use Qiniu\Storage\UploadManager;
    
    class Qiniu
    {
        const QINIU_URL = '';
        const ACCESS_KEY = '';
        const SECRET_KEY = '';
        const BUCKET = '';
    
        /**
         * 获取token
         * @return string
         */
        public static function createToken()
        {
            // 构建鉴权对象
            $auth = new Auth(self::ACCESS_KEY, self::SECRET_KEY);
    
            // 生成上传token
            $token = $auth->uploadToken(self::BUCKET, $key = null, 3600, ['mimeLimit' => 'image/*'], true);
    
            // 返回token
            return $token;
        }
    
        /*
         * 上传图片
         * */
        public static function uploads($fileName = 'file')
        {
            $file = request()->file($fileName);
            // 要上传图片的本地路径
            $filePath = $file->getRealPath();
            $ext      = $file->getOriginalExtension(); // 扩展名
    
            // 上传到七牛后保存的文件名
            $key = 'crm/'.substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
            // 需要填写你的 Access Key 和 Secret Key
            $accessKey = self::ACCESS_KEY;
            $secretKey = self::SECRET_KEY;
            // 构建鉴权对象
            $auth = new Auth($accessKey, $secretKey);
            // 要上传的空间
            $bucket = self::BUCKET;
            $token = $auth->uploadToken($bucket);
            // 初始化 UploadManager 对象并进行文件的上传
            $uploadMgr = new UploadManager();
            // 调用 UploadManager 的 putFile 方法进行文件的上传
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
            if ($err !== null) {
                return ["err"=>1,"msg"=>$err,"data"=>""];
            } else {
                //返回图片的完整URL
                return ["err"=>0,"msg"=>"上传完成","data"=>(self::QINIU_URL.$ret['key'])];
            }
        }
    
        /**
         * 上传base64位图片到七牛云
         * $image base64位图片流
         */
        public static function uploadPicBase64($image, $type=null)
        {
            // 去除base64,
            $num = strpos($image,',');
            $image = substr($image, $num+1);
            $str = isset($image)?$image:false;
    
            //生成图片key
            $rand = rand(1111,9999);
            $now = time();
            $name = 'merchants/'.$now.$rand;
            if ($type) {
                $name = $name.'.'.$type;
            }
    
            $Key = base64_encode($name);
    
            $upToken = self::createToken();
    
            if($str)
            {
                $qiniu = self::phpCurlImg("http://upload-z1.qiniu.com/putb64/-1/key/".$Key,$str,$upToken);
                //upload.qiniup.com 上传域名适用于华东空间。华北空间使用 upload-z1.qiniu.com,华南空间使用 upload-z2.qiniu.com,北美空间使用 upload-na0.qiniu.com。
                $qiniuArr = json_decode($qiniu,true);
    
                if(!empty($qiniuArr['key'])&&$qiniuArr['key']==$name)
                {
                    return self::QINIU_URL.$qiniuArr['key'];
                }
                else
                {
                    return false;
                }
            }
            return false;
        }
    
        //七牛base64上传方法
        public static function phpCurlImg($remote_server,$post_string,$upToken)
        {
            $headers = array();
            $headers[] = 'Content-Type:application/octet-stream';
            $headers[] = 'Authorization:UpToken '.$upToken;
            $headers[] = 'Host: upload-z1.qiniu.com';
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$remote_server);
            curl_setopt($ch, CURLOPT_HTTPHEADER ,$headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            $data = curl_exec($ch);
            curl_close($ch);
            return $data;
        }
    
        /*
        * 删除图片
        * */
        public static function deletePicture()
        {
            $accessKey = self::ACCESS_KEY;
            $secretKey = self::SECRET_KEY;
            $bucket = self::BUCKET;
    
            $key = request()->param('img');
            $auth = new Auth($accessKey, $secretKey);
            $config = new \Qiniu\Config();
            $bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
            $err = $bucketManager->delete($bucket, $key);
            if ($err=='') {
                return true;
            }else{
                return false;
            }
        }
    
    }

    普通post上传图片

     $qiuniu = (new Qiniu())::uploads();

    base64格式上传

     $image_data = fread(fopen($file_path, 'r'), filesize($file_path));
            $base64_image = ';base64,'.chunk_split(base64_encode($image_data));
            $qiuniu = (new Qiniu())::uploadPicBase64($base64_image);
            //上传成功,删除本地文件
            if ($qiuniu) {
                $url_img_url = dirname(dirname(app_path())).'/public/'.$file_path;     //本地图片绝对地址
                $url_img_path = iconv('utf-8', 'gbk', $url_img_url);
                if (PATH_SEPARATOR == ':') {
                    //linux
                    unlink($url_img_url);
                } else {
                    //Windows
                    unlink($url_img_path);
                }
            }
            return ['status' => true, 'url' => $qiuniu];
  • 相关阅读:
    网络卖家自曝黑幕 “信用刷手”欺骗你 狼人:
    卡巴斯基:2009年恶意软件发展情况将持续恶化 狼人:
    微软证实云计算平台暂时中断 显现安全弊端 狼人:
    工程目录Maven安装配置及其插件m2e(Eclipse Indigo 和 MyEclipse8.5)的安装配置
    文件系统crondsendmailpostdrop导致Linux定期死掉的完整解决过程实录
    汇编语言处理器面向机器的编程语言 : 汇编语言:如果自己要发明一种编程语言,那么需要做些什么事情呢?
    包解密腾讯手机管家ROOT功能分析
    nullnullhdu 2608
    按钮下载Eclipse Color Theme
    控件学习IOS开源项目(1)之RatingView星级评论控件学习
  • 原文地址:https://www.cnblogs.com/bkhdd/p/15923300.html
Copyright © 2020-2023  润新知