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) {