Skip to content

Commit

Permalink
Merge pull request #21 from mcnaveen/refactor/improvements
Browse files Browse the repository at this point in the history
refactor(resize): ♻️ add resize and fit in sharp
  • Loading branch information
mcnaveen authored Jan 11, 2023
2 parents d13655d + 17ce4ea commit adc3ede
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
},
Expand Down
31 changes: 23 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,54 @@
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;
decoded: Uint8ClampedArray;
width: number;
height: number;
}

export const blurhashFromURL = async (url: string) => {
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",
})
.ensureAlpha()
.raw()
.toBuffer({
resolveWithObject: true,
});


const encoded = encode(
new Uint8ClampedArray(data),
info.width,
info.height,
4,
4
);
const decoded = decode(encoded, info.width, info.height);

const output: IOutput = {
encoded: encoded,
decoded: decoded,
width: info.width,
height: info.height,
encoded,
width,
height,
};

return output;
Expand Down
4 changes: 3 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -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: 600,
});
console.log(output);
}

Expand Down
16 changes: 15 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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==
Expand Down Expand Up @@ -1181,6 +1188,13 @@ pupa@^2.1.1:
dependencies:
escape-goat "^2.0.0"

[email protected]:
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"
Expand Down

0 comments on commit adc3ede

Please sign in to comment.