@alttiri/get-image-data
Advanced tools
Comparing version 0.0.2-20241214 to 0.0.3-20241215
{ | ||
"name": "@alttiri/get-image-data", | ||
"version": "0.0.2-20241214", | ||
"version": "0.0.3-20241215", | ||
"description": "A simple library to get ImageData on Node.js and browsers", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/AlttiRi/get-image-data", |
@@ -5,6 +5,18 @@ # get-image-data | ||
- In Node.js use `getImageDataWithCanvas` to get `ImageData` with `sharp` library. | ||
```bash | ||
npm i @alttiri/get-image-data | ||
``` | ||
_Do not forget to install `sharp`. It's not included as a dependency._ | ||
--- | ||
- For browsers use `getImageDataWithCanvas`. It uses `OffscreenCanvas` to get the `ImageData` for `File`/`Blob` input. | ||
- In Node.js use [`getImageDataWithSharp`](https://github.com/AlttiRi/get-image-data/blob/master/src/get-with-sharp.ts) to get `ImageData` with `sharp` library. | ||
_Do not forget to install [`sharp`](https://www.npmjs.com/package/sharp) (`npm i sharp`). It's not included as a dependency._ | ||
```bash | ||
npm i @alttiri/get-image-data sharp | ||
``` | ||
- For browsers just use [`getImageDataWithCanvas`](https://github.com/AlttiRi/get-image-data/blob/master/src/get-with-canvas.ts). | ||
It uses `OffscreenCanvas` to get the `ImageData` for `File`/`Blob` input. | ||
import { ImageDataLike } from "./types.js"; | ||
export declare function getImageDataWithSharp(imagePath: string): Promise<ImageDataLike>; | ||
export declare function getImageDataWithSharp(inputData: string | ArrayBufferLike, longPathFix?: boolean): Promise<ImageDataLike>; |
@@ -0,3 +1,4 @@ | ||
import path from "node:path"; | ||
let sharpCache; | ||
export async function getImageDataWithSharp(imagePath) { | ||
export async function getImageDataWithSharp(inputData, longPathFix = true) { | ||
if (!sharpCache) { | ||
@@ -7,4 +8,7 @@ const sharpModule = await import("sharp"); | ||
} | ||
if (longPathFix && typeof inputData === "string") { | ||
inputData = path.toNamespacedPath(inputData); | ||
} | ||
const sharp = sharpCache; | ||
const imageData = await sharp(imagePath) | ||
const imageData = await sharp(inputData) | ||
.ensureAlpha() | ||
@@ -11,0 +15,0 @@ .raw() |
5286
56
22