项目文件夹

文件
wehub-resource-sync 98e40dac97
CLI Smoke Test / smoke-test-linux (20) (push) Has been cancelled
CLI Smoke Test / smoke-test-linux (24) (push) Has been cancelled
CLI Smoke Test / smoke-test-windows (20) (push) Has been cancelled
CLI Smoke Test / smoke-test-windows (24) (push) Has been cancelled
Expo App TypeScript typecheck / typecheck (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:40:49 +08:00

38 行
1.1 KiB
TypeScript

import { thumbhash } from "./thumbhash";
export async function processImage(src: Buffer) {
const sharp = (await import("sharp")).default;
// Read image
let meta = await sharp(src).metadata();
let width = meta.width!;
let height = meta.height!;
if (meta.format !== 'png' && meta.format !== 'jpeg') {
throw new Error('Unsupported image format');
}
// Resize
let targetWidth = 100;
let targetHeight = 100;
if (width > height) {
targetHeight = Math.round(height * targetWidth / width);
} else if (height > width) {
targetWidth = Math.round(width * targetHeight / height);
}
// Resize image
const { data, info } = await sharp(src).resize(targetWidth, targetHeight).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
// Thumbhash
const binaryThumbHash = thumbhash(info.width, info.height, data);
const thumbhashStr = Buffer.from(binaryThumbHash).toString('base64');
return {
pixels: data,
width: width,
height: height,
thumbhash: thumbhashStr,
format: meta.format
};
}