From 6c0d4271ecb6c009f8482a24ee3b8cb39410361d Mon Sep 17 00:00:00 2001 From: Thomas Lynch Date: Tue, 6 Oct 2020 10:18:50 +0000 Subject: [PATCH] close #263 option to make >thumbSize gif images have animated thumbnails, smaller gifs always static --- configs/main.js.example | 3 +++ helpers/files/imagethumbnail.js | 5 +++-- models/forms/makepost.js | 16 ++++++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/configs/main.js.example b/configs/main.js.example index 84f19b92..fcc2196e 100644 --- a/configs/main.js.example +++ b/configs/main.js.example @@ -121,6 +121,9 @@ module.exports = { in which case png is used. */ thumbExtension: '.jpg', + //gif images > thumbnail size will have animated thumbnails + animatedGifThumbnails: false, + //max thumb dimensions (square) in px. images smaller than this are not thumbnailed thumbSize: 250, diff --git a/helpers/files/imagethumbnail.js b/helpers/files/imagethumbnail.js index 0708c598..5830e37c 100644 --- a/helpers/files/imagethumbnail.js +++ b/helpers/files/imagethumbnail.js @@ -2,10 +2,11 @@ const gm = require('gm') , { thumbSize } = require(__dirname+'/../../configs/main.js') , uploadDirectory = require(__dirname+'/uploadDirectory.js'); -module.exports = (file) => { +module.exports = (file, firstFrameOnly=true) => { return new Promise((resolve, reject) => { - gm(`${uploadDirectory}/file/${file.filename}[0]`) //0 for first gif frame + //[0] for first frame (gifs, etc) + gm(`${uploadDirectory}/file/${file.filename}${firstFrameOnly ? '[0]' : ''}`) .resize(Math.min(thumbSize, file.geometry.width), Math.min(thumbSize, file.geometry.height)) .write(`${uploadDirectory}/file/thumb-${file.hash}${file.thumbextension}`, function (err) { if (err) { diff --git a/models/forms/makepost.js b/models/forms/makepost.js index 272e4eac..11e601cc 100644 --- a/models/forms/makepost.js +++ b/models/forms/makepost.js @@ -25,7 +25,7 @@ const path = require('path') , deletePosts = require(__dirname+'/deletepost.js') , spamCheck = require(__dirname+'/../../helpers/checks/spamcheck.js') , { checkRealMimeTypes, thumbSize, thumbExtension, videoThumbPercentage, - postPasswordSecret, strictFiltering } = require(__dirname+'/../../configs/main.js') + postPasswordSecret, strictFiltering, animatedGifThumbnails } = require(__dirname+'/../../configs/main.js') , buildQueue = require(__dirname+'/../../queue.js') , dynamicResponse = require(__dirname+'/../../helpers/dynamic.js') , { buildThread } = require(__dirname+'/../../helpers/tasks.js'); @@ -255,15 +255,23 @@ module.exports = async (req, res, next) => { const existsThumb = await pathExists(`${uploadDirectory}/file/thumb-${processedFile.hash}${processedFile.thumbextension}`); processedFile.geometry = imageData.size ; processedFile.geometryString = imageData.Geometry; + const lteThumbSize = (processedFile.geometry.height <= thumbSize + && processedFile.geometry.width <= thumbSize); processedFile.hasThumb = !(mimeTypes.allowed(file.mimetype, {image: true}) && subtype !== 'png' - && processedFile.geometry.height <= thumbSize - && processedFile.geometry.width <= thumbSize); + && lteThumbSize); if (!existsFull) { await moveUpload(file, processedFile.filename, 'file'); } if (!existsThumb && processedFile.hasThumb) { - await imageThumbnail(processedFile); + let firstFrameOnly = true; + if (!lteThumbSize + && file.mimetype === 'image/gif' + && animatedGifThumbnails === true) { + firstFrameOnly = false; + processedFile.thumbextension = '.gif'; + } + await imageThumbnail(processedFile, firstFrameOnly); } processedFile = fixGifs(processedFile); break;