作者 lyh

hx

... ... @@ -10,6 +10,7 @@ use App\Models\News\NewsCategory;
use App\Models\News\NewsCategory as NewsCategoryModel;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BTemplate;
use App\Services\CosService;
use Illuminate\Support\Facades\DB;
use mysql_xdevapi\Exception;
... ... @@ -186,7 +187,9 @@ class NewsLogic extends BaseLogic
if(isset($param['image'])){
$param['image'] = str_replace_url($param['image'] ?? '');
}
if(isset($this->param['text'])){
}
if(isset($this->param['id'])){
$param['operator_id'] = $this->user['id'];
}else{
... ... @@ -209,6 +212,49 @@ class NewsLogic extends BaseLogic
return $this->success($param);
}
public function processBase64Image($inputString) {
// 正则匹配Base64图片流文件
$pattern = '/data:image\/(png|jpg|jpeg|gif);base64,([a-zA-Z0-9\/+\=]+)/';
$outputString = $inputString;
// 找到所有符合条件的Base64图片流
if (preg_match_all($pattern, $inputString, $matches)) {
// 遍历匹配的Base64流文件
foreach ($matches[0] as $index => $base64String) {
$imageType = $matches[1][$index]; // 图片类型
$base64Data = $matches[2][$index]; // Base64数据
// 解码Base64图片
$decodedData = base64_decode($base64Data);
if ($decodedData === false) {
continue; // 如果解码失败,则跳过
}
// 设置文件名
$outputFile = 'images/' . uniqid() . '.' . $imageType;
$filePath = __DIR__ . '/' . $outputFile;
// 保存图片到服务器
if (file_put_contents($filePath, $decodedData)) {
// 生成图片URL
$imageUrl = 'http://your-domain.com/' . $outputFile;
// 替换原字符串中的Base64流为图片链接
$outputString = str_replace($base64String, $imageUrl, $outputString);
}
}
}
return $outputString;
}
/**
* @remark :自调用
* @name :manager_uploads
* @author :lyh
* @method :post
* @time :2024/11/7 11:00
*/
public function manager_uploads($files){
$this->uploads = config('upload.default_image');
$path = $this->uploads['path_b'].'/'.$this->user['project_id'].'/image_news/'.date('Y-m');
$cosService = new CosService();
$fileName = md5(uniqid() . '_' . time() . '.' . $this->user['project_id'].rand(1,10000));
return $cosService->uploadFile($files,$path,$fileName);
}
/**
* @remark :获取分类(字符串)
* @name :getLastCategory
... ...
... ... @@ -5,6 +5,7 @@ namespace App\Services;
use App\Exceptions\InquiryFilterException;
use App\Models\File\ImageSetting;
use App\Utils\LogUtils;
use Illuminate\Support\Str;
use Qcloud\Cos\Client;
/**
* @remark :对象存储cos
... ... @@ -34,7 +35,19 @@ class CosService
],
]);
$key = $path.'/'.$filename;
$Body = $binary ? $files : fopen($files->getRealPath(), 'r');
// 判断是否为 Base64 编码的图片流文件
if (Str::startsWith($files, 'data:image')) {
// 分离 Base64 头部和数据部分
[$meta, $base64Data] = explode(',', $files);
// 解码 Base64 数据
$Body = base64_decode($base64Data);
if ($Body === false) {
throw new \Exception("Base64 数据解码失败");
}
} else {
// 如果不是 Base64 流文件,处理为普通文件上传
$Body = $binary ? $files : fopen($files->getRealPath(), 'r');
}
$options = [
'Bucket' => $cos['bucket'],
'Key' => $key,
... ... @@ -53,6 +66,7 @@ class CosService
], true);
}
$res = $cosClient->putObject($options);
@file_put_contents(storage_path('logs/lyh_error.log'), var_export($res, true) . PHP_EOL, FILE_APPEND);
return $key;
}
... ...