This commit is contained in:
seb
2026-07-31 20:10:19 +02:00
parent e8326e22ac
commit 243143e7eb
2 changed files with 32 additions and 11 deletions

View File

@@ -10,16 +10,37 @@ export default async function processAvifImage(inputBuffer) {
? inputBuffer
: Buffer.from(inputBuffer);
const meta = await sharp(input).metadata();
const w = meta.width ?? 0;
const h = meta.height ?? 0;
const isAvif =
meta.format === 'avif' ||
(meta.format === 'heif' && meta.compression === 'av1');
const useOriginalFull =
isAvif && w > 0 && h > 0 && w <= FULL_MAX && h <= FULL_MAX;
if (!useOriginalFull) {
console.log(
`processAvifImage: converting full image (format=${meta.format ?? 'unknown'}, ${w}×${h}, avif=${isAvif})`,
);
}
const thumbPipeline = sharp(input)
.resize(THUMB_MAX, THUMB_MAX, { fit: 'inside', withoutEnlargement: true });
const [full, thumb, thumbRgba] = await Promise.all([
sharp(input)
.resize(FULL_MAX, FULL_MAX, { fit: 'inside', withoutEnlargement: true })
.avif({ effort: 4 })
.toBuffer({ resolveWithObject: true }),
sharp(input)
.resize(THUMB_MAX, THUMB_MAX, { fit: 'inside', withoutEnlargement: true })
.avif({ effort: 4 })
.toBuffer({ resolveWithObject: true }),
sharp(input)
useOriginalFull
? Promise.resolve({
info: { width: w, height: h },
data: input,
})
: sharp(input)
.resize(FULL_MAX, FULL_MAX, { fit: 'inside', withoutEnlargement: true })
.avif({ effort: 4 })
.toBuffer({ resolveWithObject: true }),
thumbPipeline.clone().avif({ effort: 4 }).toBuffer({ resolveWithObject: true }),
thumbPipeline
.clone()
.resize(100, 100, { fit: 'inside', withoutEnlargement: true })
.ensureAlpha()
.raw()