u
This commit is contained in:
51
src/util/processAvifImage.js
Normal file
51
src/util/processAvifImage.js
Normal 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'),
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user