From 6dcf19349a52cfad0ca94e47ee20e8d371131609 Mon Sep 17 00:00:00 2001 From: filesite Date: Wed, 16 Oct 2024 20:37:54 +0800 Subject: [PATCH] improve small image create, add minimal size check --- conf/app.php | 4 ++-- themes/beauty/controller/SiteController.php | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/conf/app.php b/conf/app.php index 24fc63b..0f3bf60 100644 --- a/conf/app.php +++ b/conf/app.php @@ -3,8 +3,8 @@ * Config */ $configs = array( - 'version' => '0.3.5', - 'releaseDate' => '2024-10-07', + 'version' => '0.3.6', + 'releaseDate' => '2024-10-16', 'showVersion' => false, //默认不显示版本号和发布日期 'default_timezone' => 'Asia/Hong_Kong', //timezone, check more: https://www.php.net/manual/en/timezones.asia.php diff --git a/themes/beauty/controller/SiteController.php b/themes/beauty/controller/SiteController.php index f5e67af..8140992 100644 --- a/themes/beauty/controller/SiteController.php +++ b/themes/beauty/controller/SiteController.php @@ -590,6 +590,7 @@ Class SiteController extends Controller { //优先从缓存获取小尺寸的图片 //增加父目录封面图缓存更新 //增加图片尺寸类型参数: size + //增加缩略图生成失败检查,如果缩略图文件大小小于 5 Kb,则认为生成了无效的图片(如黑图) public function actionSmallimg() { $imgId = $this->get('id', ''); $imgUrl = $this->get('url', ''); @@ -605,6 +606,17 @@ Class SiteController extends Controller { $small_image_client_cache_seconds = FSC::$app['config']['small_image_client_cache_seconds']; + //检查文件大小,如果小于 5 Kb,则重新生成图片 + if (!empty($cachedData)) { + $imgType = preg_replace('/^data:(image\/.+);base64,.+$/i', "$1", $cachedData); + $base64_img = preg_replace('/^data:image\/.+;base64,/i', '', $cachedData); + $img_data = base64_decode($base64_img); + $minNormalImgSize = 5 * 1024; //最小图片尺寸:5Kb + if (strlen($img_data) < $minNormalImgSize) { + $cachedData = null; + } + } + //无缓存,则实时生成缩略图 if (empty($cachedData)) { $tmpUrl = parse_url($imgUrl); @@ -629,7 +641,6 @@ Class SiteController extends Controller { $base64_img = preg_replace('/^data:image\/.+;base64,/i', '', $cachedData); $img_data = base64_decode($base64_img); - $etag = md5($img_data); $etag_from_client = !empty($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : ''; //get etag from client if (!empty($etag) && $etag == $etag_from_client) {