Skip to content

Commit 17ce4ea

Browse files
committed
refactor(image): ♻️ preserve original image width height
1 parent 9ce4106 commit 17ce4ea

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
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: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
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;
716
width: number;
817
height: number;
918
}
1019

11-
export const blurhashFromURL = async (url: string, { size = 32 }: { size?: number } = {}) => {
20+
export const blurhashFromURL = async (url: string, options: IOptions = {}) => {
21+
const { size = 32 } = options;
1222

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)
1830
.resize(size, size, {
1931
fit: "inside",
2032
})
@@ -24,6 +36,7 @@ export const blurhashFromURL = async (url: string, { size = 32 }: { size?: numbe
2436
resolveWithObject: true,
2537
});
2638

39+
2740
const encoded = encode(
2841
new Uint8ClampedArray(data),
2942
info.width,
@@ -33,9 +46,9 @@ export const blurhashFromURL = async (url: string, { size = 32 }: { size?: numbe
3346
);
3447

3548
const output: IOutput = {
36-
encoded: encoded,
37-
width: info.width,
38-
height: info.height,
49+
encoded,
50+
width,
51+
height,
3952
};
4053

4154
return output;

test/index.js

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

33
async function getBlurhash() {
44
const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png", {
5-
size: 32,
5+
size: 600,
66
});
77
console.log(output);
88
}

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)