@alttiri/get-image-data
Version:
A simple library to get ImageData on Node.js and browsers
27 lines (26 loc) • 799 B
JavaScript
import path from "node:path";
let sharpCache;
export async function getImageDataWithSharp(inputData, longPathFix = true) {
if (!sharpCache) {
const sharpModule = await import("sharp");
sharpCache = sharpModule.default;
}
if (longPathFix && typeof inputData === "string") {
inputData = path.toNamespacedPath(inputData);
}
else if (ArrayBuffer.isView(inputData)) {
inputData = inputData.buffer;
}
const sharp = sharpCache;
const imageData = await sharp(inputData)
.ensureAlpha()
.raw()
.toBuffer({ resolveWithObject: true });
const { data, info } = imageData;
return {
width: info.width,
height: info.height,
data: new Uint8ClampedArray(data),
colorSpace: "srgb",
};
}