From 9ce4106843723f65699977dcce4b89b1656dd06f Mon Sep 17 00:00:00 2001 From: Naveen MC <8493007+mcnaveen@users.noreply.github.com> Date: Tue, 10 Jan 2023 20:38:32 +0530 Subject: [PATCH] refactor(resize): :recycle: add resize and fit in sharp --- src/index.ts | 10 ++++++---- test/index.js | 4 +++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index d500a85..c1099e4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,22 +4,26 @@ import sharp from 'sharp'; export interface IOutput { encoded: string; - decoded: Uint8ClampedArray; width: number; height: number; } -export const blurhashFromURL = async (url: string) => { +export const blurhashFromURL = async (url: string, { size = 32 }: { size?: number } = {}) => { + const response = await fetch(url); const arrayBuffer = await response.arrayBuffer(); const returnedBuffer = Buffer.from(arrayBuffer); const { data, info } = await sharp(returnedBuffer) + .resize(size, size, { + fit: "inside", + }) .ensureAlpha() .raw() .toBuffer({ resolveWithObject: true, }); + const encoded = encode( new Uint8ClampedArray(data), info.width, @@ -27,11 +31,9 @@ export const blurhashFromURL = async (url: string) => { 4, 4 ); - const decoded = decode(encoded, info.width, info.height); const output: IOutput = { encoded: encoded, - decoded: decoded, width: info.width, height: info.height, }; diff --git a/test/index.js b/test/index.js index 19f03b9..f49e16f 100644 --- a/test/index.js +++ b/test/index.js @@ -1,7 +1,9 @@ const { blurhashFromURL } = require("../dist/index.js"); async function getBlurhash() { - const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png"); + const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png", { + size: 32, + }); console.log(output); }