responsively-app/desktop-app/app/imageWorker.js
2020-05-28 18:04:13 +05:30

66 lines
1.6 KiB
JavaScript

const registerPromiseWorker = require('promise-worker/register');
const mergeImg = require('merge-img');
const {promisify} = require('util');
const Jimp = require('jimp');
const os = require('os');
const path = require('path');
const UUID = require('uuid/v4');
const fs = require('fs-extra');
const tempDir = path.join(os.tmpdir(), UUID());
registerPromiseWorker(({images, direction, resultFilename}) => {
if (direction === 'horizontal') {
return stitchHorizontally(images);
}
return stitchVertically(images, resultFilename);
});
async function stitchHorizontally(images) {
const result = await mergeImg(
images.map(img => ({src: Buffer.from(img)})),
{
direction: false,
}
);
const tempPath = await writeToTempFile(result);
return tempPath;
}
async function writeToTempFile(image) {
return new Promise(async (resolve, reject) => {
await fs.ensureDir(tempDir);
const tempPath = path.join(tempDir, `${UUID()}.png`);
await image.write(tempPath, err => {
if (err) {
return reject(err);
}
return resolve(tempPath);
});
});
}
async function stitchVertically(images, {dir, file}) {
const result = (
await mergeImg(
await Promise.all(
images.map(async img => {
const JimpImg = await Jimp.read(img);
return {
src: await JimpImg.getBufferAsync('image/png'),
};
})
),
{
direction: true,
}
)
)
.rgba(false)
.background(0xffffffff);
await fs.ensureDir(dir);
await Promise.all([
result.write(path.join(dir, file)),
...images.map(image => fs.remove(image)),
]);
}