2023-11-28 06:52:53 +00:00
|
|
|
module.exports = (decode, encode) => {
|
2023-11-09 23:36:08 +00:00
|
|
|
const convertImage = async ({ image, format, quality }) => {
|
2023-11-28 06:52:53 +00:00
|
|
|
return await encode[format]({
|
2023-11-09 23:36:08 +00:00
|
|
|
width: image.width,
|
|
|
|
height: image.height,
|
|
|
|
data: image.data,
|
2023-11-28 07:27:04 +00:00
|
|
|
quality
|
2023-11-09 23:36:08 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const convert = async ({ buffer, format, quality, all }) => {
|
2023-11-28 06:52:53 +00:00
|
|
|
if (!encode[format]) {
|
|
|
|
throw new Error(`output format needs to be one of [${Object.keys(encode)}]`);
|
2023-11-09 23:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!all) {
|
|
|
|
const image = await decode({ buffer });
|
|
|
|
return await convertImage({ image, format, quality });
|
|
|
|
}
|
|
|
|
|
|
|
|
const images = await decode.all({ buffer });
|
|
|
|
|
|
|
|
return images.map(image => {
|
|
|
|
return {
|
|
|
|
convert: async () => await convertImage({
|
|
|
|
image: await image.decode(),
|
|
|
|
format,
|
|
|
|
quality
|
|
|
|
})
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
one: async ({ buffer, format, quality = 0.92 }) => await convert({ buffer, format, quality, all: false }),
|
|
|
|
all: async ({ buffer, format, quality = 0.92 }) => await convert({ buffer, format, quality, all: true })
|
|
|
|
};
|
|
|
|
};
|