feat: add source-only HEIC/HEIF image converter
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import fs from 'node:fs/promises';
|
||||
import sharp from 'sharp';
|
||||
import convert from 'heic-convert';
|
||||
import { register } from './registry.js';
|
||||
import { sharpFormatName, buildFormatOptions } from './image.js';
|
||||
import { encodeIcoFromInput, DEFAULT_ICON_SIZE } from './ico.js';
|
||||
|
||||
const IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'webp', 'gif', 'tiff', 'avif'];
|
||||
const HEIC_SOURCE_FORMATS = ['heic', 'heif'];
|
||||
|
||||
async function decodeToPng(inputPath) {
|
||||
const buffer = await fs.readFile(inputPath);
|
||||
return convert({ buffer, format: 'PNG' });
|
||||
}
|
||||
|
||||
export function registerHeicConverter() {
|
||||
for (const sourceFormat of HEIC_SOURCE_FORMATS) {
|
||||
for (const targetFormat of IMAGE_FORMATS) {
|
||||
register({
|
||||
family: 'image',
|
||||
sourceFormat,
|
||||
targetFormat,
|
||||
convert: async (inputPath, outputPath, { quality } = {}) => {
|
||||
const pngBuffer = await decodeToPng(inputPath);
|
||||
await sharp(pngBuffer)
|
||||
.toFormat(sharpFormatName(targetFormat), buildFormatOptions(targetFormat, quality))
|
||||
.toFile(outputPath);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
register({
|
||||
family: 'image',
|
||||
sourceFormat,
|
||||
targetFormat: 'ico',
|
||||
convert: async (inputPath, outputPath, { iconSize } = {}) => {
|
||||
const pngBuffer = await decodeToPng(inputPath);
|
||||
const icoBuffer = await encodeIcoFromInput(pngBuffer, iconSize ?? DEFAULT_ICON_SIZE);
|
||||
await fs.writeFile(outputPath, icoBuffer);
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user