From 76f1b7dcd459b91fcfe39a33233d2b10454a84c0 Mon Sep 17 00:00:00 2001 From: filesite Date: Wed, 17 Jul 2024 22:36:34 +0800 Subject: [PATCH] add config parameter for small image generator --- conf/app.php | 1 + themes/beauty/controller/SiteController.php | 9 +++++++++ themes/beauty/views/layout/main.php | 7 +++++++ www/js/beauty.js | 19 ++++++++++++------- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/conf/app.php b/conf/app.php index 774008a..b63c895 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天 + 'enableSmallImage' => true, //开启图片小尺寸缩略图 /* //视频皮肤配置 diff --git a/themes/beauty/controller/SiteController.php b/themes/beauty/controller/SiteController.php index 9ab31a5..5b4a2a8 100644 --- a/themes/beauty/controller/SiteController.php +++ b/themes/beauty/controller/SiteController.php @@ -266,6 +266,7 @@ Class SiteController extends Controller { } //优先从缓存获取小尺寸的图片 + //增加父目录封面图缓存更新 public function actionSmallimg() { $imgId = $this->get('id', ''); $imgUrl = $this->get('url', ''); @@ -297,12 +298,20 @@ Class SiteController extends Controller { $code = 0; $msg = 'OK'; + $cateId = $this->post('pid', ''); $imgId = $this->post('id', ''); $imgData = $this->post('data', ''); //base64格式的图片数据 if (empty($imgId) || empty($imgData)) { $code = 0; $msg = '参数不能为空'; }else { + //如果是目录封面图生成缩略图,则更新目录封面图缓存数据 + if (!empty($cateId)) { + $cacheKey = $this->getCacheKey($cateId, 'dirsnap'); + $img_id = ''; //为保持数据格式一致,图片id传空 + Common::saveCacheToFile($cacheKey, array('url' => $imgData, 'img_id' => $img_id)); + } + $cacheKey = $this->getCacheKey($imgId, 'imgsm'); $cacheSubDir = 'image'; $saved = Common::saveCacheToFile($cacheKey, $imgData, $cacheSubDir); diff --git a/themes/beauty/views/layout/main.php b/themes/beauty/views/layout/main.php index 1ea65bc..95c8f1c 100644 --- a/themes/beauty/views/layout/main.php +++ b/themes/beauty/views/layout/main.php @@ -101,6 +101,13 @@ require_once __DIR__ . '/../../../../plugins/Html.php'; + \ No newline at end of file diff --git a/www/js/beauty.js b/www/js/beauty.js index 8754e95..888699a 100644 --- a/www/js/beauty.js +++ b/www/js/beauty.js @@ -21,18 +21,20 @@ if ($('#image_site').get(0)) { }) //需要浏览器支持naturalWidth - var saveSmallImg = function(imgEl) { + var saveSmallImg = function(imgEl, cateId) { var width = imgEl.width, naturalWidth = imgEl.naturalWidth, naturalHeight = imgEl.naturalHeight; - if (!naturalWidth || naturalWidth - width < 100) {return false;} + if (!naturalWidth || naturalWidth - width < 100 || + (typeof(disableSmallImage) != 'undefined' && disableSmallImage) + ) { + return false; + } var aspect = naturalHeight / naturalWidth; - var canvas = document.createElement('canvas'); - - canvas.width = width; - canvas.height = width * aspect; + canvas.width = width * 1.5; + canvas.height = canvas.width * aspect; var ctx = canvas.getContext('2d'); ctx.drawImage( imgEl, 0, 0, canvas.width, canvas.height ); @@ -45,6 +47,9 @@ if ($('#image_site').get(0)) { id: $(imgEl).attr('data-id'), data: smallImg }; + if (typeof(cateId) != 'undefined' && cateId) { + params.pid = cateId; + } $.ajax({ url: '/site/savesmallimg', @@ -218,7 +223,7 @@ $('.dir_item').each(function(index, el) { setTimeout(function() { var imgs = $(el).find('.im_img'); if (imgs.length > 0) { - saveSmallImg(imgs[0]); + saveSmallImg(imgs[0], id); } }, 100); }