From 6ebdf6f206b2fd2015451d741a9ba3261e78548a Mon Sep 17 00:00:00 2001 From: filesite Date: Mon, 12 Aug 2024 11:59:53 +0800 Subject: [PATCH] improve small image generator, avoid zoom small image --- conf/app.php | 1 + themes/beauty/controller/SiteController.php | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/conf/app.php b/conf/app.php index 50a7257..94ede2f 100644 --- a/conf/app.php +++ b/conf/app.php @@ -53,6 +53,7 @@ $configs = array( 'supportedVideoExts' => array('mp4', 'mov', 'm3u8'), 'screenshot_start' => 1000, //视频播放页快照截取开始时间,单位:毫秒 'screenshot_expire_seconds' => 315360000, //视频封面图缓存3650天 + 'small_image_zoom_rate' => 2.5, //缩略图在最小尺寸基础上的放大倍数,以确保清晰度 'enableSmallImage' => true, //开启图片小尺寸缩略图,设置 false 则关闭 /* diff --git a/themes/beauty/controller/SiteController.php b/themes/beauty/controller/SiteController.php index 52aec98..0868e25 100644 --- a/themes/beauty/controller/SiteController.php +++ b/themes/beauty/controller/SiteController.php @@ -270,7 +270,7 @@ Class SiteController extends Controller { if (empty($cachedData)) { //从缓存数据中获取目录的realpath - $cachedData = Common::getCacheFromFile($cacheId); + $cachedData = Common::getCacheFromFile($cacheId, $expireSeconds); if (!empty($cachedData)) { $realpath = $cachedData[$cateId]['realpath']; $scanner = new DirScanner(); @@ -374,15 +374,23 @@ Class SiteController extends Controller { list($naturalWidth, $naturalHeight, $imgTypeIndex, $style) = getimagesize($img_filepath); $imgType = image_type_to_extension($imgTypeIndex); + //小图片则保持原图尺寸 + if ($naturalWidth <= $max_width || $naturalHeight <= $max_height) { + return false; + } + //生成同比例缩略图尺寸 + $zoomRate = FSC::$app['config']['small_image_zoom_rate']; //缩略图在最小尺寸基础上放大比例,为确保清晰度 $width = $min_width; $height = $min_height; $aspect = $naturalHeight / $naturalWidth; if ($naturalWidth <= $naturalHeight) { - $width = $width * 2.5 <= $max_width ? (int)($width * 2.5) : $max_width; + if ($width * $zoomRate >= $naturalWidth) {return false;} //避免把小图片放大 + $width = $width * $zoomRate <= $max_width ? (int)($width * $zoomRate) : $max_width; $height = (int)($width * $aspect); }else { - $height = $height * 2.5 <= $max_height ? (int)($height * 2.5) : $max_height; + if ($height * $zoomRate >= $naturalHeight) {return false;} //避免把小图片放大 + $height = $height * $zoomRate <= $max_height ? (int)($height * $zoomRate) : $max_height; $width = (int)($height / $aspect); }