11import fetch from "node-fetch" ;
22import { encode , decode } from "blurhash" ;
33import 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+ }
514export 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 ;
0 commit comments