diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php
index 8c55b826c..32acf7725 100644
--- a/app/Exceptions/Handler.php
+++ b/app/Exceptions/Handler.php
@@ -96,14 +96,25 @@ class Handler extends ExceptionHandler
$path = $request->path();
// 处理图片
- $pattern = '/^(uploads\/.*\.(png|jpg|jpeg))\/crop\/([^\/]+)$/';
- if (preg_match($pattern, $path, $matches)) {
+ $patternCrop = '/^(uploads\/.*\.(png|jpg|jpeg))\/crop\/([^\/]+)$/';
+ $patternThumb = '/^(uploads\/.*)_thumb\.(png|jpg|jpeg)$/';
+ $matchesCrop = null;
+ $matchesThumb = null;
+ if (preg_match($patternCrop, $path, $matchesCrop) || preg_match($patternThumb, $path, $matchesThumb)) {
// 获取参数
- $file = $matches[1];
- $ext = $matches[2];
- $rules = preg_replace('/\s+/', '', $matches[3]);
- $rules = str_replace(['=', '&'], [':', ','], $rules);
- $rules = explode(',', $rules);
+ if ($matchesCrop) {
+ $file = $matchesCrop[1];
+ $ext = $matchesCrop[2];
+ $rules = preg_replace('/\s+/', '', $matchesCrop[3]);
+ $rules = str_replace(['=', '&'], [':', ','], $rules);
+ $rules = explode(',', $rules);
+ } elseif ($matchesThumb) {
+ $file = $matchesThumb[1];
+ $ext = $matchesThumb[2];
+ $rules = ['percentage:320x0'];
+ } else {
+ return null;
+ }
if (empty($rules)) {
return null;
}
diff --git a/resources/assets/js/functions/common.js b/resources/assets/js/functions/common.js
index b1bd05716..aa106676d 100755
--- a/resources/assets/js/functions/common.js
+++ b/resources/assets/js/functions/common.js
@@ -1199,6 +1199,33 @@ const timezone = require("dayjs/plugin/timezone");
}
return result;
}, {});
+ },
+
+ /**
+ * 从HTML中提取图片参数
+ * @param imgTag
+ * @returns {{original, src: (*|null), width: (number|*), height: (number|*)}}
+ */
+ extractImageParameter(imgTag) {
+ const srcMatch = imgTag.match(/\s+src=(["'])([^'"]*)\1/i);
+ const widthMatch = imgTag.match(/\s+width=(["'])([^'"]*)\1/i);
+ const heightMatch = imgTag.match(/\s+height=(["'])([^'"]*)\1/i);
+ return {
+ src: srcMatch ? srcMatch[2] : null,
+ width: $A.runNum(widthMatch ? widthMatch[2] : 0),
+ height: $A.runNum(heightMatch ? heightMatch[2] : 0),
+ original: imgTag,
+ }
+ },
+
+ /**
+ * 从HTML中提取所有图片参数
+ * @param html
+ * @returns {{original, src: (*|null), width: (number|*), height: (number|*)}[]}
+ */
+ extractImageParameterAll(html) {
+ const imgTags = html.match(/]*?>/g) || [];
+ return imgTags.map(imgTag => this.extractImageParameter(imgTag));
}
});
diff --git a/resources/assets/js/functions/web.js b/resources/assets/js/functions/web.js
index b30e5ef0f..b91e95f99 100755
--- a/resources/assets/js/functions/web.js
+++ b/resources/assets/js/functions/web.js
@@ -249,26 +249,17 @@ import {MarkdownPreview} from "../store/markdown";
text = text.replace(/
]*?>/g, `[${$A.L('动画表情')}]`)
if (imgClassName) {
text = text.replace(/
]*?src="(\S+)"[^>]*?>/g, function (res, src) {
- const widthMatch = res.match("width=\"(\\d+)\""),
- heightMatch = res.match("height=\"(\\d+)\"");
- if (widthMatch && heightMatch) {
- const data = {
- width: parseInt(widthMatch[1]),
- height: parseInt(heightMatch[1]),
- maxSize: 40,
- src
- }
- const ratioExceed = $A.imageRatioExceed(data.width, data.height, 2)
- if (ratioExceed > 0 && /\.(png|jpg|jpeg)$/.test(data.src)) {
- src = $A.thumbRestore(data.src) + `/crop/ratio:${ratioExceed},percentage:80x0`
- if (data.width > data.height) {
- data.width = data.height * ratioExceed;
- } else {
- data.height = data.width * ratioExceed;
- }
- }
- const scale = $A.scaleToScale(data.width, data.height, data.maxSize);
- imgClassName = `${imgClassName}" style="width:${scale.width}px;height:${scale.height}px`
+ const item = $A.extractImageParameter(res);
+ if (item.width && item.height) {
+ const data = $A.imageRatioHandle({
+ src: item.src,
+ width: item.width,
+ height: item.height,
+ crops: {ratio: 2, percentage: '80x0'},
+ scaleSize: 40,
+ })
+ src = data.src
+ imgClassName = `${imgClassName}" style="width:${data.width}px;height:${data.height}px`
}
return `[image:${src}]`
})
@@ -344,44 +335,25 @@ import {MarkdownPreview} from "../store/markdown";
}).join("")
}
// 处理图片显示尺寸
- const array = text.match(/
]*?>/g);
- if (array) {
- const widthReg = new RegExp("width=\"(\\d+)\""),
- heightReg = new RegExp("height=\"(\\d+)\"")
- array.some(res => {
- const widthMatch = res.match(widthReg),
- heightMatch = res.match(heightReg);
- if (widthMatch && heightMatch) {
- const data = {
- res,
- width: parseInt(widthMatch[1]),
- height: parseInt(heightMatch[1]),
- maxSize: res.indexOf("emoticon") > -1 ? 150 : 220, // 跟css中的设置一致
- }
- if (data.maxSize === 220) {
- const ratioExceed = $A.imageRatioExceed(data.width, data.height)
- if (ratioExceed > 0) {
- const srcMatch = res.match(/src=(["'])(([^'"]*)\.(png|jpg|jpeg))\1/);
- if (srcMatch) {
- data.res = data.res.replace(srcMatch[2], $A.thumbRestore(srcMatch[2]) + `/crop/ratio:${ratioExceed},percentage:320x0`)
- if (data.width > data.height) {
- data.width = data.height * ratioExceed;
- } else {
- data.height = data.width * ratioExceed;
- }
- }
- }
- }
- const scale = $A.scaleToScale(data.width, data.height, data.maxSize);
- const value = data.res
- .replace(widthReg, `original-width="${data.width}"`)
- .replace(heightReg, `original-height="${data.height}" style="width:${scale.width}px;height:${scale.height}px"`)
- text = text.replace(res, value)
- } else {
- text = text.replace(res, `