Skip to content

Commit adc3ede

Browse files
authored
Merge pull request #21 from mcnaveen/refactor/improvements
refactor(resize): ♻️ add resize and fit in sharp
2 parents d13655d + 17ce4ea commit adc3ede

File tree

5 files changed

+56
-11
lines changed

5 files changed

+56
-11
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ getBlurhash();
7272
}
7373
```
7474

75+
### Optional Size Parameter
76+
77+
By default, the image is resized to 32x32. You can pass the size as an optional parameter.
78+
79+
```javascript
80+
async function getBlurhash() {
81+
const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png", {
82+
size: 64,
83+
});
84+
console.log(output);
85+
}
86+
```
87+
7588
---
7689

7790
#### :green_heart: Message

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "blurhash-from-url",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Simple utility to generate blurhash from Image URL",
55
"main": "dist/index.js",
66
"module": "dist/index.esm.js",
@@ -18,6 +18,7 @@
1818
"license": "MIT",
1919
"dependencies": {
2020
"blurhash": "^1.1.5",
21+
"image-size": "^1.0.2",
2122
"node-fetch": "2.6.7",
2223
"sharp": "^0.30.7"
2324
},

src/index.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,54 @@
11
import fetch from "node-fetch";
22
import { encode, decode } from "blurhash";
33
import sharp from 'sharp';
4+
import sizeOf from "image-size";
45

6+
export interface IOptions {
7+
size?: number;
8+
}
9+
10+
export interface IInput {
11+
url: string;
12+
options?: IOptions;
13+
}
514
export interface IOutput {
615
encoded: string;
7-
decoded: Uint8ClampedArray;
816
width: number;
917
height: number;
1018
}
1119

12-
export const blurhashFromURL = async (url: string) => {
20+
export const blurhashFromURL = async (url: string, options: IOptions = {}) => {
21+
const { size = 32 } = options;
22+
1323
const response = await fetch(url);
1424
const arrayBuffer = await response.arrayBuffer();
1525
const returnedBuffer = Buffer.from(arrayBuffer);
1626

17-
const { data, info } = await sharp(returnedBuffer)
27+
const { width, height, } = sizeOf(returnedBuffer);
28+
29+
const { info, data } = await sharp(returnedBuffer)
30+
.resize(size, size, {
31+
fit: "inside",
32+
})
1833
.ensureAlpha()
1934
.raw()
2035
.toBuffer({
2136
resolveWithObject: true,
2237
});
38+
39+
2340
const encoded = encode(
2441
new Uint8ClampedArray(data),
2542
info.width,
2643
info.height,
2744
4,
2845
4
2946
);
30-
const decoded = decode(encoded, info.width, info.height);
3147

3248
const output: IOutput = {
33-
encoded: encoded,
34-
decoded: decoded,
35-
width: info.width,
36-
height: info.height,
49+
encoded,
50+
width,
51+
height,
3752
};
3853

3954
return output;

test/index.js

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

33
async function getBlurhash() {
4-
const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png");
4+
const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png", {
5+
size: 600,
6+
});
57
console.log(output);
68
}
79

yarn.lock

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,13 @@ ieee754@^1.1.13:
733733
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352"
734734
integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==
735735

736+
image-size@^1.0.2:
737+
version "1.0.2"
738+
resolved "https://registry.yarnpkg.com/image-size/-/image-size-1.0.2.tgz#d778b6d0ab75b2737c1556dd631652eb963bc486"
739+
integrity sha512-xfOoWjceHntRb3qFCrh5ZFORYH8XCdYpASltMhZ/Q0KZiOwjdE/Yl2QCiWdwD+lygV5bMCvauzgu5PxBX/Yerg==
740+
dependencies:
741+
queue "6.0.2"
742+
736743
import-fresh@^3.0.0, import-fresh@^3.2.1:
737744
version "3.3.0"
738745
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b"
@@ -759,7 +766,7 @@ inflight@^1.0.4:
759766
once "^1.3.0"
760767
wrappy "1"
761768

762-
inherits@2, inherits@^2.0.3, inherits@^2.0.4:
769+
inherits@2, inherits@^2.0.3, inherits@^2.0.4, inherits@~2.0.3:
763770
version "2.0.4"
764771
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
765772
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
@@ -1181,6 +1188,13 @@ pupa@^2.1.1:
11811188
dependencies:
11821189
escape-goat "^2.0.0"
11831190

1191+
1192+
version "6.0.2"
1193+
resolved "https://registry.yarnpkg.com/queue/-/queue-6.0.2.tgz#b91525283e2315c7553d2efa18d83e76432fed65"
1194+
integrity sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA==
1195+
dependencies:
1196+
inherits "~2.0.3"
1197+
11841198
randombytes@^2.1.0:
11851199
version "2.1.0"
11861200
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"

0 commit comments

Comments
 (0)