This commit is contained in:
seb
2026-07-31 19:49:07 +02:00
parent 6b1701d4dc
commit e8326e22ac
10 changed files with 827 additions and 5 deletions

View File

@@ -0,0 +1,51 @@
import { createHash } from 'node:crypto';
import sharp from 'sharp';
import { rgbaToThumbHash } from 'thumbhash';
const FULL_MAX = 1000;
const THUMB_MAX = 200;
export default async function processAvifImage(inputBuffer) {
const input = Buffer.isBuffer(inputBuffer)
? inputBuffer
: Buffer.from(inputBuffer);
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)
.resize(100, 100, { fit: 'inside', withoutEnlargement: true })
.ensureAlpha()
.raw()
.toBuffer({ resolveWithObject: true }),
]);
const thumbHashBytes = rgbaToThumbHash(
thumbRgba.info.width,
thumbRgba.info.height,
thumbRgba.data,
);
return {
full: {
w: full.info.width,
h: full.info.height,
size: full.data.length,
buffer: full.data,
hash: createHash('sha256').update(full.data).digest('hex'),
},
thumb: {
w: thumb.info.width,
h: thumb.info.height,
size: thumb.data.length,
buffer: thumb.data,
hash: Buffer.from(thumbHashBytes).toString('base64'),
},
};
}