feat: support quality/compressionLevel options in image converters

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-30 23:03:39 +02:00
co-authored by Claude Sonnet 5
parent 8caab530e7
commit 57d7dac2b5
2 changed files with 73 additions and 3 deletions
+11 -2
View File
@@ -7,6 +7,13 @@ function sharpFormatName(format) {
return format === 'jpg' ? 'jpeg' : format;
}
export function buildFormatOptions(targetFormat, quality) {
if (quality == null) return {};
if (targetFormat === 'png') return { compressionLevel: quality };
if (targetFormat === 'gif') return {};
return { quality };
}
export function registerImageConverters() {
for (const sourceFormat of IMAGE_FORMATS) {
for (const targetFormat of IMAGE_FORMATS) {
@@ -16,8 +23,10 @@ export function registerImageConverters() {
family: 'image',
sourceFormat,
targetFormat,
convert: async (inputPath, outputPath) => {
await sharp(inputPath).toFormat(sharpFormatName(targetFormat)).toFile(outputPath);
convert: async (inputPath, outputPath, { quality } = {}) => {
await sharp(inputPath)
.toFormat(sharpFormatName(targetFormat), buildFormatOptions(targetFormat, quality))
.toFile(outputPath);
},
});
}