Skip to content

Commit

Permalink
feat: 修改水印输出文件名
Browse files Browse the repository at this point in the history
fix: 修复出现色块问题
fix: 修复图片方向引发的水印输出效果不对问题
fix: 修复照片快门错误问题
  • Loading branch information
云中君 committed Sep 14, 2023
1 parent c122806 commit 2ede647
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
48 changes: 28 additions & 20 deletions electron/src/modules/tools/blur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 });
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions electron/src/router/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -57,7 +56,8 @@ function readConfig() {
r.listen<any, boolean>(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);
Expand All @@ -74,6 +74,7 @@ r.listen<any, boolean>(routerConfig.startTask, async (data) => {

// 发送到前端
maskGenWin.webContents.send(routerConfig.on.createMask, {
name,
md5: _md5,
exifInfo,
blur: blurInfo,
Expand All @@ -100,7 +101,7 @@ r.listen<any, any>(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) {
Expand Down
5 changes: 4 additions & 1 deletion web/main/app.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion web/mask/app.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
});
await window.api.composite({
name: task.name,
md5: task.md5,
mask: maskUrl,
text: textImgInfo,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2ede647

Please sign in to comment.