Skip to content

Commit f2e268b

Browse files
committed
🎉 Initial
0 parents  commit f2e268b

File tree

12 files changed

+762
-0
lines changed

12 files changed

+762
-0
lines changed

.github/workflows/test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Test
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/[email protected]
14+
- uses: actions/[email protected]
15+
with:
16+
node-version: "14.x"
17+
- name: use npm
18+
run: |
19+
npm install
20+
npm test

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/dist

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/node_modules
2+
.github
3+
/test
4+
/image
5+
/dist
6+
renovate.json

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Generate Blurhash from Image URL
2+
3+
:unicorn: Simple utility to generate blurhash from Image URL
4+
5+
![Test](https://github.com/mcnaveen/blurhash-from-url/workflows/Test/badge.svg)
6+
![Downloads this Week](https://img.shields.io/npm/dw/blurhash-from-url)
7+
![Bundle Size](https://img.shields.io/bundlephobia/min/blurhash-from-url)
8+
![Version](https://img.shields.io/npm/v/blurhash-from-url)
9+
10+
![Blurhash from URL](./image/cover.png)
11+
12+
### :package: Requirements
13+
14+
- Node.js 16X LTS or Higher 📦
15+
16+
### :sparkles: Installation
17+
18+
- Install the NPM Package with the below command:
19+
20+
```
21+
npm install blurhash-from-url --save
22+
```
23+
24+
(or)
25+
26+
- Install with Yarn:
27+
28+
```
29+
yarn add blurhash-from-url
30+
```
31+
32+
### :bulb: Usage Example
33+
34+
- Import it in your project
35+
- Pass the URL of the image
36+
- Make sure to use Async/Await function
37+
38+
```javascript
39+
//ES6 Import
40+
import { blurhashFromURL } from "blurhash-from-url";
41+
42+
// Commonjs Import
43+
// const { blurhashFromURL } = require("blurhash-from-url");
44+
45+
async function getBlurhash() {
46+
const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png");
47+
console.log(output);
48+
}
49+
50+
getBlurhash();
51+
```
52+
53+
### :ballot_box_with_check: Example Output
54+
55+
```json
56+
{
57+
encoded: 'UnR.*,kW.TnPt7WBocozpJV@nMkWadofWCV@',
58+
decoded: Uint8ClampedArray(1440000) [
59+
255, 255, 251, 255, 255, 255, 251, 255, 255, 255, 251, 255,
60+
255, 255, 251, 255, 255, 255, 251, 255, 255, 255, 251, 255,
61+
255, 255, 251, 255, 255, 255, 251, 255, 255, 255, 251, 255,
62+
255, 255, 251, 255, 255, 255, 250, 255, 255, 255, 250, 255,
63+
255, 255, 250, 255, 255, 255, 250, 255, 255, 255, 250, 255,
64+
255, 255, 250, 255, 255, 255, 250, 255, 255, 255, 250, 255,
65+
255, 255, 250, 255, 255, 255, 250, 255, 255, 255, 250, 255,
66+
255, 255, 250, 255, 255, 255, 250, 255, 255, 255, 250, 255,
67+
255, 255, 250, 255,
68+
... 1439900 more items
69+
],
70+
width: 600,
71+
height: 600
72+
}
73+
```
74+
75+
---
76+
77+
#### :green_heart: Message
78+
79+
I hope you find this useful. If you have any questions, please create an issue.

image/cover.png

39.2 KB
Loading

package.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "blurhash-from-url",
3+
"version": "0.0.4",
4+
"description": "Simple utility to generate blurhash from Image URL",
5+
"main": "dist/cjs/index.js",
6+
"module": "dist/esm/index.esm.js",
7+
"files": [
8+
"dist"
9+
],
10+
"types": "dist/index.d.ts",
11+
"scripts": {
12+
"build": "rollup -c",
13+
"test": "yarn build && node ./test/index.js",
14+
"watch": "rollup -cw"
15+
},
16+
"license": "MIT",
17+
"dependencies": {
18+
"axios": "^0.27.2",
19+
"blurhash": "^1.1.5",
20+
"sharp": "^0.30.7"
21+
},
22+
"devDependencies": {
23+
"@types/sharp": "^0.30.5",
24+
"rollup": "^2.78.1",
25+
"rollup-plugin-typescript2": "^0.33.0",
26+
"typescript": "^4.8.2"
27+
},
28+
"publishConfig": {
29+
"access": "public"
30+
},
31+
"repository": {
32+
"type": "git",
33+
"url": "git+https://github.com/mcnaveen/blurhash-from-url.git"
34+
},
35+
"author": "mcnaveen <[email protected]>",
36+
"bugs": {
37+
"url": "https://github.com/mcnaveen/blurhash-from-url/issues"
38+
},
39+
"homepage": "https://github.com/mcnaveen/blurhash-from-url#readme",
40+
"keywords": [
41+
"blurhash",
42+
"optimization",
43+
"bundle",
44+
"sharp",
45+
"plaiceholder",
46+
"blurhash-from-url"
47+
]
48+
}

renovate.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
3+
"extends": [
4+
"config:base"
5+
]
6+
}

rollup.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import typescript from "rollup-plugin-typescript2";
2+
3+
import pkg from "./package.json";
4+
5+
export default {
6+
input: "src/index.ts",
7+
output: [
8+
{ file: pkg.main, format: "cjs" },
9+
{ file: pkg.module, format: "es" },
10+
],
11+
external: [
12+
...Object.keys(pkg.dependencies || {}),
13+
...Object.keys(pkg.peerDependencies || {}),
14+
],
15+
16+
plugins: [typescript({ typescript: require("typescript") })],
17+
};

src/index.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import get from "axios";
2+
import { encode, decode } from "blurhash";
3+
import sharp from 'sharp';
4+
5+
export const blurhashFromURL = async (url: string) => {
6+
const image = await get(url, {
7+
responseType: "arraybuffer",
8+
});
9+
const returnedBuffer = Buffer.from(image.data);
10+
11+
const { data, info } = await sharp(returnedBuffer)
12+
.ensureAlpha()
13+
.raw()
14+
.toBuffer({
15+
resolveWithObject: true,
16+
});
17+
const encoded = encode(
18+
new Uint8ClampedArray(data),
19+
info.width,
20+
info.height,
21+
4,
22+
4
23+
);
24+
const decoded = decode(encoded, info.width, info.height);
25+
26+
const output = {
27+
encoded: encoded,
28+
decoded: decoded,
29+
width: info.width,
30+
height: info.height,
31+
};
32+
33+
return output;
34+
};

test/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { blurhashFromURL } = require("../dist/cjs/index.js");
2+
3+
async function getBlurhash() {
4+
const output = await blurhashFromURL("https://i.imgur.com/NhfEdg2.png");
5+
console.log(output);
6+
}
7+
8+
getBlurhash();

0 commit comments

Comments
 (0)