Skip to content

Commit 907ca01

Browse files
committed
feat: added progressive jpg and refactored ffmpeg compression.
1 parent 2bb0b01 commit 907ca01

18 files changed

+526
-243
lines changed

README.md

+93-17
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ A simple NodeJS package for image compression powered by FFMPEG.
1818

1919
- **Note:** The **Mac OS** is currently not supported by this package as of the moment.
2020

21-
- To check the FFMPEG latest build or release please refer to this link: [FFMPEG Main Download Site](https://ffmpeg.org/download.html)
21+
- To check the FFMPEG latest build or release please refer to this link: [FFMPEG Main Download Link](https://ffmpeg.org/download.html)
22+
23+
- ImageMagick:
24+
25+
- To be able to use the "ProressiveJPEG" class this require the ImageMagick package. To get the latest version [ImageMagick Download Link](https://imagemagick.org/script/download.php)
2226

2327
## Install
2428

@@ -32,60 +36,132 @@ npm install nodejs-image-squeezer --save
3236

3337
## Usage
3438

35-
- Below are the simple implementation of the package using **TypeScript**:
39+
### FFMPEG
40+
41+
- Below are the simple implementation using **TypeScript**:
42+
43+
```ts
44+
45+
// Import the main class of the NodeJS Image Squeezer.
46+
import ImageSqueezer from 'nodejs-image-squeezer';
47+
48+
// Initialize the main class.
49+
var imgSqueezer = new ImageSqueezer.FFMPEGComppresion();
50+
51+
// Load the necessary requirements and validate
52+
// if the package fit for the current environment.
53+
imgSqueezer.load();
54+
55+
// Override the default binaries ffmpeg.
56+
imgSqueezer.setBin('/path/to/binary');
57+
58+
// Provide the source file path of the desire image
59+
// that will be compress later on.
60+
imgSqueezer.setSourceFilePath('/path/source-filename');
61+
62+
// Provide the output file path of the compressed image.
63+
imgSqueezer.setOutputFilePath('/path/output-filename');
64+
65+
// (Optional) This will allow output file path as empty
66+
// and will use the source file path as the output.
67+
imgSqueezer.allowEmptyOutputFilePath();
68+
69+
// Execute the image compression.
70+
// return a promise.
71+
imgSqueezer.compress();
72+
```
73+
74+
- Basic implementation without using superset libraries for JavaScript (using a pure node.js syntax):
75+
76+
```js
77+
78+
// Require the main class of the NodeJS Image Squeezer.
79+
var ImageSqueezer = require('nodejs-image-squeezer');
80+
81+
// Initialize the main class.
82+
var imgSqueezer = new ImageSqueezer.FFMPEGComppresion();
83+
84+
// Load the necessary requirements and validate
85+
// if the package fit for the current environment.
86+
imgSqueezer.load();
87+
88+
// Override the default binaries ffmpeg.
89+
imgSqueezer.setBin('/path/to/binary');
90+
91+
// Provide the source file path of the desire image
92+
// that will be compress later on.
93+
imgSqueezer.setSourceFilePath('/path/source-filename');
94+
95+
// Provide the output file path of the compressed image.
96+
imgSqueezer.setOutputFilePath('/path/output-filename');
97+
98+
// (Optional) This will allow output file path as empty
99+
// and will use the source file path as the output.
100+
imgSqueezer.allowEmptyOutputFilePath();
101+
102+
// Execute the image compression.
103+
// return a promise.
104+
imgSqueezer.compress();
105+
```
106+
107+
### JPEG Progressive
108+
109+
- Below are the simple implementation using **TypeScript**:
36110

37111
```ts
38112

39113
// Import the main class of the NodeJS Image Squeezer.
40114
import ImageSqueezer from 'nodejs-image-squeezer';
41115

42116
// Initialize the main class.
43-
var imageSqueezer = new ImageSqueezer();
117+
var imgSqueezer = new ImageSqueezer.ProgressiveJPEG();
44118

45119
// Load the necessary requirements and validate
46120
// if the package fit for the current environment.
47-
imageSqueezer.load();
121+
imgSqueezer.load();
48122

49-
// Override the default binaries
50-
imageSqueezer.setFFMpegBin('/path/to/binary');
123+
// Override the default binaries ffmpeg.
124+
imgSqueezer.setBin('/path/to/binary');
51125

52126
// Provide the source file path of the desire image
53127
// that will be compress later on.
54-
imageSqueezer.setSourceFilePath('/path/source-filename');
128+
imgSqueezer.setSourceFilePath('/path/source-filename');
55129

56130
// Provide the output file path of the compressed image.
57-
imageSqueezer.setOutputFilePath('/path/output-filename');
131+
imgSqueezer.setOutputFilePath('/path/output-filename');
58132

59133
// Execute the image compression.
60-
imageSqueezer.compress();
134+
// return a promise.
135+
imgSqueezer.compress();
61136
```
62137

63-
- Basic implementation of the package without superset libraries of JavaScript (using a pure node.js syntax):
138+
- Basic implementation without using superset libraries for JavaScript (using a pure node.js syntax):
64139

65140
```js
66141

67142
// Require the main class of the NodeJS Image Squeezer.
68143
var ImageSqueezer = require('nodejs-image-squeezer');
69144

70145
// Initialize the main class.
71-
var imageSqueezer = new ImageSqueezer();
146+
var imgSqueezer = new ImageSqueezer.ProgressiveJPEG();
72147

73148
// Load the necessary requirements and validate
74149
// if the package fit for the current environment.
75-
imageSqueezer.load();
150+
imgSqueezer.load();
76151

77-
// Override the default binaries
78-
imageSqueezer.setFFMpegBin('/path/to/binary');
152+
// Override the default binaries ffmpeg.
153+
imgSqueezer.setBin('/path/to/binary');
79154

80155
// Provide the source file path of the desire image
81156
// that will be compress later on.
82-
imageSqueezer.setSourceFilePath('/path/source-filename');
157+
imgSqueezer.setSourceFilePath('/path/source-filename');
83158

84159
// Provide the output file path of the compressed image.
85-
imageSqueezer.setOutputFilePath('/path/output-filename');
160+
imgSqueezer.setOutputFilePath('/path/output-filename');
86161

87162
// Execute the image compression.
88-
imageSqueezer.compress();
163+
// return a promise.
164+
imgSqueezer.compress();
89165
```
90166

91167
## License

commitlint.config.js

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
// https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional
22
module.exports = {
3-
rules: {
4-
'body-leading-blank': [1, 'always'],
5-
'footer-leading-blank': [1, 'always'],
6-
'scope-case': [2, 'always', 'lower-case'],
7-
'subject-case': [2, 'never', [
8-
'sentence-case', 'start-case', 'pascal-case', 'upper-case'
9-
]],
10-
'subject-empty': [2, 'never'],
11-
'type-case': [2, 'always', 'lower-case'],
12-
'type-empty': [2, 'never'],
13-
'type-enum': [2, 'always', [
14-
'build',
15-
'chore',
16-
'ci',
17-
'docs',
18-
'feat',
19-
'fix',
20-
'perf',
21-
'refactor',
22-
'revert',
23-
'style',
24-
'test'
25-
]]
26-
}
3+
rules: {
4+
'body-leading-blank': [1, 'always'],
5+
'footer-leading-blank': [1, 'always'],
6+
'scope-case': [2, 'always', 'lower-case'],
7+
'subject-case': [2, 'never', [
8+
'sentence-case', 'start-case', 'pascal-case', 'upper-case'
9+
]],
10+
'subject-empty': [2, 'never'],
11+
'type-case': [2, 'always', 'lower-case'],
12+
'type-empty': [2, 'never'],
13+
'type-enum': [2, 'always', [
14+
'build',
15+
'chore',
16+
'ci',
17+
'docs',
18+
'feat',
19+
'fix',
20+
'perf',
21+
'refactor',
22+
'revert',
23+
'style',
24+
'test'
25+
]]
26+
}
2727
};

jest.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ module.exports = {
77
},
88
"cacheDirectory": "jest-cache/",
99
"collectCoverage": true,
10+
"collectCoverageFrom": [
11+
"src/**/*.ts",
12+
"!src/ImageSqueezer.ts"
13+
],
1014
"coverageDirectory": "jest-coverage"
1115
}

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
"types": "dist/ImageSqueezer.d.ts",
77
"scripts": {
88
"test": "jest",
9-
"pre-build-lint": "lint-staged",
10-
"build-tsc": "./node_modules/.bin/tsc",
11-
"build-release": "standard-version"
9+
"build-release": "standard-version",
10+
"build-tsc": "./node_modules/.bin/tsc"
1211
},
1312
"keywords": [
1413
"image",
@@ -56,7 +55,7 @@
5655
},
5756
"husky": {
5857
"hooks": {
59-
"pre-commit": "lint-staged",
58+
"pre-commit": "lint-staged && npm run build-tsc",
6059
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
6160
}
6261
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export class ImageSqueezerCommonException extends Error {
2+
3+
constructor(message: string) {
4+
5+
super(message);
6+
7+
this.name = 'ImageSqueezerCommonException';
8+
}
9+
10+
public static emptySourceFilePath(): ImageSqueezerCommonException {
11+
12+
return new ImageSqueezerCommonException('The source file path is empty.');
13+
}
14+
15+
public static emptyOutputFilePath(): ImageSqueezerCommonException {
16+
17+
return new ImageSqueezerCommonException('The output file path is empty.');
18+
}
19+
}

src/Exception/ImageSqueezerException.ts

-19
This file was deleted.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export class ProgressiveJPEGException extends Error {
2+
3+
constructor(message: string) {
4+
5+
super(message);
6+
7+
this.name = 'ProgressiveJPEGException';
8+
}
9+
10+
public static requiredDependenciesNotInstalled(): ProgressiveJPEGException {
11+
12+
return new ProgressiveJPEGException('The required dependencies not installed in the current system. (ie. Image Magick)');
13+
}
14+
}

0 commit comments

Comments
 (0)