Skip to content

Commit 4e9965d

Browse files
committed
0.0.7
1 parent 5220602 commit 4e9965d

File tree

4 files changed

+62
-16
lines changed

4 files changed

+62
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "blurhash-from-url",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "Simple utility to generate blurhash from Image URL",
55
"main": "dist/index.js",
66
"module": "dist/index.esm.js",

rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default {
1111
external: [
1212
...Object.keys(pkg.dependencies || {}),
1313
...Object.keys(pkg.peerDependencies || {}),
14+
"fs",
1415
],
1516

1617
plugins: [typescript({ typescript: require("typescript") })],

src/index.ts

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,60 @@
11
import fetch from "node-fetch";
2-
import { encode, decode } from "blurhash";
2+
import fs from "fs";
3+
import { encode } from "blurhash";
34
import sharp from 'sharp';
45
import sizeOf from "image-size";
56

67
export interface IOptions {
78
size?: number;
9+
offline?: boolean;
810
}
911

10-
export interface IInput {
11-
url: string;
12-
options?: IOptions;
13-
}
1412
export interface IOutput {
1513
encoded: string;
1614
width: number;
1715
height: number;
1816
}
1917

20-
export const blurhashFromURL = async (url: string, options: IOptions = {}) => {
21-
const { size = 32 } = options;
18+
/**
19+
* Generate a Blurhash string from a given image URL or local path.
20+
*
21+
* @param {string} source - The image URL or local path to the image file.
22+
* @param {IOptions} [options] - The optional configuration options.
23+
* @param {number} [options.size=32] - The desired size of the image for encoding the Blurhash.
24+
* @param {boolean} [options.offline=false] - Set to `true` if the image source is a local path, `false` if it's a URL.
25+
* @returns {Promise<IOutput>} The Promise that resolves to the encoded Blurhash string, along with the image width and height.
26+
* @default size 32
27+
* @default offline false
28+
* @example
29+
* ```js
30+
* import { blurhashFromURL } from "blurhash-from-url";
31+
*
32+
* const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png", {
33+
* size: 32,
34+
* });
35+
*
36+
* console.log(output);
37+
* ```
38+
*/
39+
export const blurhashFromURL = async (source: string, options: IOptions = {}): Promise<IOutput> => {
40+
const { size = 32, offline = false } = options;
41+
42+
let width, height, returnedBuffer;
2243

23-
const response = await fetch(url);
24-
const arrayBuffer = await response.arrayBuffer();
25-
const returnedBuffer = Buffer.from(arrayBuffer);
44+
if (offline) {
45+
const { width: localWidth, height: localHeight } = sizeOf(source);
46+
width = localWidth;
47+
height = localHeight;
48+
returnedBuffer = await sharp(fs.readFileSync(source)).toBuffer();
49+
} else {
50+
const response = await fetch(source);
51+
const arrayBuffer = await response.arrayBuffer();
52+
returnedBuffer = Buffer.from(arrayBuffer);
2653

27-
const { width, height, } = sizeOf(returnedBuffer);
54+
const { width: remoteWidth, height: remoteHeight } = sizeOf(returnedBuffer);
55+
width = remoteWidth;
56+
height = remoteHeight;
57+
}
2858

2959
const { info, data } = await sharp(returnedBuffer)
3060
.resize(size, size, {
@@ -36,7 +66,6 @@ export const blurhashFromURL = async (url: string, options: IOptions = {}) => {
3666
resolveWithObject: true,
3767
});
3868

39-
4069
const encoded = encode(
4170
new Uint8ClampedArray(data),
4271
info.width,

test/index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
const { blurhashFromURL } = require("../dist/index.js");
22

3-
async function getBlurhash() {
3+
/* Remote Image */
4+
async function getRemoteBlurhash() {
45
const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png", {
5-
size: 600,
6+
size: 32,
7+
offline: true,
68
});
9+
console.log("\n 🪄 Remote Image Blurhash\n");
710
console.log(output);
11+
console.log("\n");
812
}
913

10-
getBlurhash();
14+
getRemoteBlurhash();
15+
16+
/* Local Image */
17+
async function getLocalBlurhash() {
18+
const output = await blurhashFromURL("./image/cover.png", {
19+
size: 32,
20+
offline: true,
21+
});
22+
console.log("\n 🪄 Local Image Blurhash\n");
23+
console.log(output);
24+
}
25+
26+
getLocalBlurhash();

0 commit comments

Comments
 (0)