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 @@
// MARK: - Imports
// #region Imports
import sharp from 'sharp';
import { optimizeImage } from 'wasm-image-optimization';
import { encodeRGBA } from 'skelehash-wasm';
import { createLogger } from '../../util/logger.js';
// #endregion
@@ -23,21 +23,22 @@ export async function computeSkeleHash(buffer) {
const base64Data = input.includes(',') ? input.split(',')[1] : input;
input = Buffer.from(base64Data, 'base64');
}
const { data, info } = await sharp(input)
.rotate()
.resize({ width: 200, height: 200, fit: 'inside' })
.ensureAlpha()
.raw()
.toBuffer({ resolveWithObject: true });
const raw = await optimizeImage({
image: input,
width: 200,
height: 200,
fit: 'contain',
format: 'raw',
});
const result = await encodeRGBA(info.width, info.height, data);
const result = await encodeRGBA(raw.width, raw.height, raw.data);
if (!result?.base62) {
throw new Error('encodeRGBA failed to produce a base62 skelehash');
}
return {
base62: result.base62,
width: info.width,
height: info.height,
width: raw.width,
height: raw.height,
hashLength: result.hash?.length ?? 0,
};
}