beautiful-image 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +99 -35
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,24 +1,13 @@
1
1
  # beautiful-image
2
2
 
3
- Image optimization and filters powered by Rust/WASM.
3
+ Compress and optimize images with minimal quality loss in the browser or on the server. Powered by Rust/WASM with zero native dependencies.
4
4
 
5
- Resize, compress, and apply filters to images in the browser. Runs fast via WebAssembly.
5
+ ## How it works
6
6
 
7
- ## Why Rust and WASM?
7
+ Most tools that compress images do it at the cost of visible quality degradation. `beautiful-image` combines resize, sharpening, and JPEG encoding tuned to produce the smallest file size while keeping the image looking sharp and clean.
8
8
 
9
- Browsers can resize and encode JPEG natively, but **cannot apply filters** like sharpen, blur, or color adjustments. Rust gives us:
10
-
11
- - Image processing algorithms (sharpen, blur, etc.) that browsers don't have
12
- - Fast execution via WebAssembly
13
- - Battle-tested `image` crate with optimized implementations
14
-
15
- ## Use Cases
16
-
17
- - **E-commerce** - Bulk optimize product images before upload
18
- - **CMS/Blogs** - Process images on the client before saving
19
- - **Social apps** - Apply filters and compress photos
20
- - **Photo editors** - Real-time filter previews
21
- - **Blurred previews** - Show blurred thumbnails before unlocking content
9
+ - **Browser** uses the native Canvas API for fast GPU-accelerated decode and resize, then hands off to WASM for sharpening and JPEG encoding
10
+ - **Node.js** runs the full pipeline in WASM (decode, resize, filters, encode), making it ideal for serverless environments like AWS Lambda or Google Cloud Functions with no native dependencies
22
11
 
23
12
  ## Install
24
13
 
@@ -26,37 +15,111 @@ Browsers can resize and encode JPEG natively, but **cannot apply filters** like
26
15
  npm install beautiful-image
27
16
  ```
28
17
 
29
- ## Usage
18
+ ## Browser
19
+
20
+ ```html
21
+ <input type="file" id="upload" accept="image/*" />
22
+ <img id="preview" />
23
+ ```
30
24
 
31
25
  ```typescript
32
26
  import { image } from 'beautiful-image'
33
27
 
34
- const result = await image(file)
35
- .resize(1000)
36
- .sharpen(1.5)
37
- .toJpeg(75)
28
+ const input = document.getElementById('upload') as HTMLInputElement
29
+
30
+ input.addEventListener('change', async () => {
31
+ const file = input.files?.[0]
32
+ if (!file) return
33
+
34
+ const result = await image(file)
35
+ .resize(1200)
36
+ .sharpen()
37
+ .toJpeg(80)
38
38
 
39
- // result.blob - optimized image
40
- // result.originalSize - original size in bytes
41
- // result.optimizedSize - new size in bytes
42
- // result.compressionRatio - 0.85 = 85% smaller
39
+ // result.blob optimized image as Blob
40
+ // result.originalSize original size in bytes
41
+ // result.optimizedSize new size in bytes
42
+ // result.compressionRatio 0.85 = 85% smaller
43
+ // result.width / result.height
44
+
45
+ document.getElementById('preview').src = URL.createObjectURL(result.blob)
46
+ })
43
47
  ```
44
48
 
45
- ## Filters
49
+ For a full working demo see [`examples/web-demo`](./examples/web-demo).
50
+
51
+ ## Node.js
52
+
53
+ ```typescript
54
+ import { image } from 'beautiful-image/node'
55
+ import { readFileSync, writeFileSync } from 'node:fs'
56
+
57
+ const input = readFileSync('./photo.jpg')
58
+
59
+ const result = await image(input)
60
+ .resize(1200)
61
+ .sharpen()
62
+ .toJpeg(80)
63
+
64
+ writeFileSync('./optimized.jpg', result.data)
65
+
66
+ // result.data — optimized image as Buffer
67
+ // result.originalSize — original size in bytes
68
+ // result.optimizedSize — new size in bytes
69
+ // result.compressionRatio — 0.85 = 85% smaller
70
+ ```
71
+
72
+ ### Lambda + S3 example
73
+
74
+ ```typescript
75
+ import { S3Client, GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3'
76
+ import { image } from 'beautiful-image/node'
77
+
78
+ const s3 = new S3Client({})
79
+
80
+ export const handler = async (event: any) => {
81
+ const bucket = event.Records[0].s3.bucket.name
82
+ const key = decodeURIComponent(event.Records[0].s3.object.key)
83
+
84
+ const { Body } = await s3.send(new GetObjectCommand({ Bucket: bucket, Key: key }))
85
+ const input = Buffer.from(await Body!.transformToByteArray())
86
+
87
+ const result = await image(input).resize(1200).sharpen().toJpeg(80)
88
+
89
+ await s3.send(new PutObjectCommand({
90
+ Bucket: bucket,
91
+ Key: `optimized/${key}`,
92
+ Body: result.data,
93
+ ContentType: 'image/jpeg',
94
+ }))
95
+ }
96
+ ```
97
+
98
+ ## API
99
+
100
+ All methods are available in both browser and Node.js:
46
101
 
47
102
  ```typescript
48
103
  image(file)
49
- .resize(width) // resize maintaining aspect ratio
50
- .sharpen(sigma) // 1.0 subtle, 3.0 strong
51
- .blur(sigma) // gaussian blur
52
- .brightness(value) // -100 to 100
53
- .contrast(value) // -100 to 100
54
- .hueRotate(degrees) // -180 to 180
55
- .grayscale() // black & white
56
- .invert() // invert colors
57
- .toJpeg(quality) // 1-100
104
+ .resize(width) // resize maintaining aspect ratio
105
+ .sharpen(sigma) // default 1.5 subtle to strong
106
+ .blur(sigma) // gaussian blur
107
+ .brightness(value) // -100 to 100
108
+ .contrast(value) // -100 to 100
109
+ .hueRotate(degrees) // -180 to 180
110
+ .grayscale() // black & white
111
+ .invert() // invert colors
112
+ .toJpeg(quality) // 1-100
58
113
  ```
59
114
 
115
+ ## Use Cases
116
+
117
+ - **E-commerce** — Optimize product images before upload, saving storage and bandwidth
118
+ - **CMS/Blogs** — Process images on the client before saving, no server needed
119
+ - **Social apps** — Compress and filter photos before posting
120
+ - **Lambda/Cloud Functions** — Automatically optimize images on upload to S3 or cloud storage
121
+ - **Blurred previews** — Generate blurred thumbnails before unlocking content
122
+
60
123
  ## TODO
61
124
 
62
125
  - [ ] More filters (sepia, vignette, noise)
@@ -65,3 +128,4 @@ image(file)
65
128
  - [ ] Presets
66
129
  - [ ] Web Worker support
67
130
  - [ ] Batch processing
131
+ - [ ] `getImageDimensions()` return width/height from Node.js pipeline (`image::image_dimensions()` reads only the header, no full decode)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "beautiful-image",
3
3
  "private": false,
4
- "version": "0.2.5",
4
+ "version": "0.2.6",
5
5
  "type": "module",
6
6
  "author": "Eduar Castaño <hello@eduar.tech> (https://eduar.tech)",
7
7
  "license": "MIT",