作者 lyh

hx

... ... @@ -188,7 +188,7 @@ class NewsLogic extends BaseLogic
$param['image'] = str_replace_url($param['image'] ?? '');
}
if(isset($this->param['text'])){
$this->param['text'] = $this->handleText($this->param['text']);
}
if(isset($this->param['id'])){
$param['operator_id'] = $this->user['id'];
... ... @@ -212,34 +212,42 @@ class NewsLogic extends BaseLogic
return $this->success($param);
}
public function handleText($text){
$pattern = '/<img\s+[^>]*src=["\']([^"\']+)["\']/i';
$matches = [];
preg_match_all($pattern, $text, $matches);
$updatedSources = $this->saveBase64Images($matches[1]);
if(!empty($updatedSources)){
foreach($updatedSources as $k => $v){
$text = str_replace($v, $k, $text);
}
}
return $this->success($text);
}
/**
* @remark :处理text
* @name :processBase64Image
* @remark :解码图片
* @name :saveBase64Images
* @author :lyh
* @method :post
* @time :2024/11/7 16:18
* @time :2024/11/7 16:52
*/
public function processImage($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图片
public function saveBase64Images($imageSources)
{
$updatedSources = [];
foreach ($imageSources as $src) {
if (preg_match('/^data:image\/(png|jpg|jpeg|gif);base64,/', $src, $match)) {
$imageType = $match[1]; // Image type (png, jpg, etc.)
$base64Data = preg_replace('/^data:image\/(png|jpg|jpeg|gif);base64,/', '', $src);
$decodedData = base64_decode($base64Data);
return $decodedData;
if ($decodedData === false) {
continue; // 如果解码失败,则跳过
}
$imageUrl = $this->manager_uploads($decodedData);
$outputString = str_replace($base64String, $imageUrl, $outputString);
$imageUrl = $this->manager_uploads($decodedData,$imageType);
$updatedSources[$imageUrl] = $src;
}
}
return $outputString;
return $this->success($updatedSources);
}
/**
* @remark :自调用
... ... @@ -248,11 +256,11 @@ class NewsLogic extends BaseLogic
* @method :post
* @time :2024/11/7 11:00
*/
public function manager_uploads($files){
public function manager_uploads($files,$type = 'png'){
$this->uploads = config('upload.default_image');
$path = $this->uploads['path_b'].'/'.($this->user['project_id'] ?? 1618).'/image_news/'.date('Y-m');
$cosService = new CosService();
$fileName = md5(uniqid() . '_' . time() . '.' . ($this->user['project_id'] ?? 1618).rand(1,10000)).'png';
$fileName = md5(uniqid() . '_' . time() . '.' . ($this->user['project_id'] ?? 1618).rand(1,10000)) . '.' .$type;
return getImageUrl($cosService->uploadFile($files,$path,$fileName));
}
/**
... ...