picslim 0.0.4 → 0.0.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.
- package/README.md +7 -5
- package/package.json +1 -1
- package/picslim.js +35 -14
package/README.md
CHANGED
|
@@ -20,8 +20,10 @@ picslim [options]
|
|
|
20
20
|
|
|
21
21
|
Options:
|
|
22
22
|
|
|
23
|
-
**-q, --quality [value]:** Set the image quality (1 to 100, default: 80)
|
|
24
|
-
**-
|
|
23
|
+
**-q, --quality [value]:** Set the image quality (1 to 100, default: 80).<br>
|
|
24
|
+
**-mw, --maxWidth [value]:** Set the maximum width allowed (default: null).<br>
|
|
25
|
+
**-mh, --maxHeight [value]:** Set the maximum height allowed (default: null).<br>
|
|
26
|
+
**-c, --compressionLevel [value]:** Set the compression level (0 to 9, default: 9).
|
|
25
27
|
|
|
26
28
|
Example:
|
|
27
29
|
|
|
@@ -32,6 +34,7 @@ picslim -q 90 -w 1920
|
|
|
32
34
|
This will optimize all JPEG and PNG images in the current directory, and the optimized images will be saved in a 'min' directory.
|
|
33
35
|
|
|
34
36
|
### Example
|
|
37
|
+
|
|
35
38
|
Suppose you have a directory with the following images:
|
|
36
39
|
|
|
37
40
|
- image1.jpg
|
|
@@ -55,11 +58,10 @@ After running the command, you will have the following directory structure:
|
|
|
55
58
|
- image4.png
|
|
56
59
|
```
|
|
57
60
|
|
|
58
|
-
###
|
|
59
|
-
This project is licensed under the MIT License. See the LICENSE file for details.
|
|
61
|
+
### License
|
|
60
62
|
|
|
63
|
+
This project is licensed under the MIT License. See the LICENSE file for details.
|
|
61
64
|
|
|
62
65
|
### Author
|
|
63
66
|
|
|
64
67
|
Ivan Mercedes
|
|
65
|
-
|
package/package.json
CHANGED
package/picslim.js
CHANGED
|
@@ -9,6 +9,7 @@ const yargs = require("yargs");
|
|
|
9
9
|
* @typedef {Object} Argv
|
|
10
10
|
* @property {number} quality - Image quality (1 to 100)
|
|
11
11
|
* @property {number} width - Maximum width allowed
|
|
12
|
+
* @property {number} compressionLevel - PNG compression level (0 to 9)
|
|
12
13
|
*/
|
|
13
14
|
|
|
14
15
|
const argv = yargs.options({
|
|
@@ -19,20 +20,35 @@ const argv = yargs.options({
|
|
|
19
20
|
type: "number",
|
|
20
21
|
default: 80,
|
|
21
22
|
},
|
|
22
|
-
|
|
23
|
-
alias: "
|
|
23
|
+
mw: {
|
|
24
|
+
alias: "maxWidth",
|
|
24
25
|
describe: "Maximum width allowed",
|
|
25
26
|
demandOption: false,
|
|
26
27
|
type: "number",
|
|
27
|
-
default:
|
|
28
|
+
default: null,
|
|
29
|
+
},
|
|
30
|
+
mh: {
|
|
31
|
+
alias: "maxHeight",
|
|
32
|
+
describe: "Maximum height allowed",
|
|
33
|
+
demandOption: false,
|
|
34
|
+
type: "number",
|
|
35
|
+
default: null,
|
|
36
|
+
},
|
|
37
|
+
c: {
|
|
38
|
+
alias: "compressionLevel",
|
|
39
|
+
describe: "PNG compression level (0 to 9)",
|
|
40
|
+
demandOption: false,
|
|
41
|
+
type: "number",
|
|
42
|
+
default: 9,
|
|
28
43
|
},
|
|
29
|
-
|
|
30
44
|
}).argv;
|
|
31
45
|
|
|
32
46
|
const inputDir = "./";
|
|
33
47
|
const outputDir = "./min";
|
|
34
48
|
const quality = argv.quality;
|
|
35
|
-
const maxWidth = argv.
|
|
49
|
+
const maxWidth = argv.maxWidth;
|
|
50
|
+
const maxHeight = argv.maxHeight;
|
|
51
|
+
const compressionLevel = argv.compressionLevel;
|
|
36
52
|
|
|
37
53
|
/**
|
|
38
54
|
* Verifies if the output directory exists; if not, creates it.
|
|
@@ -58,8 +74,12 @@ fs.readdir(inputDir, (err, files) => {
|
|
|
58
74
|
.metadata()
|
|
59
75
|
.then((metadata) => {
|
|
60
76
|
const originalWidth = metadata.width;
|
|
77
|
+
const originalHeight = metadata.height;
|
|
61
78
|
sharp(inputPath)
|
|
62
|
-
.resize(
|
|
79
|
+
.resize(
|
|
80
|
+
originalWidth > maxWidth ? maxWidth : null,
|
|
81
|
+
originalHeight > maxHeight ? maxHeight : null,
|
|
82
|
+
)
|
|
63
83
|
.jpeg({
|
|
64
84
|
quality,
|
|
65
85
|
})
|
|
@@ -72,19 +92,20 @@ fs.readdir(inputDir, (err, files) => {
|
|
|
72
92
|
});
|
|
73
93
|
});
|
|
74
94
|
} else if (file.match(/\.(png)$/i)) {
|
|
75
|
-
const compressionLevel = (quality / 100) * 10;
|
|
76
|
-
const limitedCompressionLevel = Math.min(
|
|
77
|
-
Math.max(compressionLevel, 0),
|
|
78
|
-
9,
|
|
79
|
-
);
|
|
80
95
|
sharp(inputPath)
|
|
81
96
|
.metadata()
|
|
82
97
|
.then((metadata) => {
|
|
83
98
|
const originalWidth = metadata.width;
|
|
84
|
-
|
|
99
|
+
const originalHeight = metadata.height;
|
|
85
100
|
sharp(inputPath)
|
|
86
|
-
.resize(
|
|
87
|
-
|
|
101
|
+
.resize(
|
|
102
|
+
originalWidth > maxWidth ? maxWidth : null,
|
|
103
|
+
originalHeight > maxHeight ? maxHeight : null,
|
|
104
|
+
)
|
|
105
|
+
.png({
|
|
106
|
+
quality,
|
|
107
|
+
compressionLevel,
|
|
108
|
+
})
|
|
88
109
|
.toFile(outputPath, (err, info) => {
|
|
89
110
|
if (err) {
|
|
90
111
|
console.error(`Optimization error ${file}: `, err);
|