From 2ede6475c3e6baa2cb79aba56c36fc53ae3ede4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E4=B8=AD=E5=90=9B?= Date: Thu, 14 Sep 2023 22:25:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E6=B0=B4=E5=8D=B0?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E6=96=87=E4=BB=B6=E5=90=8D=20fix:=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=87=BA=E7=8E=B0=E8=89=B2=E5=9D=97=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20fix:=20=E4=BF=AE=E5=A4=8D=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E6=96=B9=E5=90=91=E5=BC=95=E5=8F=91=E7=9A=84=E6=B0=B4=E5=8D=B0?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E6=95=88=E6=9E=9C=E4=B8=8D=E5=AF=B9=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20fix:=20=E4=BF=AE=E5=A4=8D=E7=85=A7=E7=89=87?= =?UTF-8?q?=E5=BF=AB=E9=97=A8=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electron/src/modules/tools/blur.ts | 48 +++++++++++++++++------------- electron/src/router/query.ts | 7 +++-- web/main/app.svelte | 5 +++- web/mask/app.svelte | 7 ++++- 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/electron/src/modules/tools/blur.ts b/electron/src/modules/tools/blur.ts index 6000a97..4523192 100644 --- a/electron/src/modules/tools/blur.ts +++ b/electron/src/modules/tools/blur.ts @@ -18,28 +18,33 @@ export async function createBlurImg(filePath: string, toFilePath: string, option const mainImgSharp = await sharp(filePath); const mainImgInfoMetaData = await mainImgSharp.metadata(); - const originWidth = mainImgInfoMetaData.width; - const originHeight = mainImgInfoMetaData.height; - const blur = Math.round(Math.sqrt(originWidth ** 2 + originHeight ** 2) / 10) - 65; + const rotate = (((mainImgInfoMetaData.orientation || 1) - 1) * 90) % 360; + const rotateFileSharp = await mainImgSharp.rotate(rotate); + const rotateFileInfo = await rotateFileSharp.toBuffer({ resolveWithObject: true }); + + const rotateWidth = rotateFileInfo.info.width; + const rotateHeight = rotateFileInfo.info.height; + const outWidth = option.landscape && rotateWidth < rotateHeight ? rotateHeight : rotateWidth; + const outHeight = option.landscape && rotateWidth < rotateHeight ? rotateWidth : rotateHeight; - // 生成模糊背景 + const bgInfo = await sharp(filePath) + .rotate(rotate) + .resize({ width: outWidth, height: outHeight, fit: 'fill' }) + .toFormat('jpeg', { quality: 50 }) + .toBuffer({ resolveWithObject: true }); + fs.writeFileSync(toFilePath, bgInfo.data); + + const blur = Math.round(Math.sqrt(outWidth ** 2 + outHeight ** 2) / 10) - 65; + + // 生成模糊背景; await new Promise((r) => { ffmpeg - .input(filePath) + .input(toFilePath) .outputOptions('-vf', `boxblur=${blur}:2`) .saveToFile(toFilePath || filePath) .on('end', r); }); - const outWidth = option.landscape && originWidth < originHeight ? originHeight : originWidth; - const outHeight = option.landscape && originWidth < originHeight ? originWidth : originHeight; - const bgInfo = await sharp(toFilePath || filePath) - .rotate((((mainImgInfoMetaData.orientation || 1) - 1) * 90) % 360) - .resize({ width: outWidth, height: outHeight, fit: 'fill' }) - .toFormat('jpeg', { quality: 100 }) - .toBuffer({ resolveWithObject: true }); - fs.writeFileSync(toFilePath, bgInfo.data); - return { path: toFilePath, width: bgInfo.info.width, @@ -50,19 +55,22 @@ export async function createBlurImg(filePath: string, toFilePath: string, option export async function createScaleImg(filePath: string, toFilePath: string, option?: Option) { const mainImgSharp = await sharp(filePath); const mainImgInfoMetaData = await mainImgSharp.metadata(); - const originWidth = mainImgInfoMetaData.width; - const originHeight = mainImgInfoMetaData.height; + const rotate = (((mainImgInfoMetaData.orientation || 1) - 1) * 90) % 360; + + const rotateFileSharp = await mainImgSharp.rotate(rotate); + const rotateFileInfo = await rotateFileSharp.toBuffer({ resolveWithObject: true }); + const originWidth = rotateFileInfo.info.width; + const originHeight = rotateFileInfo.info.height; + const blurImgHeight = option.landscape && originWidth < originHeight ? originWidth : originHeight; const contentHeight = Math.round(blurImgHeight * 0.8); const resize = { width: 0, height: 0 }; - const rotate = (((mainImgInfoMetaData.orientation || 1) - 1) * 90) % 360; resize.width = Math.round((originWidth / originHeight) * contentHeight); resize.height = contentHeight; - const mainImgInfo = await mainImgSharp + const mainImgInfo = await rotateFileSharp .withMetadata({ orientation: 1 }) - .rotate(rotate) .resize({ ...resize, fit: 'inside' }) .toFormat('jpeg', { quality: 100 }) .toBuffer({ resolveWithObject: true }); @@ -92,7 +100,7 @@ interface TextInfo { export async function sharpComposite(bgPath: string | Buffer, mainPath: string, toFilePath: string, textInfo: TextInfo) { const bgInfo = await sharp(bgPath) - .toFormat('png', { quality: 100, alphaQuality: 100 }) + .toFormat('png') .toBuffer({ resolveWithObject: true }); if (!bgInfo) return undefined; diff --git a/electron/src/router/query.ts b/electron/src/router/query.ts index 1bb92ec..01dbb08 100644 --- a/electron/src/router/query.ts +++ b/electron/src/router/query.ts @@ -5,7 +5,6 @@ import { createWindow } from '../../main/create-window'; import routerConfig from '../../router-config'; import { Router } from '../modules/router'; import { createBlurImg, createScaleImg, getExifInfo, sharpComposite } from '../modules/tools/blur'; -import { formatDate } from '../utils/date'; import md5 from '../utils/md5'; const r = new Router(); @@ -57,7 +56,8 @@ function readConfig() { r.listen(routerConfig.startTask, async (data) => { await createMaskWin(); - for (const url of data.fileUrlList) { + for (const fileInfo of data.fileUrlList) { + const { path: url, name } = fileInfo; const _md5 = md5(url); const imgPath = path.resolve(cacheDir, _md5); const buffer = Buffer.alloc(200 * 1024); @@ -74,6 +74,7 @@ r.listen(routerConfig.startTask, async (data) => { // 发送到前端 maskGenWin.webContents.send(routerConfig.on.createMask, { + name, md5: _md5, exifInfo, blur: blurInfo, @@ -100,7 +101,7 @@ r.listen(routerConfig.composite, async (data) => { data.text.info.data = Buffer.from(data.text.info.data.split(',')[1], 'base64'); } - await sharpComposite(buffer, scalePath, path.resolve(config.output, `${formatDate('yyyy-MM-dd hh.mm.ss.S')}.jpg`), data.text); + await sharpComposite(buffer, scalePath, path.resolve(config.output, data.name), data.text); fs.rmSync(scalePath); fs.rmSync(blurPath); } catch (e) { diff --git a/web/main/app.svelte b/web/main/app.svelte index 756ef5c..e4d1038 100644 --- a/web/main/app.svelte +++ b/web/main/app.svelte @@ -33,7 +33,10 @@ fileUrlList = []; for (let i = 0; i < files.length; i++) { - fileUrlList.push(files[i].path); + fileUrlList.push({ + path: files[i].path, + name: files[i].name, + }); } } } diff --git a/web/mask/app.svelte b/web/mask/app.svelte index 56b7c24..6e12f3b 100644 --- a/web/mask/app.svelte +++ b/web/mask/app.svelte @@ -36,6 +36,7 @@ }); await window.api.composite({ + name: task.name, md5: task.md5, mask: maskUrl, text: textImgInfo, @@ -171,7 +172,11 @@ } if (exifInfo.ExposureTime) { - infoTextArr.push(`1/${1 / exifInfo.ExposureTime}s`); + if (exifInfo.ExposureTime < 1) { + infoTextArr.push(`1/${1 / exifInfo.ExposureTime}s`); + } else { + infoTextArr.push(`${exifInfo.ExposureTime}s`); + } } if (exifInfo.ISO) {