From 71f13a0b50716b713bff2ed5a827660dfc3caf1e Mon Sep 17 00:00:00 2001 From: kuaifan Date: Mon, 2 Dec 2024 10:40:55 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E4=B8=8A=E4=BC=A0=E5=9B=BE=E7=89=87=E5=8E=8B=E7=BC=A9?= =?UTF-8?q?=E8=B4=A8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/FileController.php | 2 +- app/Http/Controllers/Api/SystemController.php | 13 ++++++------ app/Http/Controllers/IndexController.php | 1 - app/Ldap/LdapUser.php | 2 +- app/Models/File.php | 2 +- app/Models/ProjectTaskContent.php | 2 +- app/Models/Setting.php | 3 +++ app/Models/WebSocketDialog.php | 4 ++-- app/Models/WebSocketDialogMsg.php | 6 +++--- app/Module/Base.php | 21 +++++++------------ app/Module/Image.php | 20 +++++++++++------- .../setting/components/SystemSetting.vue | 4 ++++ 12 files changed, 42 insertions(+), 38 deletions(-) diff --git a/app/Http/Controllers/Api/FileController.php b/app/Http/Controllers/Api/FileController.php index b0537542a..e8067515e 100755 --- a/app/Http/Controllers/Api/FileController.php +++ b/app/Http/Controllers/Api/FileController.php @@ -553,7 +553,7 @@ class FileController extends AbstractController $tmpPath = "uploads/file/document/" . date("Ym") . "/" . $id . "/attached/"; Base::makeDir(public_path($tmpPath)); $tmpPath .= md5($text) . "." . $matchs[1][$key]; - if (Base::saveContentImage(public_path($tmpPath), base64_decode($text), 90)) { + if (Base::saveContentImage(public_path($tmpPath), base64_decode($text))) { $paramet = getimagesize(public_path($tmpPath)); $data['content'] = str_replace($matchs[0][$key], ' $path, "fileName" => $fileName, "scale" => $scale, - "quality" => 85 + "quality" => true ]); } else { $data = Base::upload([ @@ -955,7 +954,7 @@ class SystemController extends AbstractController "path" => $path, "fileName" => $fileName, "scale" => $scale, - "quality" => 100 + "quality" => true ]); } if (Base::isError($data)) { @@ -1091,7 +1090,7 @@ class SystemController extends AbstractController "image64" => $image64, "path" => $path, "fileName" => $fileName, - "quality" => 85 + "quality" => true ]); } else { $data = Base::upload([ @@ -1099,7 +1098,7 @@ class SystemController extends AbstractController "type" => 'file', "path" => $path, "fileName" => $fileName, - "quality" => 100 + "quality" => true ]); } // diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php index 640025b94..69f08156b 100755 --- a/app/Http/Controllers/IndexController.php +++ b/app/Http/Controllers/IndexController.php @@ -322,7 +322,6 @@ class IndexController extends InvokeController "type" => 'publish', "path" => $draftPath, "fileName" => true, - "quality" => 100 ]); } diff --git a/app/Ldap/LdapUser.php b/app/Ldap/LdapUser.php index 6ca3d3b3d..ba0d1e251 100644 --- a/app/Ldap/LdapUser.php +++ b/app/Ldap/LdapUser.php @@ -146,7 +146,7 @@ class LdapUser extends Model $path = "uploads/user/ldap/"; $file = "{$path}{$user->userid}.jpeg"; Base::makeDir(public_path($path)); - if (Base::saveContentImage(public_path($file), $userimg, 90)) { + if (Base::saveContentImage(public_path($file), $userimg)) { $user->userimg = $file; } } diff --git a/app/Models/File.php b/app/Models/File.php index 4fd5d525b..17cdfa59c 100644 --- a/app/Models/File.php +++ b/app/Models/File.php @@ -251,7 +251,7 @@ class File extends AbstractModel "type" => 'more', "autoThumb" => false, "path" => $path, - "quality" => 100 + "quality" => true ]); if (Base::isError($data)) { throw new ApiException($data['msg']); diff --git a/app/Models/ProjectTaskContent.php b/app/Models/ProjectTaskContent.php index 4087dd671..5f5357ebd 100644 --- a/app/Models/ProjectTaskContent.php +++ b/app/Models/ProjectTaskContent.php @@ -79,7 +79,7 @@ class ProjectTaskContent extends AbstractModel $tmpPath = $path . 'attached/'; Base::makeDir(public_path($tmpPath)); $tmpPath .= md5($text) . "." . $matchs[1][$key]; - if (Base::saveContentImage(public_path($tmpPath), base64_decode($text), 90)) { + if (Base::saveContentImage(public_path($tmpPath), base64_decode($text))) { $paramet = getimagesize(public_path($tmpPath)); $content = str_replace($matchs[0][$key], 'name) { case 'system': $value['system_alias'] = $value['system_alias'] ?: env('APP_NAME'); + $value['image_compress'] = $value['image_compress'] ?: 'open'; + $value['image_quality'] = min(100, max(0, intval($value['image_quality']) ?: 90)); + $value['image_save_local'] = $value['image_save_local'] ?: 'open'; if (!is_array($value['task_default_time']) || count($value['task_default_time']) != 2 || !Timer::isTime($value['task_default_time'][0]) || !Timer::isTime($value['task_default_time'][1])) { $value['task_default_time'] = ['09:00', '18:00']; } diff --git a/app/Models/WebSocketDialog.php b/app/Models/WebSocketDialog.php index b7fca77d0..26c85e9d2 100644 --- a/app/Models/WebSocketDialog.php +++ b/app/Models/WebSocketDialog.php @@ -807,7 +807,7 @@ class WebSocketDialog extends AbstractModel "image64" => $image64, "path" => $path, "fileName" => $fileName, - "quality" => 85 + "quality" => true ]); } else if ($filePath) { Base::makeDir(public_path($path)); @@ -818,7 +818,7 @@ class WebSocketDialog extends AbstractModel "type" => 'more', "path" => $path, "fileName" => $fileName, - "quality" => 100, + "quality" => true, "convertVideo" => true ]); } diff --git a/app/Models/WebSocketDialogMsg.php b/app/Models/WebSocketDialogMsg.php index eb333e0b7..2ff622c27 100644 --- a/app/Models/WebSocketDialogMsg.php +++ b/app/Models/WebSocketDialogMsg.php @@ -760,7 +760,7 @@ class WebSocketDialogMsg extends AbstractModel $imagePath = "uploads/chat/" . date("Ym") . "/" . $dialog_id . "/"; Base::makeDir(public_path($imagePath)); $imagePath .= md5s($base64) . "." . $matchs[1][$key]; - if (Base::saveContentImage(public_path($imagePath), base64_decode($base64), 90)) { + if (Base::saveContentImage(public_path($imagePath), base64_decode($base64))) { $imageSize = getimagesize(public_path($imagePath)); if ($extension = Image::thumbImage(public_path($imagePath), public_path($imagePath) . "_thumb.{*}", 320, 0, 80)) { $imagePath .= "_thumb.{$extension}"; @@ -844,7 +844,7 @@ class WebSocketDialogMsg extends AbstractModel $image = file_get_contents($str); if (empty($image)) { $text = str_replace($matchs[0][$key], "[:IMAGE:browse:90:90:images/other/imgerr.jpg::]", $text); - } else if (Base::saveContentImage(public_path($imagePath), $image, 90)) { + } else if (Base::saveContentImage(public_path($imagePath), $image)) { $imageSize = getimagesize(public_path($imagePath)); if ($extension = Image::thumbImage(public_path($imagePath), public_path($imagePath) . "_thumb.{*}", 320, 0, 80)) { $imagePath .= "_thumb.{$extension}"; @@ -1012,7 +1012,7 @@ class WebSocketDialogMsg extends AbstractModel $fileUrl = "uploads/chat/" . date("Ym") . "/" . $dialog_id . "/" . md5s($msg['thumb']) . ".jpg"; $filePath = public_path($fileUrl); Base::makeDir(dirname($filePath)); - if (!Base::saveContentImage($filePath, $thumb, 90)) { + if (!Base::saveContentImage($filePath, $thumb)) { throw new ApiException('保存地图快照失败'); } $imageSize = getimagesize($filePath); diff --git a/app/Module/Base.php b/app/Module/Base.php index 057d42862..7e469198e 100755 --- a/app/Module/Base.php +++ b/app/Module/Base.php @@ -1932,7 +1932,7 @@ class Base // 图片裁剪 $cutMode = ($width > 0 && $height > 0) ? 'cover' : 'percentage'; $cutMode = $param['scale'][2] ?? $cutMode; - Image::thumbImage($array['file'], $array['file'], $width, $height, 90, $cutMode); + Image::thumbImage($array['file'], $array['file'], $width, $height, true, $cutMode); // 更新图片尺寸 $paramet = getimagesize($array['file']); $array['width'] = $paramet[0]; @@ -1949,9 +1949,8 @@ class Base } } // 压缩图片 - $quality = intval($param['quality']); - if ($quality > 0) { - Image::compressImage($array['file'], $quality); + if ($param['quality']) { + Image::compressImage($array['file'], $param['quality']); $array['size'] = Base::twoFloat(filesize($array['file']) / 1024, true); } //生成缩略图 @@ -2177,7 +2176,7 @@ class Base // 图片裁剪 $cutMode = ($width > 0 && $height > 0) ? 'cover' : 'percentage'; $cutMode = $param['scale'][2] ?? $cutMode; - Image::thumbImage($array['file'], $array['file'], $width, $height, 90, $cutMode); + Image::thumbImage($array['file'], $array['file'], $width, $height, true, $cutMode); // 更新图片尺寸 $paramet = getimagesize($array['file']); $array['width'] = $paramet[0]; @@ -2194,9 +2193,8 @@ class Base } } // 压缩图片 - $quality = intval($param['quality']); - if ($quality > 0) { - Image::compressImage($array['file'], $quality); + if ($param['quality']) { + Image::compressImage($array['file'], $param['quality']); $array['size'] = Base::twoFloat(filesize($array['file']) / 1024, true); } // 生成缩略图 @@ -2830,14 +2828,11 @@ class Base * 保存图片到文件(同时压缩) * @param $path * @param $content - * @param int $quality 压缩图片质量(默认:0不压缩) * @return bool */ - public static function saveContentImage($path, $content, int $quality = 0) { + public static function saveContentImage($path, $content) { if (file_put_contents($path, $content)) { - if ($quality > 0) { - Image::compressImage($path, $quality); - } + Image::compressImage($path); return true; } return false; diff --git a/app/Module/Image.php b/app/Module/Image.php index 0d3c82ef2..439c3fa8b 100644 --- a/app/Module/Image.php +++ b/app/Module/Image.php @@ -220,11 +220,11 @@ class Image * @param string $savePath 保存路径 * @param int $width 宽度 * @param int $height 高度 - * @param int $quality 压缩质量(0-100), 0 为不压缩 + * @param int|bool $quality 压缩质量(0-100), 0 为不压缩,true 为从系统设置里面获取 * @param string $mode 模式(percentage|cover|contain) * @return string|null 成功返回图片后缀,失败返回 false */ - public static function thumbImage(string $imagePath, string $savePath, int $width, int $height, int $quality = 0, string $mode = 'percentage'): ?string + public static function thumbImage(string $imagePath, string $savePath, int $width, int $height, int|bool $quality = 0, string $mode = 'percentage'): ?string { if (!file_exists($imagePath)) { return null; @@ -237,7 +237,7 @@ class Image $image = new Image($imagePath); $image->thumb($width, $height, $mode); $image->saveTo($savePath); - if ($quality > 0) { + if ($quality) { Image::compressImage($savePath, $quality); } if ($savePath != $imagePath && filesize($savePath) >= filesize($imagePath)) { @@ -253,14 +253,18 @@ class Image /** * 压缩图片(如果压缩后的图片比原图还大那就直接使用原图) * @param array|string $path 图片路径(如果是数组,第1个元素为原图路径,第2个元素为保存路径) - * @param int $quality 压缩质量(0-100) + * @param int|bool $quality 压缩质量(0-100),如果为 true,则从系统设置里面获取 * @param float $minSize 最小尺寸,小于这个尺寸不压缩(单位:KB) * @return bool */ - public static function compressImage(array|string $path, int $quality = 100, float $minSize = 5): bool + public static function compressImage(array|string $path, int|bool $quality = true, float $minSize = 5): bool { - if (Base::settingFind("system", "image_compress") === 'close') { - return false; + if ($quality === true) { + $setting = Base::setting("system"); + if ($setting['image_compress'] === 'close') { + return false; + } + $quality = $setting['image_quality']; } if (is_array($path)) { $imagePath = $path[0]; @@ -272,7 +276,7 @@ class Image if (!file_exists($imagePath)) { return false; } - $quality = min(max($quality, 1), 100); + $quality = min(max(intval($quality), 1), 100); $imageSize = filesize($imagePath); if ($minSize > 0 && $imageSize < $minSize * 1024) { return false; diff --git a/resources/assets/js/pages/manage/setting/components/SystemSetting.vue b/resources/assets/js/pages/manage/setting/components/SystemSetting.vue index 349e0659e..0610199d6 100644 --- a/resources/assets/js/pages/manage/setting/components/SystemSetting.vue +++ b/resources/assets/js/pages/manage/setting/components/SystemSetting.vue @@ -223,6 +223,10 @@ {{$L('关闭')}}
{{$L('数码相机4M的图片,优化后仅有700KB左右,而且肉眼基本看不出区别。')}}
+ + {{$L('压缩质量')}} + +
{{$L('取值范围:0-100,建议:90')}}