picslim 0.0.4

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 (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +65 -0
  3. package/package.json +36 -0
  4. package/picslim.js +98 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License Copyright (c) 2023 Ivan Mercedes
2
+
3
+ Permission is hereby granted, free
4
+ of charge, to any person obtaining a copy of this software and associated
5
+ documentation files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use, copy, modify, merge,
7
+ publish, distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to the
9
+ following conditions:
10
+
11
+ The above copyright notice and this permission notice
12
+ (including the next paragraph) shall be included in all copies or substantial
13
+ portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16
+ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
18
+ EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # PicSlim
2
+
3
+ **Picslim** is a Node.js package that allows you to optimize images in a specified directory. It supports JPEG and PNG images, and you can control the quality and resizing of the images during optimization.
4
+
5
+ ## Installation
6
+
7
+ You can install picslim globally using npm:
8
+
9
+ ```bash
10
+ npm install -g picslim
11
+ ```
12
+
13
+ # Usage
14
+
15
+ Once installed, you can use the optimizimage command in your terminal. Here's how you can use it:
16
+
17
+ ```
18
+ picslim [options]
19
+ ```
20
+
21
+ Options:
22
+
23
+ **-q, --quality [value]:** Set the image quality (1 to 100, default: 80).
24
+ **-w, --width [value]:** Set the maximum width allowed (default: 1366).
25
+
26
+ Example:
27
+
28
+ ```bash
29
+ picslim -q 90 -w 1920
30
+ ```
31
+
32
+ This will optimize all JPEG and PNG images in the current directory, and the optimized images will be saved in a 'min' directory.
33
+
34
+ ### Example
35
+ Suppose you have a directory with the following images:
36
+
37
+ - image1.jpg
38
+ - image2.jpg
39
+ - image3.png
40
+ - image4.png
41
+
42
+ You can optimize all these images with the following command:
43
+
44
+ ```bash
45
+ picslim -q 90 -w 1920
46
+ ```
47
+
48
+ After running the command, you will have the following directory structure:
49
+
50
+ ```markdown
51
+ - min
52
+ - image1.jpg
53
+ - image2.jpg
54
+ - image3.png
55
+ - image4.png
56
+ ```
57
+
58
+ ### License
59
+ This project is licensed under the MIT License. See the LICENSE file for details.
60
+
61
+
62
+ ### Author
63
+
64
+ Ivan Mercedes
65
+
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "picslim",
3
+ "version": "0.0.4",
4
+ "description": "Picslim is a Node.js package that allows you to optimize images in a specified directory.",
5
+ "main": "picslim.js",
6
+ "bin": {
7
+ "picslim": "picslim.js"
8
+ },
9
+ "source": "picslim.js",
10
+ "scripts": {
11
+ "start": "node picslim.js -q 80"
12
+ },
13
+ "keywords": [
14
+ "optimization",
15
+ "images",
16
+ "package",
17
+ "Node.js",
18
+ "cli"
19
+ ],
20
+ "author": "Ivan Mercedes <ivanmercede@gmail.com> (https://ivanmercedes.com/)",
21
+ "homepage": "https://github.com/ivanmercedes/picslim#readme",
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/ivanmercedes/picslim.git"
25
+ },
26
+ "license": "MIT",
27
+ "files": [
28
+ "picslim.js",
29
+ "package.json",
30
+ "README.md"
31
+ ],
32
+ "dependencies": {
33
+ "sharp": "^0.32.6",
34
+ "yargs": "^17.7.2"
35
+ }
36
+ }
package/picslim.js ADDED
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env node
2
+ const fs = require("fs");
3
+ const sharp = require("sharp");
4
+ const yargs = require("yargs");
5
+
6
+ /**
7
+ * Parses command line arguments using yargs library.
8
+ *
9
+ * @typedef {Object} Argv
10
+ * @property {number} quality - Image quality (1 to 100)
11
+ * @property {number} width - Maximum width allowed
12
+ */
13
+
14
+ const argv = yargs.options({
15
+ q: {
16
+ alias: "quality",
17
+ describe: "Image quality (1 to 100)",
18
+ demandOption: false,
19
+ type: "number",
20
+ default: 80,
21
+ },
22
+ w: {
23
+ alias: "width",
24
+ describe: "Maximum width allowed",
25
+ demandOption: false,
26
+ type: "number",
27
+ default: 1366,
28
+ },
29
+
30
+ }).argv;
31
+
32
+ const inputDir = "./";
33
+ const outputDir = "./min";
34
+ const quality = argv.quality;
35
+ const maxWidth = argv.width;
36
+
37
+ /**
38
+ * Verifies if the output directory exists; if not, creates it.
39
+ *
40
+ * @param {string} dir - The directory to verify.
41
+ */
42
+ if (!fs.existsSync(outputDir)) {
43
+ fs.mkdirSync(outputDir);
44
+ }
45
+
46
+ fs.readdir(inputDir, (err, files) => {
47
+ if (err) {
48
+ console.error("Error reading input directory: ", err);
49
+ return;
50
+ }
51
+
52
+ files.forEach((file) => {
53
+ const inputPath = `${inputDir}/${file}`;
54
+ const outputPath = `${outputDir}/${file}`;
55
+
56
+ if (file.match(/\.(jpg|jpeg)$/i)) {
57
+ sharp(inputPath)
58
+ .metadata()
59
+ .then((metadata) => {
60
+ const originalWidth = metadata.width;
61
+ sharp(inputPath)
62
+ .resize(originalWidth > maxWidth ? maxWidth : null)
63
+ .jpeg({
64
+ quality,
65
+ })
66
+ .toFile(outputPath, (err, info) => {
67
+ if (err) {
68
+ console.error(`Optimization error ${file}: `, err);
69
+ } else {
70
+ console.log(`Optimized PNG image: ${file}`);
71
+ }
72
+ });
73
+ });
74
+ } 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
+ sharp(inputPath)
81
+ .metadata()
82
+ .then((metadata) => {
83
+ const originalWidth = metadata.width;
84
+
85
+ sharp(inputPath)
86
+ .resize(originalWidth > maxWidth ? maxWidth : null)
87
+ .png({ compressionLevel: limitedCompressionLevel })
88
+ .toFile(outputPath, (err, info) => {
89
+ if (err) {
90
+ console.error(`Optimization error ${file}: `, err);
91
+ } else {
92
+ console.log(`Optimized PNG image: ${file}`);
93
+ }
94
+ });
95
+ });
96
+ }
97
+ });
98
+ });