123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
- namespace app\oss\service;
- use app\common\service\Oss\Bucket;
- use app\common\service\Oss\Object;
- use Exception;
- use OSS\OssClient;
- use think\Config;
- class OssServiceV2 {
- static private $bucket;
- static private $object;
- public function __construct($bucketName = '') {
- self::$bucket = new Bucket($bucketName);
- self::$object = new Object($bucketName);
- }
- /**
- * 文件上传到阿里云
- * @param $filePath 保存在阿里云的路径和名称
- * @param $content 文件流
- * @param bool $isEncode 是否需要加密
- * @return bool|string
- */
- public function fileUpload($filePath, $content, $isEncode = true){
- try {
- $ossRes = self::$object->putObject($filePath, $content);
- if ($ossRes) {
- return $isEncode === true ? rawurlencode($filePath) : $filePath;
- }
- return false;
- } catch (Exception $ex) {
- throw new \RuntimeException('OSS ERROR MSG:' . $ex->getMessage());
- }
- }
- /**
- * 删除阿里云上的文件
- * @param $filePath 保存在阿里云的路径和名称
- * @return bool
- */
- public function fileDelete($filePath) {
- return self::$object->deleteObject($filePath);
- }
- /**
- * acl
- * @param $object 相对地址
- * @param $acl ['default', 'private', 'public-read', 'public-read-write']
- * @return bool
- */
- public function putObjectAcl($object, $acl='private') {
- return self::$object->putObjectAcl($object, $acl);
- }
- /**
- * 下载接口
- * @param $filePath
- * @return null
- */
- public function downFile($filePath) {
- return self::$object->getObject($filePath);
- }
- /**
- * 图片在阿里云上存在
- * @param $imagePath
- * @return bool
- */
- public function fileExist($imagePath) {
- return self::$object->doesObjectExist($imagePath);
- }
- /**
- * 图片信息
- * @param $imagePath
- * @param $ossDomain
- * @return mixed
- */
- public function imageInfo($imagePath, $ossDomain) {
- $imageInfo = file_get_contents($ossDomain . $imagePath . '@info');
- return json_decode($imageInfo, true);
- }
- public function bucketList() {
- return self::$bucket->listBuckets();
- }
- /**
- * 获取代签名的URI
- * @param array $params = [
- * 'object' => 'xxx',
- * 'bucket' => 'xxx',
- * 'isIntranet' => true,
- * 'crop' => [
- * 'x' => 1,
- * 'y' => 2,
- * 'w' => 3
- * ],
- * 'snapshot' => [
- * 't' => 1,
- * 'f' => 2,
- * 'w' => 3
- * ]
- * ]
- * @return string
- */
- public function getSignUrl(array $params) {
- $fileExist = $this->fileExist($params['object']);
- if (!$fileExist) {
- throw new \RuntimeException('未找到该文件: ' . $params['object']);
- }
- $isIntranet = isset($params['isIntranet']) ?$params['isIntranet']:true;
- if ((bool)$isIntranet !== true) {
- Config::set('ALIYUN_OSS_ENDPOINT_DOMAIN', Config::get('ALIYUN_OSS_ENDPOINT'));
- }
- $object = new Object($params['bucket']);
- $ossClientObj = $object->getOssClient();
- $ossClientObj->setUseSSL(true);
- $timeout = 3600;
- $option = '';
- // 图片处理
- if (!empty($params['resize'])) {
- $option = 'image/resize,m_lfit,h_' . $params['resize']['h'] . ',w_' . $params['resize']['w']; //图片缩放
- } else if (!empty($params['crop'])) {// 图片切割
- $option = 'image/crop';
- $keys = ['x', 'y', 'w', 'h', 'g'];
- foreach ($keys as $key) {
- if(!empty($params['crop'][$key])){
- $option .= ',' . $key . '_' . $params['crop'][$key];
- }
- }
- } else if (!empty($params['snapshot'])) {// 视频截帧
- $option = 'video/snapshot';
- //t-时间 f-格式 m-模式
- $keys = ['t', 'f', 'w', 'h', 'm'];
- foreach ($keys as $key) {
- if(!empty($params['snapshot'][$key])){
- $option .= ',' . $key . '_' . $params['snapshot'][$key];
- }
- }
- }
- $url = $ossClientObj->signUrl($params['bucket'], $params['object'], $timeout, 'GET', !empty($option) ? [OssClient::OSS_PROCESS => $option] : null);
- if(empty($params['url'])){
- return $url;
- }
- return preg_replace('#^https?:\/\/[^\/]+#', rtrim($params['url'], '\/'), $url);
- }
- /**
- * 批量获取签名
- * @param array $data = [['bucket' => xxx, 'object' => xxx],['bucket' => xxx, 'object => xxx]]
- * @return array
- */
- public function getMultiSignUrl(array $data){
- foreach ($data as $key => $dataItem) {
- $data[$key]['newUrl'] = $this->getSignUrl($dataItem);
- }
- return $data;
- }
- /**
- * 获取图片信息
- * @param array
- * @return array 图片信息
- */
- public function getPublicImageInfo($imageUrl){
- $imageInfoUrl = $imageUrl . '?x-oss-process=image/info';
- //获取oss返回的json对象
- return json_decode(file_get_contents($imageInfoUrl), true);
- }
- /**
- * 拼接成正方形
- * @param array $param
- * @return string
- */
- public function getProcessImageUrl(array $param) {
- // 获取图片信息
- $imageUrl = 'http://' . $param['bucket'] . '.' . $param['endpoint'] . $param['path'];
- // 取图片最短的边作为正方形的边长
- if (!empty($param['process']['crop']['shape']) && 'square' === $param['process']['crop']['shape']) {
- $imageInfo = $this->getPublicImageInfo($imageUrl);
- $height = $imageInfo['ImageHeight']['value'];
- $width = $imageInfo['ImageWidth']['value'];
- $param['process']['crop']['h'] = $height > $width ? $width : $height; //高
- $param['process']['crop']['w'] = $height > $width ? $width : $height; //宽
- }
- $processFunc = current(array_keys($param['process'])); // 获取方法(crop裁剪 resize缩放)
- switch ($processFunc) {
- case 'crop':
- $imageUrl .= '?x-oss-process=image/' . $processFunc;
- $keys = ['x', 'y', 'w', 'h', 'g'];
- foreach ($keys as $key) {
- if(!empty($param['process'][$processFunc][$key])){
- $imageUrl .= ',' . $key . '_' . $param['process'][$processFunc][$key];
- }
- }
- break;
- case 'resize':
- // 暂时不写谁用谁加
- break;
- }
- return $imageUrl;
- }
- /**
- * 删除oss图片
- * @param $url $url为完整路径 http://www......
- */
- public function checkAndDelOssRemotePic($url = ''){
- if (empty($url)) {
- return;
- }
- //获取该文件的路径 去除掉 http网址
- $path = substr(parse_url($url, PHP_URL_PATH), 1);
- //判断该文件是否存在
- if ($this->fileExist($path)) {
- $this->fileDelete($path);
- }
- }
- /**
- * @param $upType
- * @param array $params
- * @param $fileName
- * @param $ext
- * @return array
- */
- public function getFileNameAndRealContent($upType, array $params, $fileName, $ext){
- if ($upType === 'base64') {//base64上传方式
- $content = isset($params['content']) ?$params['content']: '';
- $rule = "/^(data:\s*image\/(\w+);base64,)/";
- if (preg_match($rule, $content, $match)) {
- $content = preg_replace($rule, '', $content);
- if (!empty($fileName) && empty($ext)) {
- $dataImages = [
- 'gif' => 'gif',
- 'png' => 'png',
- 'jpeg' => 'jpg',
- 'jpg' => 'jpg',
- 'x-icon' => 'ico',
- ];
- $ext = $dataImages[$match[2]];
- $fileName = rtrim($fileName, '.') . '.' . $ext;
- }
- }
- $realContent = base64_decode($content);
- } else {//普通文件上传方式
- //$content = $this->IJ('content', false, '内容不能为空');
- $content = isset($_FILES['content']) ?$_FILES['content']:[];
- if (empty($content)) {
- throw new \RuntimeException('请选择要上传的文件');
- }
- if (!empty($fileName) && empty($ext)) {
- $imgType = (string)$content['type'];
- $imageTypes = [
- 'image/gif' => 'gif',
- 'image/jng' => 'png',
- 'image/jpeg' => 'jpg',
- ];
- if (!empty($imgType) && in_array($imgType, $imageTypes, true)) {
- $ext = $imageTypes[$imgType];
- } else if (!empty($content['name'])) {
- $ext = pathinfo($content['name'], PATHINFO_EXTENSION);
- }
- $fileName = rtrim($fileName, '.') . (!empty($ext) ? '.' . $ext : '');
- }
- $realContent = $content ? file_get_contents($content['tmp_name']) : null;
- }
- return [$fileName, $realContent];
- }
- }
|