上传文件

8 篇文章 0 订阅

use yii\web\UploadedFile;

$uploadFile = UploadedFile::getInstance($model, 'url');
// 连接aws服务器
                $client = S3Client::factory([
                            'credentials' => [
                                'key' => Yii::$app->params['awsKey'],
                                'secret' => Yii::$app->params['awsSecret'],
                            ]
                ]);
                /**
                 * 按时间生成目录
                 * 获取文件后缀
                 * 生成文件
                 */
                $filename = explode(".", $uploadFile->name);
                $data = date("Y", time()) . '/' . date("m", time()) . '/' . date("d", time()) . '/';
                $save_path = "../../uploads/optionalbubble/$data";
                //创建相应了、目录
                if (!file_exists($save_path)) {
                    mkdir($save_path, 0777, true);
                }
                $namef = md5(uniqid(rand(), true)) . '.' . end($filename);
                $savePath = $save_path . $namef;
                $img_full_path = str_replace("/backend../..", "", Yii::$app->basePath . $save_path . $namef);
                $uploadFile->saveAs($savePath);
                //生成缩略图后的名字
                $thumbPath = $save_path . 'thumb_' . $namef;
                //生成缩略图
                /*
                 * param1 $savePath 图片路径
                 * param2 80 图片宽度(数值随意填写)
                 * param3 80 图片高度(数值随意填写)
                 * param4 $thumbPath 缩略图片路径
                 * param5 1 缩略标签 (0 | 1 | -1)三种数值
                 * param6 0.5 缩放比例 ( 0 < param6 < 1 | param6 > 1 )
                 */
                $Yuthumb = new Yuthumb($savePath, 80, 80, $thumbPath, 1, 0.5);
                $img_path_thumb = str_replace("/backend../..", "", Yii::$app->basePath . $Yuthumb->des_file);
                // 上传到aws
                $awsBucket = Yii::$app->params['awsBucket']['pic'];
                $resultAws = $client->putObject([
                    'Bucket' => $awsBucket,
                    'Key' => $namef,
                    'SourceFile' => $img_full_path,
                    'ACL' => 'public-read'
                ]);
                //缩略图上传
                $resultAwsThumb = $client->putObject([
                    'Bucket' => $awsBucket,
                    'Key' => 'thumb_' . $namef,
                    'SourceFile' => $img_path_thumb,
                    'ACL' => 'public-read'
                ]);
                //上传到OSS存储服务
                $ossClient = OSSClient::factory([
                            'AccessKeyId' => Yii::$app->params['keyId'],
                            'AccessKeySecret' => Yii::$app->params['keySecret'],
                            'Endpoint' => Yii::$app->params['Endpoint'],
                ]);
                $key = $namef;  //ob的名字
                $keyThumb = 'thumb_' . $namef;
                 $ossClient->putObject([
                    'Bucket' => Yii::$app->params['classPicBucket'],
                    'Key' => $key,
                    'Content' => fopen($img_full_path, 'r'),
                    'ContentLength' => filesize($img_full_path)
                ]);
                 $ossClient->putObject([
                    'Bucket' => Yii::$app->params['classPicBucket'],
                    'Key' => $keyThumb,
                    'Content' => fopen($img_path_thumb, 'r'),
                    'ContentLength' => filesize($img_path_thumb)
                ]);
                $ossUrl = "http://" . Yii::$app->params['classPicBucket'] . ".oss-cn-qingdao.aliyuncs.com/" . $key;
                $ossThumbUrl = "http://" . Yii::$app->params['classPicBucket'] . ".oss-cn-qingdao.aliyuncs.com/" . $keyThumb;
                //缩略图路径名称
                $thumbArray = [
                    'en' => $resultAwsThumb['ObjectURL'],
                    'cn' => $ossThumbUrl
                ];
                $bubblethumb = serialize($thumbArray);
                //原图片
                $imgArray = [
                    'en' => $resultAws['ObjectURL'],
                    'cn' => $ossUrl
                ];
                $bubbleurl = serialize($imgArray);
                @unlink($img_full_path);
                $result=array();
                $result[0]=$bubblethumb;
                $result[1]=$bubbleurl;
                return $result;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值