sharp by wasm

This commit is contained in:
seb
2026-10-28 01:46:51 +01:00
parent 1c5385061c
commit e7884c03ef
20 changed files with 530 additions and 364 deletions

View File

@@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import sharp from 'sharp';
import { optimizeImage } from 'wasm-image-optimization';
import { defineTool, ToolError } from 'libllmorbiter';
import * as dbCompanySetting from '../db/companysetting.js';
import { prepareVfs, syncVfsToDb } from './vfs.js';
@@ -118,11 +118,20 @@ async function toAvif(img, { signal } = {}) {
const input = Buffer.from(await res.arrayBuffer());
if (input.length === 0) continue;
return await sharp(input)
.rotate() // honour EXIF orientation before resizing
.resize(MAX_EDGE, MAX_EDGE, { fit: 'inside', withoutEnlargement: true })
.avif({ quality: AVIF_QUALITY })
.toBuffer();
const meta = await optimizeImage({ image: input, format: 'none' });
const srcW = meta.originalWidth || 0;
const srcH = meta.originalHeight || 0;
const needsResize = srcW > MAX_EDGE || srcH > MAX_EDGE;
const scale = needsResize ? Math.min(MAX_EDGE / srcW, MAX_EDGE / srcH) : 1;
const out = await optimizeImage({
image: input,
width: needsResize ? Math.max(1, Math.round(srcW * scale)) : undefined,
height: needsResize ? Math.max(1, Math.round(srcH * scale)) : undefined,
fit: 'fill',
format: 'avif',
quality: AVIF_QUALITY,
});
return Buffer.from(out.data);
} catch {
// Try the next candidate URL; a dead imageUrl with a live thumbnail is common.
}