Security News
cURL Project and Go Security Teams Reject CVSS as Broken
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
@xkeshi/image-compressor
Advanced tools
A simple JavaScript image compressor. Uses the Browser's native canvas.toBlob API to do the compression work. General use this to precompress a client image file before upload it.
dist/
├── image-compressor.js (UMD)
├── image-compressor.min.js (UMD, compressed)
├── image-compressor.common.js (CommonJS, default)
└── image-compressor.esm.js (ES Module)
npm install @xkeshi/image-compressor
new ImageCompressor([file[, options]])
file
The target image file for compressing.
options
Object
The options for compressing. Check out the available options.
<input type="file" id="file" accept="image/*">
import axios from 'axios';
import ImageCompressor from '@xkeshi/image-compressor';
document.getElementById('file').addEventListener('change', (e) => {
const file = e.target.files[0];
if (!file) {
return;
}
new ImageCompressor(file, {
quality: .6,
success(result) {
const formData = new FormData();
formData.append('file', result, result.name);
// Send the compressed image file to server with XMLHttpRequest.
axios.post('/path/to/upload', formData).then(() => {
console.log('Upload success!');
});
},
error(e) {
console.log(e.message);
},
});
})
boolean
true
Indicates if read the image's Exif Orientation value (JPEG image only), and then rotate or flip the image automatically with the value.
Note: Don't trust this all the time as some JPEG images have incorrect (not standard) Orientation values.
number
Infinity
The max width of the output image. The value should be greater then 0
.
Avoid to get a blank output image, you might need to set the
maxWidth
andmaxHeight
options to limited numbers, because of the size limits of a canvas element.
number
Infinity
The max height of the output image. The value should be greater then 0
.
number
0
The min width of the output image. The value should be greater then 0
and should not be greater than the maxWidth
.
number
0
The min height of the output image. The value should be greater then 0
and should not be greater than the maxHeight
.
number
undefined
The width of the output image. If not specified, the natural width of the original image will be used, or if the height
option is set, the width will be computed automatically by the natural aspect ratio.
number
undefined
The height of the output image. If not specified, the natural height of the original image will be used, or if the width
option is set, the height will be computed automatically by the natural aspect ratio.
Note: In order to keep the same aspect ratio to the original image, if the width
option is set, will use it compute the height automatically, which means the height
option will be ignored.
number
0.8
The quality of the output image.
It must be a number between 0
and 1
. Be careful to use 1
as it may make the size of the output image become larger.
Check out canvas.toBlob for more detail.
Note: This option only available for image/jpeg
and image/webp
images.
Examples (in Chrome 61):
Quality | Input size | Output size | Compression ratio | Description |
---|---|---|---|---|
0 | 2.12 MB | 114.61 KB | 94.72% | - |
0.2 | 2.12 MB | 349.57 KB | 83.90% | - |
0.4 | 2.12 MB | 517.10 KB | 76.18% | - |
0.6 | 2.12 MB | 694.99 KB | 67.99% | Recommend |
0.8 | 2.12 MB | 1.14 MB | 46.41% | Recommend |
1 | 2.12 MB | 2.12 MB | 0% | Not recommend |
NaN | 2.12 MB | 2.01 MB | 5.02% | - |
string
'auto'
The mime type of the output image. By default, the original mime type of the source image file will be used.
number
5000000
(5MB)PNG files over this value will be converted to JPEGs. To disable this, just set the value to Infinity
. See #2.
Examples (in Chrome 61):
convertSize | Input size (type) | Output size (type) | Compression ratio |
---|---|---|---|
5 MB | 1.87 MB (PNG) | 1.87 MB (PNG) | 0% |
5 MB | 5.66 MB (PNG) | 450.24 KB (JPEG) | 92.23% |
5 MB | 9.74 MB (PNG) | 883.89 KB (JPEG) | 91.14% |
Function
null
result
: The compressed image (a Blob
object).The success callback for the image compressing process.
Function
null
err
: The compression error (an Error
object).The error callback for the image compressing process.
file
:
File
or Blob
options
(optional):
Object
Promise
Compress an image file.
const imageCompressor = new ImageCompressor();
imageCompressor.compress(file, options)
.then((result) => {
// Handle the compressed image file.
})
.catch((err) => {
// Handle the error
})
babel-polyfill
for Promise
support)Maintained under the Semantic Versioning guidelines.
FAQs
A simple JavaScript image compressor.
The npm package @xkeshi/image-compressor receives a total of 862 weekly downloads. As such, @xkeshi/image-compressor popularity was classified as not popular.
We found that @xkeshi/image-compressor demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.