Browse Source

add config parameter for small image generator

master
filesite 4 months ago
parent
commit
76f1b7dcd4
  1. 1
      conf/app.php
  2. 9
      themes/beauty/controller/SiteController.php
  3. 7
      themes/beauty/views/layout/main.php
  4. 19
      www/js/beauty.js

1
conf/app.php

@ -53,6 +53,7 @@ $configs = array(
'supportedVideoExts' => array('mp4', 'mov', 'm3u8'), 'supportedVideoExts' => array('mp4', 'mov', 'm3u8'),
'screenshot_start' => 1000, //视频播放页快照截取开始时间,单位:毫秒 'screenshot_start' => 1000, //视频播放页快照截取开始时间,单位:毫秒
'screenshot_expire_seconds' => 315360000, //视频封面图缓存3650天 'screenshot_expire_seconds' => 315360000, //视频封面图缓存3650天
'enableSmallImage' => true, //开启图片小尺寸缩略图
/* /*
//视频皮肤配置 //视频皮肤配置

9
themes/beauty/controller/SiteController.php

@ -266,6 +266,7 @@ Class SiteController extends Controller {
} }
//优先从缓存获取小尺寸的图片 //优先从缓存获取小尺寸的图片
//增加父目录封面图缓存更新
public function actionSmallimg() { public function actionSmallimg() {
$imgId = $this->get('id', ''); $imgId = $this->get('id', '');
$imgUrl = $this->get('url', ''); $imgUrl = $this->get('url', '');
@ -297,12 +298,20 @@ Class SiteController extends Controller {
$code = 0; $code = 0;
$msg = 'OK'; $msg = 'OK';
$cateId = $this->post('pid', '');
$imgId = $this->post('id', ''); $imgId = $this->post('id', '');
$imgData = $this->post('data', ''); //base64格式的图片数据 $imgData = $this->post('data', ''); //base64格式的图片数据
if (empty($imgId) || empty($imgData)) { if (empty($imgId) || empty($imgData)) {
$code = 0; $code = 0;
$msg = '参数不能为空'; $msg = '参数不能为空';
}else { }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'); $cacheKey = $this->getCacheKey($imgId, 'imgsm');
$cacheSubDir = 'image'; $cacheSubDir = 'image';
$saved = Common::saveCacheToFile($cacheKey, $imgData, $cacheSubDir); $saved = Common::saveCacheToFile($cacheKey, $imgData, $cacheSubDir);

7
themes/beauty/views/layout/main.php

@ -101,6 +101,13 @@ require_once __DIR__ . '/../../../../plugins/Html.php';
<script src="/js/qrcode.min.js"></script> <script src="/js/qrcode.min.js"></script>
<script src="/js/video.min.js"></script> <script src="/js/video.min.js"></script>
<script src="/js/beauty.js?v<?= Html::getStaticFileVersion('beauty.js', 'js') ?>"></script> <script src="/js/beauty.js?v<?= Html::getStaticFileVersion('beauty.js', 'js') ?>"></script>
<script>
<?php if (FSC::$app['config']['enableSmallImage'] == false) {
echo <<<eof
var disableSmallImage = true;
eof;
} ?>
</script>
<?php echo Html::getGACode(); ?> <?php echo Html::getGACode(); ?>
</body> </body>
</html> </html>

19
www/js/beauty.js

@ -21,18 +21,20 @@ if ($('#image_site').get(0)) {
}) })
//需要浏览器支持naturalWidth //需要浏览器支持naturalWidth
var saveSmallImg = function(imgEl) { var saveSmallImg = function(imgEl, cateId) {
var width = imgEl.width, var width = imgEl.width,
naturalWidth = imgEl.naturalWidth, naturalWidth = imgEl.naturalWidth,
naturalHeight = imgEl.naturalHeight; 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 aspect = naturalHeight / naturalWidth;
var canvas = document.createElement('canvas'); var canvas = document.createElement('canvas');
canvas.width = width * 1.5;
canvas.width = width; canvas.height = canvas.width * aspect;
canvas.height = width * aspect;
var ctx = canvas.getContext('2d'); var ctx = canvas.getContext('2d');
ctx.drawImage( imgEl, 0, 0, canvas.width, canvas.height ); ctx.drawImage( imgEl, 0, 0, canvas.width, canvas.height );
@ -45,6 +47,9 @@ if ($('#image_site').get(0)) {
id: $(imgEl).attr('data-id'), id: $(imgEl).attr('data-id'),
data: smallImg data: smallImg
}; };
if (typeof(cateId) != 'undefined' && cateId) {
params.pid = cateId;
}
$.ajax({ $.ajax({
url: '/site/savesmallimg', url: '/site/savesmallimg',
@ -218,7 +223,7 @@ $('.dir_item').each(function(index, el) {
setTimeout(function() { setTimeout(function() {
var imgs = $(el).find('.im_img'); var imgs = $(el).find('.im_img');
if (imgs.length > 0) { if (imgs.length > 0) {
saveSmallImg(imgs[0]); saveSmallImg(imgs[0], id);
} }
}, 100); }, 100);
} }

Loading…
Cancel
Save