optimo 0.0.12 → 0.0.14
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.
- package/README.md +8 -1
- package/bin/help.js +3 -1
- package/package.json +1 -1
- package/src/util.js +18 -1
package/README.md
CHANGED
|
@@ -18,6 +18,8 @@ npx -y optimo public/media/banner.png # for a file
|
|
|
18
18
|
npx -y optimo public/media/banner.png -f jpeg # convert + optimize
|
|
19
19
|
npx -y optimo public/media/banner.png -r 50% # resize + optimize
|
|
20
20
|
npx -y optimo public/media/banner.png -r 100kB # resize to max file size
|
|
21
|
+
npx -y optimo public/media/banner.png -r w960 # resize to max width
|
|
22
|
+
npx -y optimo public/media/banner.png -r h480 # resize to max height
|
|
21
23
|
```
|
|
22
24
|
|
|
23
25
|
## Highlights
|
|
@@ -26,7 +28,7 @@ npx -y optimo public/media/banner.png -r 100kB # resize to max file size
|
|
|
26
28
|
- Compression-first per format.
|
|
27
29
|
- Format-specific tuning for stronger size reduction.
|
|
28
30
|
- Safety guard: if optimized output is not smaller, original file is kept.
|
|
29
|
-
- Resizing supports percentage values (`50%`)
|
|
31
|
+
- Resizing supports percentage values (`50%`), max file size targets (`100kB`), width (`w960`), & height (`h480`).
|
|
30
32
|
|
|
31
33
|
## Programmatic API
|
|
32
34
|
|
|
@@ -46,6 +48,11 @@ await optimo.file('/absolute/path/image.jpg', {
|
|
|
46
48
|
onLogs: console.log
|
|
47
49
|
})
|
|
48
50
|
|
|
51
|
+
await optimo.file('/absolute/path/image.jpg', {
|
|
52
|
+
resize: 'w960',
|
|
53
|
+
onLogs: console.log
|
|
54
|
+
})
|
|
55
|
+
|
|
49
56
|
// optimize a dir recursively
|
|
50
57
|
const result = await optimo.dir('/absolute/path/images')
|
|
51
58
|
|
package/bin/help.js
CHANGED
|
@@ -10,7 +10,7 @@ Usage
|
|
|
10
10
|
Options
|
|
11
11
|
-d, --dry-run Show what would be optimized without making changes
|
|
12
12
|
-f, --format Convert output format (e.g. jpeg, webp, avif)
|
|
13
|
-
-r, --resize Resize by percentage (50%)
|
|
13
|
+
-r, --resize Resize by percentage (50%), size (100kB), width (w960), or height (h480)
|
|
14
14
|
|
|
15
15
|
Examples
|
|
16
16
|
$ optimo image.jpg
|
|
@@ -19,4 +19,6 @@ Examples
|
|
|
19
19
|
$ optimo image.png -f jpeg
|
|
20
20
|
$ optimo image.png -r 50%
|
|
21
21
|
$ optimo image.png -r 100kB
|
|
22
|
+
$ optimo image.png -r w960
|
|
23
|
+
$ optimo image.png -r h480
|
|
22
24
|
`)
|
package/package.json
CHANGED
package/src/util.js
CHANGED
|
@@ -35,6 +35,23 @@ const parseResize = resize => {
|
|
|
35
35
|
const raw = String(resize).trim()
|
|
36
36
|
const normalized = raw.toLowerCase().replace(/\s+/g, '')
|
|
37
37
|
|
|
38
|
+
const dimensionMatch = normalized.match(/^([wh])(\d+)$/)
|
|
39
|
+
if (dimensionMatch) {
|
|
40
|
+
const axis = dimensionMatch[1]
|
|
41
|
+
const value = Number(dimensionMatch[2])
|
|
42
|
+
|
|
43
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
44
|
+
throw new TypeError(
|
|
45
|
+
'Resize width/height must be greater than 0 (e.g. w960, h480)'
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
mode: 'dimension',
|
|
51
|
+
value: axis === 'w' ? `${value}x` : `x${value}`
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
38
55
|
const maxSizeMatch = normalized.match(/^(\d*\.?\d+)(b|kb|mb|gb)$/)
|
|
39
56
|
if (maxSizeMatch) {
|
|
40
57
|
const units = { b: 1, kb: 1024, mb: 1024 ** 2, gb: 1024 ** 3 }
|
|
@@ -56,7 +73,7 @@ const parseResize = resize => {
|
|
|
56
73
|
|
|
57
74
|
if (!Number.isFinite(value) || value <= 0) {
|
|
58
75
|
throw new TypeError(
|
|
59
|
-
'Resize must be a percentage (
|
|
76
|
+
'Resize must be a percentage (50%), max size (100kB), width (w960), or height (h480)'
|
|
60
77
|
)
|
|
61
78
|
}
|
|
62
79
|
|