diff --git a/README.md b/README.md index 479cbcd..c3730ec 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,19 @@ getBlurhash(); } ``` +### Optional Size Parameter + +By default, the image is resized to 32x32. You can pass the size as an optional parameter. + +```javascript +async function getBlurhash() { + const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png", { + size: 64, + }); + console.log(output); +} +``` + --- #### :green_heart: Message diff --git a/package.json b/package.json index ad221b2..dc92fa8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "blurhash-from-url", - "version": "0.0.5", + "version": "0.0.6", "description": "Simple utility to generate blurhash from Image URL", "main": "dist/index.js", "module": "dist/index.esm.js", @@ -18,6 +18,7 @@ "license": "MIT", "dependencies": { "blurhash": "^1.1.5", + "image-size": "^1.0.2", "node-fetch": "2.6.7", "sharp": "^0.30.7" }, diff --git a/src/index.ts b/src/index.ts index c1099e4..784fff4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,20 +1,32 @@ import fetch from "node-fetch"; import { encode, decode } from "blurhash"; import sharp from 'sharp'; +import sizeOf from "image-size"; +export interface IOptions { + size?: number; +} + +export interface IInput { + url: string; + options?: IOptions; +} export interface IOutput { encoded: string; width: number; height: number; } -export const blurhashFromURL = async (url: string, { size = 32 }: { size?: number } = {}) => { +export const blurhashFromURL = async (url: string, options: IOptions = {}) => { + const { size = 32 } = options; const response = await fetch(url); const arrayBuffer = await response.arrayBuffer(); const returnedBuffer = Buffer.from(arrayBuffer); - const { data, info } = await sharp(returnedBuffer) + const { width, height, } = sizeOf(returnedBuffer); + + const { info, data } = await sharp(returnedBuffer) .resize(size, size, { fit: "inside", }) @@ -24,6 +36,7 @@ export const blurhashFromURL = async (url: string, { size = 32 }: { size?: numbe resolveWithObject: true, }); + const encoded = encode( new Uint8ClampedArray(data), info.width, @@ -33,9 +46,9 @@ export const blurhashFromURL = async (url: string, { size = 32 }: { size?: numbe ); const output: IOutput = { - encoded: encoded, - width: info.width, - height: info.height, + encoded, + width, + height, }; return output; diff --git a/test/index.js b/test/index.js index f49e16f..6de5844 100644 --- a/test/index.js +++ b/test/index.js @@ -2,7 +2,7 @@ const { blurhashFromURL } = require("../dist/index.js"); async function getBlurhash() { const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png", { - size: 32, + size: 600, }); console.log(output); } diff --git a/yarn.lock b/yarn.lock index acb04e4..a0a61b6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -733,6 +733,13 @@ ieee754@^1.1.13: resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352" integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA== +image-size@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/image-size/-/image-size-1.0.2.tgz#d778b6d0ab75b2737c1556dd631652eb963bc486" + integrity sha512-xfOoWjceHntRb3qFCrh5ZFORYH8XCdYpASltMhZ/Q0KZiOwjdE/Yl2QCiWdwD+lygV5bMCvauzgu5PxBX/Yerg== + dependencies: + queue "6.0.2" + import-fresh@^3.0.0, import-fresh@^3.2.1: version "3.3.0" resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b" @@ -759,7 +766,7 @@ inflight@^1.0.4: once "^1.3.0" wrappy "1" -inherits@2, inherits@^2.0.3, inherits@^2.0.4: +inherits@2, inherits@^2.0.3, inherits@^2.0.4, inherits@~2.0.3: version "2.0.4" resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== @@ -1181,6 +1188,13 @@ pupa@^2.1.1: dependencies: escape-goat "^2.0.0" +queue@6.0.2: + version "6.0.2" + resolved "https://registry.yarnpkg.com/queue/-/queue-6.0.2.tgz#b91525283e2315c7553d2efa18d83e76432fed65" + integrity sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA== + dependencies: + inherits "~2.0.3" + randombytes@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"