File tree Expand file tree Collapse file tree 2 files changed +9
-5
lines changed Expand file tree Collapse file tree 2 files changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -4,34 +4,36 @@ import sharp from 'sharp';
4
4
5
5
export interface IOutput {
6
6
encoded : string ;
7
- decoded : Uint8ClampedArray ;
8
7
width : number ;
9
8
height : number ;
10
9
}
11
10
12
- export const blurhashFromURL = async ( url : string ) => {
11
+ export const blurhashFromURL = async ( url : string , { size = 32 } : { size ?: number } = { } ) => {
12
+
13
13
const response = await fetch ( url ) ;
14
14
const arrayBuffer = await response . arrayBuffer ( ) ;
15
15
const returnedBuffer = Buffer . from ( arrayBuffer ) ;
16
16
17
17
const { data, info } = await sharp ( returnedBuffer )
18
+ . resize ( size , size , {
19
+ fit : "inside" ,
20
+ } )
18
21
. ensureAlpha ( )
19
22
. raw ( )
20
23
. toBuffer ( {
21
24
resolveWithObject : true ,
22
25
} ) ;
26
+
23
27
const encoded = encode (
24
28
new Uint8ClampedArray ( data ) ,
25
29
info . width ,
26
30
info . height ,
27
31
4 ,
28
32
4
29
33
) ;
30
- const decoded = decode ( encoded , info . width , info . height ) ;
31
34
32
35
const output : IOutput = {
33
36
encoded : encoded ,
34
- decoded : decoded ,
35
37
width : info . width ,
36
38
height : info . height ,
37
39
} ;
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 : 32 ,
6
+ } ) ;
5
7
console . log ( output ) ;
6
8
}
7
9
You can’t perform that action at this time.
0 commit comments