* Create a Jimp instance from a URL, a file path, or a Buffer * @example * ```ts * import { Jimp } from "jimp"; * * // Read from a file path * const image = await Jimp.read("test/image.png"); * * // Read from a URL * const image = await Jimp.read("https:/
(
url: string | Buffer | ArrayBuffer,
options?: MimeTypeToDecodeOptions
)
| 232 | * ``` |
| 233 | */ |
| 234 | static async read( |
| 235 | url: string | Buffer | ArrayBuffer, |
| 236 | options?: MimeTypeToDecodeOptions |
| 237 | ) { |
| 238 | if (Buffer.isBuffer(url) || url instanceof ArrayBuffer) { |
| 239 | return this.fromBuffer(url); |
| 240 | } |
| 241 | |
| 242 | if (existsSync(url)) { |
| 243 | return this.fromBuffer(await readFile(url)); |
| 244 | } |
| 245 | |
| 246 | const [fetchErr, response] = await to(fetch(url)); |
| 247 | |
| 248 | if (fetchErr) { |
| 249 | throw new Error(`Could not load Buffer from URL: ${url}`); |
| 250 | } |
| 251 | |
| 252 | if (!response.ok) { |
| 253 | throw new Error(`HTTP Status ${response.status} for url ${url}`); |
| 254 | } |
| 255 | |
| 256 | const [arrayBufferErr, data] = await to(response.arrayBuffer()); |
| 257 | |
| 258 | if (arrayBufferErr) { |
| 259 | throw new Error(`Could not load Buffer from ${url}`); |
| 260 | } |
| 261 | |
| 262 | const buffer = bufferFromArrayBuffer(data); |
| 263 | return this.fromBuffer(buffer, options); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Create a Jimp instance from a bitmap. |