feat(thumbnail): 支持自定义缩略图高度参数
添加对缩略图高度参数的支持,当未指定高度时保持原有行为。修改缩略图生成逻辑以适配高度参数,包括文件名后缀和实际缩放处理。
This commit is contained in:
9
index.js
9
index.js
@@ -128,10 +128,12 @@ async function generateThumbAndCache(reply, apiData, contentPath) {
|
|||||||
const base = path.basename(srcPath);
|
const base = path.basename(srcPath);
|
||||||
const fit = (apiData.data.thumb && apiData.data.thumb.fit === 'max') ? 'inside' : 'cover';
|
const fit = (apiData.data.thumb && apiData.data.thumb.fit === 'max') ? 'inside' : 'cover';
|
||||||
const width = (apiData.data.thumb && apiData.data.thumb.w) ? apiData.data.thumb.w : 100;
|
const width = (apiData.data.thumb && apiData.data.thumb.w) ? apiData.data.thumb.w : 100;
|
||||||
|
const height = (apiData.data.thumb && apiData.data.thumb.h) ? apiData.data.thumb.h : 0;
|
||||||
|
|
||||||
const contentType = (apiData.data.headers && apiData.data.headers['content-type']) || getMimeFromUrl(apiData.data.url) || 'image/jpeg';
|
const contentType = (apiData.data.headers && apiData.data.headers['content-type']) || getMimeFromUrl(apiData.data.url) || 'image/jpeg';
|
||||||
const preferredFmt = contentType.includes('png') ? 'png' : contentType.includes('webp') ? 'webp' : 'jpeg';
|
const preferredFmt = contentType.includes('png') ? 'png' : contentType.includes('webp') ? 'webp' : 'jpeg';
|
||||||
const variantFmt = contentType.includes('video/') ? 'webp' : preferredFmt;
|
const variantFmt = contentType.includes('video/') ? 'webp' : preferredFmt;
|
||||||
const variantSuffix = `.thumb_${fit}_w${width}_${variantFmt}`;
|
const variantSuffix = `.thumb_${fit}_w${width}_h${height}_${variantFmt}`;
|
||||||
const thumbFinal = path.join(dir, base.replace('.data', variantSuffix));
|
const thumbFinal = path.join(dir, base.replace('.data', variantSuffix));
|
||||||
if (fs.existsSync(thumbFinal)) {
|
if (fs.existsSync(thumbFinal)) {
|
||||||
const st = fs.statSync(thumbFinal);
|
const st = fs.statSync(thumbFinal);
|
||||||
@@ -175,7 +177,8 @@ async function generateThumbAndCache(reply, apiData, contentPath) {
|
|||||||
console.log('Generating video thumb:', srcPath);
|
console.log('Generating video thumb:', srcPath);
|
||||||
const thumbFrameTemp = path.join(dir, base.replace('.data', `${variantSuffix}.frame.png.tmp`));
|
const thumbFrameTemp = path.join(dir, base.replace('.data', `${variantSuffix}.frame.png.tmp`));
|
||||||
const { spawn } = require('child_process');
|
const { spawn } = require('child_process');
|
||||||
const args = ['-hide_banner', '-loglevel', 'error', '-nostdin', '-ss', '1', '-i', srcPath, '-frames:v', '1', '-vf', `scale=${width}:-2`, '-f', 'image2', '-vcodec', 'png', '-y', thumbFrameTemp];
|
const scaleH = (height && height > 0) ? height : -2;
|
||||||
|
const args = ['-hide_banner', '-loglevel', 'error', '-nostdin', '-ss', '1', '-i', srcPath, '-frames:v', '1', '-vf', `scale=${width}:${scaleH}`, '-f', 'image2', '-vcodec', 'png', '-y', thumbFrameTemp];
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
let err = '';
|
let err = '';
|
||||||
const p = spawn('ffmpeg', args, { stdio: ['ignore', 'ignore', 'pipe'] });
|
const p = spawn('ffmpeg', args, { stdio: ['ignore', 'ignore', 'pipe'] });
|
||||||
@@ -194,7 +197,7 @@ async function generateThumbAndCache(reply, apiData, contentPath) {
|
|||||||
const inputMeta = await sharp(srcPath).metadata();
|
const inputMeta = await sharp(srcPath).metadata();
|
||||||
const outFmt = preferredFmt || inputMeta.format || 'jpeg';
|
const outFmt = preferredFmt || inputMeta.format || 'jpeg';
|
||||||
const thumbTemp = path.join(dir, base.replace('.data', `${variantSuffix}.tmp`));
|
const thumbTemp = path.join(dir, base.replace('.data', `${variantSuffix}.tmp`));
|
||||||
const pipeline = sharp(srcPath).resize({ width, fit });
|
const pipeline = sharp(srcPath).resize({ width, height: (height && height > 0) ? height : undefined, fit });
|
||||||
if (outFmt === 'jpeg') pipeline.jpeg({ quality: 85 });
|
if (outFmt === 'jpeg') pipeline.jpeg({ quality: 85 });
|
||||||
else if (outFmt === 'png') pipeline.png();
|
else if (outFmt === 'png') pipeline.png();
|
||||||
else pipeline.webp({ quality: 80 });
|
else pipeline.webp({ quality: 80 });
|
||||||
|
|||||||
Reference in New Issue
Block a user