File tree Expand file tree Collapse file tree 5 files changed +56
-11
lines changed Expand file tree Collapse file tree 5 files changed +56
-11
lines changed Original file line number Diff line number Diff line change @@ -72,6 +72,19 @@ getBlurhash();
72
72
}
73
73
```
74
74
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
+
75
88
---
76
89
77
90
#### :green_heart : Message
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " blurhash-from-url" ,
3
- "version" : " 0.0.5 " ,
3
+ "version" : " 0.0.6 " ,
4
4
"description" : " Simple utility to generate blurhash from Image URL" ,
5
5
"main" : " dist/index.js" ,
6
6
"module" : " dist/index.esm.js" ,
18
18
"license" : " MIT" ,
19
19
"dependencies" : {
20
20
"blurhash" : " ^1.1.5" ,
21
+ "image-size" : " ^1.0.2" ,
21
22
"node-fetch" : " 2.6.7" ,
22
23
"sharp" : " ^0.30.7"
23
24
},
Original file line number Diff line number Diff line change 1
1
import fetch from "node-fetch" ;
2
2
import { encode , decode } from "blurhash" ;
3
3
import sharp from 'sharp' ;
4
+ import sizeOf from "image-size" ;
4
5
6
+ export interface IOptions {
7
+ size ?: number ;
8
+ }
9
+
10
+ export interface IInput {
11
+ url : string ;
12
+ options ?: IOptions ;
13
+ }
5
14
export interface IOutput {
6
15
encoded : string ;
7
- decoded : Uint8ClampedArray ;
8
16
width : number ;
9
17
height : number ;
10
18
}
11
19
12
- export const blurhashFromURL = async ( url : string ) => {
20
+ export const blurhashFromURL = async ( url : string , options : IOptions = { } ) => {
21
+ const { size = 32 } = options ;
22
+
13
23
const response = await fetch ( url ) ;
14
24
const arrayBuffer = await response . arrayBuffer ( ) ;
15
25
const returnedBuffer = Buffer . from ( arrayBuffer ) ;
16
26
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
+ } )
18
33
. ensureAlpha ( )
19
34
. raw ( )
20
35
. toBuffer ( {
21
36
resolveWithObject : true ,
22
37
} ) ;
38
+
39
+
23
40
const encoded = encode (
24
41
new Uint8ClampedArray ( data ) ,
25
42
info . width ,
26
43
info . height ,
27
44
4 ,
28
45
4
29
46
) ;
30
- const decoded = decode ( encoded , info . width , info . height ) ;
31
47
32
48
const output : IOutput = {
33
- encoded : encoded ,
34
- decoded : decoded ,
35
- width : info . width ,
36
- height : info . height ,
49
+ encoded,
50
+ width,
51
+ height,
37
52
} ;
38
53
39
54
return output ;
Original file line number Diff line number Diff line change 1
1
const { blurhashFromURL } = require ( "../dist/index.js" ) ;
2
2
3
3
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
+ } ) ;
5
7
console . log ( output ) ;
6
8
}
7
9
Original file line number Diff line number Diff line change @@ -733,6 +733,13 @@ ieee754@^1.1.13:
733
733
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352"
734
734
integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==
735
735
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
+
736
743
import-fresh@^3.0.0, import-fresh@^3.2.1 :
737
744
version "3.3.0"
738
745
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b"
@@ -759,7 +766,7 @@ inflight@^1.0.4:
759
766
once "^1.3.0"
760
767
wrappy "1"
761
768
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 :
763
770
version "2.0.4"
764
771
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
765
772
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
@@ -1181,6 +1188,13 @@ pupa@^2.1.1:
1181
1188
dependencies :
1182
1189
escape-goat "^2.0.0"
1183
1190
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
+
1184
1198
randombytes@^2.1.0 :
1185
1199
version "2.1.0"
1186
1200
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"
You can’t perform that action at this time.
0 commit comments