正在显示
2 个修改的文件
包含
29 行增加
和
21 行删除
此 diff 太大无法显示。
| @@ -188,7 +188,7 @@ class NewsLogic extends BaseLogic | @@ -188,7 +188,7 @@ class NewsLogic extends BaseLogic | ||
| 188 | $param['image'] = str_replace_url($param['image'] ?? ''); | 188 | $param['image'] = str_replace_url($param['image'] ?? ''); |
| 189 | } | 189 | } |
| 190 | if(isset($this->param['text'])){ | 190 | if(isset($this->param['text'])){ |
| 191 | - | 191 | + $this->param['text'] = $this->handleText($this->param['text']); |
| 192 | } | 192 | } |
| 193 | if(isset($this->param['id'])){ | 193 | if(isset($this->param['id'])){ |
| 194 | $param['operator_id'] = $this->user['id']; | 194 | $param['operator_id'] = $this->user['id']; |
| @@ -212,34 +212,42 @@ class NewsLogic extends BaseLogic | @@ -212,34 +212,42 @@ class NewsLogic extends BaseLogic | ||
| 212 | return $this->success($param); | 212 | return $this->success($param); |
| 213 | } | 213 | } |
| 214 | 214 | ||
| 215 | + public function handleText($text){ | ||
| 216 | + $pattern = '/<img\s+[^>]*src=["\']([^"\']+)["\']/i'; | ||
| 217 | + $matches = []; | ||
| 218 | + preg_match_all($pattern, $text, $matches); | ||
| 219 | + $updatedSources = $this->saveBase64Images($matches[1]); | ||
| 220 | + if(!empty($updatedSources)){ | ||
| 221 | + foreach($updatedSources as $k => $v){ | ||
| 222 | + $text = str_replace($v, $k, $text); | ||
| 223 | + } | ||
| 224 | + } | ||
| 225 | + return $this->success($text); | ||
| 226 | + } | ||
| 227 | + | ||
| 215 | /** | 228 | /** |
| 216 | - * @remark :处理text | ||
| 217 | - * @name :processBase64Image | 229 | + * @remark :解码图片 |
| 230 | + * @name :saveBase64Images | ||
| 218 | * @author :lyh | 231 | * @author :lyh |
| 219 | * @method :post | 232 | * @method :post |
| 220 | - * @time :2024/11/7 16:18 | 233 | + * @time :2024/11/7 16:52 |
| 221 | */ | 234 | */ |
| 222 | - public function processImage($inputString) { | ||
| 223 | - // 正则匹配Base64图片流文件 | ||
| 224 | - $pattern = '/data:image\/(png|jpg|jpeg|gif);base64,([a-zA-Z0-9\/+\=]+)/'; | ||
| 225 | - $outputString = $inputString; | ||
| 226 | - // 找到所有符合条件的Base64图片流 | ||
| 227 | - if (preg_match_all($pattern, $inputString, $matches)) { | ||
| 228 | - // 遍历匹配的Base64流文件 | ||
| 229 | - foreach ($matches[0] as $index => $base64String) { | ||
| 230 | - $imageType = $matches[1][$index]; // 图片类型 | ||
| 231 | - $base64Data = $matches[2][$index]; // Base64数据 | ||
| 232 | - // 解码Base64图片 | 235 | + public function saveBase64Images($imageSources) |
| 236 | + { | ||
| 237 | + $updatedSources = []; | ||
| 238 | + foreach ($imageSources as $src) { | ||
| 239 | + if (preg_match('/^data:image\/(png|jpg|jpeg|gif);base64,/', $src, $match)) { | ||
| 240 | + $imageType = $match[1]; // Image type (png, jpg, etc.) | ||
| 241 | + $base64Data = preg_replace('/^data:image\/(png|jpg|jpeg|gif);base64,/', '', $src); | ||
| 233 | $decodedData = base64_decode($base64Data); | 242 | $decodedData = base64_decode($base64Data); |
| 234 | - return $decodedData; | ||
| 235 | if ($decodedData === false) { | 243 | if ($decodedData === false) { |
| 236 | continue; // 如果解码失败,则跳过 | 244 | continue; // 如果解码失败,则跳过 |
| 237 | } | 245 | } |
| 238 | - $imageUrl = $this->manager_uploads($decodedData); | ||
| 239 | - $outputString = str_replace($base64String, $imageUrl, $outputString); | 246 | + $imageUrl = $this->manager_uploads($decodedData,$imageType); |
| 247 | + $updatedSources[$imageUrl] = $src; | ||
| 240 | } | 248 | } |
| 241 | } | 249 | } |
| 242 | - return $outputString; | 250 | + return $this->success($updatedSources); |
| 243 | } | 251 | } |
| 244 | /** | 252 | /** |
| 245 | * @remark :自调用 | 253 | * @remark :自调用 |
| @@ -248,11 +256,11 @@ class NewsLogic extends BaseLogic | @@ -248,11 +256,11 @@ class NewsLogic extends BaseLogic | ||
| 248 | * @method :post | 256 | * @method :post |
| 249 | * @time :2024/11/7 11:00 | 257 | * @time :2024/11/7 11:00 |
| 250 | */ | 258 | */ |
| 251 | - public function manager_uploads($files){ | 259 | + public function manager_uploads($files,$type = 'png'){ |
| 252 | $this->uploads = config('upload.default_image'); | 260 | $this->uploads = config('upload.default_image'); |
| 253 | $path = $this->uploads['path_b'].'/'.($this->user['project_id'] ?? 1618).'/image_news/'.date('Y-m'); | 261 | $path = $this->uploads['path_b'].'/'.($this->user['project_id'] ?? 1618).'/image_news/'.date('Y-m'); |
| 254 | $cosService = new CosService(); | 262 | $cosService = new CosService(); |
| 255 | - $fileName = md5(uniqid() . '_' . time() . '.' . ($this->user['project_id'] ?? 1618).rand(1,10000)).'png'; | 263 | + $fileName = md5(uniqid() . '_' . time() . '.' . ($this->user['project_id'] ?? 1618).rand(1,10000)) . '.' .$type; |
| 256 | return getImageUrl($cosService->uploadFile($files,$path,$fileName)); | 264 | return getImageUrl($cosService->uploadFile($files,$path,$fileName)); |
| 257 | } | 265 | } |
| 258 | /** | 266 | /** |
-
请 注册 或 登录 后发表评论