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

9
themes/beauty/controller/SiteController.php

@ -266,6 +266,7 @@ Class SiteController extends Controller { @@ -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 { @@ -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);

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

@ -101,6 +101,13 @@ require_once __DIR__ . '/../../../../plugins/Html.php'; @@ -101,6 +101,13 @@ require_once __DIR__ . '/../../../../plugins/Html.php';
<script src="/js/qrcode.min.js"></script>
<script src="/js/video.min.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(); ?>
</body>
</html>

19
www/js/beauty.js

@ -21,18 +21,20 @@ if ($('#image_site').get(0)) { @@ -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)) { @@ -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) { @@ -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);
}

Loading…
Cancel
Save