最近使用php的hyperf框架 , 写了上传的服务类, 并使用了简单工厂模式和反射类来进行分发处理, 并使用抽象类对上传类进行了限制,统一参数,统一返回
initClass($type);
return $classObj?->uploadToOss($file);
}
/**
* 通过反射获取类的实例或者类的名称
*
* @authors: Msy
* @Created-Time: 2022/12/7 13:48
* @param $type
* @param $params
* @param $needInstance 是否实例化 , 如果是false,主要用于静态方法
* @return bool|object|string
* @throws \ReflectionException
*/
protected function initClass($type, $params = [], $needInstance = true)
{
$stats = $this->uploadClassStat();
if (!array_key_exists($type, $stats)) {
return false;
}
$className = $stats[$type];
return $needInstance ? (new \ReflectionClass($className))->newInstanceArgs($params) : $className;
}
/**
* 获取上传类映射
*
* @authors: Msy
* @Created-Time: 2022/12/7 10:53
* @return string[]
*/
protected function uploadClassStat()
{
return [
'huawei' => "App\Services\Oss\HuaWeiObs",
'aliyun' => "App\Services\Oss\AliOss",
];
}
/**
* 解密base64图片
*
* @authors: Msy
* @Created-Time: 2023/4/13 13:54
* @param $base64_image_content
* @param $type
* @param $address oss位置,选填
* @return mixed|string|null
* @throws \ReflectionException
*/
public function imgBase64Decode($base64_image_content = '', $address = '', $type = 'aliyun')
{
if (!$base64_image_content) return '';
$classObj = $this->initClass($type);
if (empty($base64_image_content)) {
return '';
}
$match = preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result);
if (!$match) {
return $base64_image_content;
}
$base64_image = str_replace($result[1], '', $base64_image_content);
$file_content = base64_decode($base64_image);
$file_type = $result[2];
return $classObj?->uploadBaseImgToOss($file_content, $file_type, $address);
}
}
getExtension();
if (!in_array(strtolower($ext), $allowed_extensions)) {
throw new ApiException(
message: config('statuscode.err_msg.not_supported_format_file'),
code: config('statuscode.err_code.not_supported_format_file'),
remind: 1,
);
}
//检测文件大小
if ($file->getSize() > $maxSize * 1024 * 1024) {
throw new ApiException(
message: config('statuscode.err_msg.file_too_large'),
code: config('statuscode.err_code.file_too_large'),
remind: 1,
);
}
$accessKeyId = config('thirtparty.aliyun.accessKeyID');
$accessKeySecret = config('thirtparty.aliyun.accessKeySecret');
$endpoint = config('oss.aliyun.endpoint');
$bucket = config('oss.aliyun.bucket');
$newFile = 'ddgcjx' . "/" . date('Ym') . "/" . uniqid() . rand(10, 99) . "." . $file->getExtension();
$filePath = $file->getPathname();
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
// 上传图片增加水印
// $style = "image/watermark,g_nw,t_90,size_100,type_ZmFuZ3poZW5naGVpdGk,x_10,y_10,P_10,image_bG9nby5wbmc_eC1vc3MtcHJvY2Vzcz1pbWFnZS9yZXNpemUsUF8xNQ==";
// 上传时可以设置相关的headers,例如设置访问权限为private、自定义元信息等。
$options = array(
OssClient::OSS_HEADERS => array(
'x-oss-object-acl' => 'public-read',
),
// OssClient::OSS_PROCESS => $style,
);
$resp = $ossClient->uploadFile($bucket, $newFile, $filePath, $options);
if ($resp['info']['http_code'] == 200) { //$resp ['HttpStatusCode']状态码 200就是成功
$res = $resp['info']['url'];
// OSS 访问域名
$url = config('oss.aliyun.url');
if (!$url) {
return $res;
} else {
return str_ireplace($bucket . '.' . $endpoint, $url, $res);
}
} else {
throw new ApiException(
message: config('statuscode.err_msg.fail'),
code: config('statuscode.err_code.fail'),
remind: 1,
);
}
}
/**
* 上传base64图片到oss
*
* @authors: Msy
* @Created-Time: 2023/3/4 20:10
* @param $baseImg
* @param $ext
* @param string $address
* @return string
*/
public function uploadBaseImgToOss($baseImg, $ext, string $address = ''): string
{
$accessKeyId = config('thirtparty.aliyun.accessKeyID');
$accessKeySecret = config('thirtparty.aliyun.accessKeySecret');
$endpoint = config('oss.aliyun.endpoint');
$bucket = config('oss.aliyun.bucket');
if ($address) {
$newFile = $address . '.' . $ext;
} else {
$newFile = 'ddgcjx' . "/" . date('Ym') . "/" . uniqid() . rand(10, 99) . "." . $ext;
}
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$options = array(
OssClient::OSS_HEADERS => array(
'x-oss-object-acl' => 'public-read',
),
// OssClient::OSS_PROCESS => $style,
);
// $resp = $ossClient->uploadFile($bucket, $newFile, $baseImg, $options);
$resp = $ossClient->putObject($bucket, $newFile, $baseImg, $options);
if ($resp['info']['http_code'] == 200) { //$resp ['HttpStatusCode']状态码 200就是成功
$res = $resp['info']['url'];
// OSS 访问域名
$url = config('oss.aliyun.url');
if (!$url) {
return $res;
} else {
return str_ireplace($bucket . '.' . $endpoint, $url, $res);
}
} else {
throw new ApiException(
message: config('statuscode.err_msg.fail'),
code: config('statuscode.err_code.fail'),
remind: 1,
);
}
}
}