feat: warn when losing transparency during thumbnail generation (#26243)

* feat: preserve alpha

* refactor: use isTransparent naming and separate getImageMetadata

* warn instead of preserve
This commit is contained in:
Min Idzelis
2026-02-23 08:16:28 -05:00
committed by GitHub
parent caebe5166a
commit 430638e129
6 changed files with 131 additions and 34 deletions

View File

@@ -153,6 +153,33 @@ describe('mimeTypes', () => {
}
});
describe('canBeTransparent', () => {
for (const img of [
'a.avif',
'a.bmp',
'a.gif',
'a.heic',
'a.heif',
'a.hif',
'a.jxl',
'a.png',
'a.svg',
'a.tif',
'a.tiff',
'a.webp',
]) {
it(`should return true for ${img}`, () => {
expect(mimeTypes.canBeTransparent(img)).toBe(true);
});
}
for (const img of ['a.jpg', 'a.jpeg', 'a.jpe', 'a.insp', 'a.jp2', 'a.cr3', 'a.dng', 'a.nef', 'a.arw']) {
it(`should return false for ${img}`, () => {
expect(mimeTypes.canBeTransparent(img)).toBe(false);
});
}
});
describe('animated image', () => {
for (const img of ['a.avif', 'a.gif', 'a.webp']) {
it('should identify animated image mime types as such', () => {

View File

@@ -77,6 +77,21 @@ const extensionOverrides: Record<string, string> = {
'image/jpeg': '.jpg',
};
const transparentCapableExtensions = new Set([
'.avif',
'.bmp',
'.gif',
'.heic',
'.heif',
'.hif',
'.jxl',
'.png',
'.svg',
'.tif',
'.tiff',
'.webp',
]);
const profileExtensions = new Set(['.avif', '.dng', '.heic', '.heif', '.jpeg', '.jpg', '.png', '.webp', '.svg']);
const profile: Record<string, string[]> = Object.fromEntries(
Object.entries(image).filter(([key]) => profileExtensions.has(key)),
@@ -134,6 +149,7 @@ export const mimeTypes = {
isProfile: (filename: string) => isType(filename, profile),
isSidecar: (filename: string) => isType(filename, sidecar),
isVideo: (filename: string) => isType(filename, video),
canBeTransparent: (filename: string) => transparentCapableExtensions.has(extname(filename).toLowerCase()),
isRaw: (filename: string) => isType(filename, raw),
lookup,
/** return an extension (including a leading `.`) for a mime-type */