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
16
width : number ;
8
17
height : number ;
9
18
}
10
19
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 ;
12
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 )
18
30
. resize ( size , size , {
19
31
fit : "inside" ,
20
32
} )
@@ -24,6 +36,7 @@ export const blurhashFromURL = async (url: string, { size = 32 }: { size?: numbe
24
36
resolveWithObject : true ,
25
37
} ) ;
26
38
39
+
27
40
const encoded = encode (
28
41
new Uint8ClampedArray ( data ) ,
29
42
info . width ,
@@ -33,9 +46,9 @@ export const blurhashFromURL = async (url: string, { size = 32 }: { size?: numbe
33
46
) ;
34
47
35
48
const output : IOutput = {
36
- encoded : encoded ,
37
- width : info . width ,
38
- height : info . height ,
49
+ encoded,
50
+ width,
51
+ height,
39
52
} ;
40
53
41
54
return output ;
0 commit comments