feat: support quality/compressionLevel options in image converters
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
+11
-2
@@ -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);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user