Skip to content

Commit 9ce4106

Browse files
committed
refactor(resize): ♻️ add resize and fit in sharp
1 parent d13655d commit 9ce4106

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,36 @@ import sharp from 'sharp';
44

55
export interface IOutput {
66
encoded: string;
7-
decoded: Uint8ClampedArray;
87
width: number;
98
height: number;
109
}
1110

12-
export const blurhashFromURL = async (url: string) => {
11+
export const blurhashFromURL = async (url: string, { size = 32 }: { size?: number } = {}) => {
12+
1313
const response = await fetch(url);
1414
const arrayBuffer = await response.arrayBuffer();
1515
const returnedBuffer = Buffer.from(arrayBuffer);
1616

1717
const { data, info } = await sharp(returnedBuffer)
18+
.resize(size, size, {
19+
fit: "inside",
20+
})
1821
.ensureAlpha()
1922
.raw()
2023
.toBuffer({
2124
resolveWithObject: true,
2225
});
26+
2327
const encoded = encode(
2428
new Uint8ClampedArray(data),
2529
info.width,
2630
info.height,
2731
4,
2832
4
2933
);
30-
const decoded = decode(encoded, info.width, info.height);
3134

3235
const output: IOutput = {
3336
encoded: encoded,
34-
decoded: decoded,
3537
width: info.width,
3638
height: info.height,
3739
};

test/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const { blurhashFromURL } = require("../dist/index.js");
22

33
async function getBlurhash() {
4-
const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png");
4+
const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png", {
5+
size: 32,
6+
});
57
console.log(output);
68
}
79

0 commit comments

Comments
 (0)