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]; } }