u
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user