fengyuanchen/compressorjs — 5.8k★ trên GitHub (JavaScript). JavaScript image compressor.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
JavaScript image compressor. Uses the browser's native HTMLCanvasElement.toBlob() method to do the compression work, which means it is lossy compression, asynchronous, and has different compression effects in different browsers. Generally use this to precompress an image on the client side before uploading it.
The configuration options. Check out the available options.
Alternatively, you may use Compressor.create(file[, options]).
Example
<input type="file" id="file" accept="image/*">
import Compressor from 'compressorjs';
document.getElementById('file').addEventListener('change', (e) => {
const file = e.target.files[0];
if (!file) {
return;
}
new Compressor(file, {
quality: 0.6,
// The compression process is asynchronous,
// which means you need to access the `result` inside the `success` hook.
success(result) {
const formData = new FormData();
// The third parameter is required by the server.
formData.append('file', result, result.name || 'compressed-image.jpg');
fetch('/path/to/upload', {
method: 'POST',
body: formData,
}).then(() => {
console.log('Upload success');
}).catch((error) => {
console.error('Upload failed', error);
});
},
error(err) {
console.log(err.message);
},
});
});
⬆ back to top
Tips
Use quality between 0.6 and 0.8 for a good balance between file size and visual quality.
Avoid quality: 1 unless you need the image to stay as close as possible to the original; it can increase the file size.
For very large images, set maxWidth and maxHeight to limit the canvas size and avoid browser memory issues.
convertTypes and convertSize are useful for automatically converting large PNG files to JPEG to reduce file size.
Options
You may set compressor options with new Compressor(file, options).
If you want to change the global default options, you may use Compressor.setDefaults(options).
strict
Type: boolean
Default: true
Indicates whether to output the original image instead of the compressed one when the size of the compressed image is greater than the original one's, except the following cases:
The retainExif option is set to true.
The mimeType option is set and its value is different from the mime type of the image.
The width option is set and its value is greater than the natural width of the image.
The height option is set and its value is greater than the natural height of the image.
The minWidth option is set and its value is greater than the natural width of the image.
The minHeight option is set and its value is greater than the natural height of the image.
The maxWidth option is set and its value is less than the natural width of the image.
The maxHeight option is set and its value is less than the natural height of the image.
checkOrientation
Type: boolean
Default: true
Indicates whether to read the image's Exif Orientation value (JPEG image only), and then rotate or flip the image automatically with the value.
Notes:
Don't trust this all the time as some JPEG images have incorrect (not standard) Orientation values.
If the size of the target image is too large (e.g., greater than 10 MB), you should disable this option to avoid an out-of-memory crash.
The image's Exif information will be removed after compressed, so if you need the Exif information, you may need to upload the original image as well.
retainExif
Type: boolean
Default: false
Indicates whether to retain the image's Exif information after compressed.
maxWidth
Type: number
Default: Infinity
The max-width of the output image. The value should be greater than 0.
Avoid getting a blank output image, you might need to set the maxWidth and maxHeight options to limited numbers, because of the size limits of a canvas element, recommend to use 4096 or lesser.
maxHeight
Type: number
Default: Infinity
The max height of the output image. The value should be greater than 0.
minWidth
Type: number
Default: 0
The min-width of the output image. The value should be greater than 0 and should not be greater than the maxWidth.
minHeight
Type: number
Default: 0
The min-height of the output image. The value should be greater than 0 and should not be greater than the maxHeight.
width
Type: number
Default: 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.
height
Type: number
Default: 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.
resize
Type: string
Default: "none"
Options: "none", "contain", and "cover".
Sets how the size of the image should be resized to the container specified by the width and height options.
Note: This option is only available when both the width and height options are specified.
quality
Type: number
Default: 0.8
The quality of the output image. It must be a number between 0 and 1. If this argument is anything else, the default values 0.92 and 0.80 are used for image/jpeg and image/webp respectively. Other arguments are ignored. Be careful to use 1 as it may make the size of the output image become larger.
Note: This option is only available for image/jpeg and image/webp images.
Check out the documentation of the HTMLCanvasElement.toBlob() method for more detail.
Examples:
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%
-
mimeType
Type: string
Default: 'auto'
Options: "auto", "image/png", "image/jpeg", and "image/webp".
The mime type of the output image. By default, the original mime type of the source image file will be used.
Note: Safari does not support mimeType conversion to "image/webp". For more details, see the browser compatibility of the HTMLCanvasElement.toBlob() method.
convertTypes
Type: Array or string (multiple types should be separated by commas)
Default: ["image/png"]
Examples:
["image/png", "image/webp"]
"image/png,image/webp"
Files whose file type is included in this list, and whose file size exceeds the convertSize value will be converted to JPEGs.
For image file type support, see the Image file type and format guide.
convertSize
Type: number
Default: 5000000 (5 MB)
Files whose file type is included in the convertTypes list, and whose file size exceeds this value will be converted to JPEGs. To disable this, just set the value to Infinity.
Examples:
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%
beforeDraw(context, canvas)
Type: Function
Default: null
Parameters:
context: The 2d rendering context of the canvas.
canvas: The canvas for compression.
The hook function to execute before drawing the image onto the canvas for compression.
fengyuanchen/compressorjs có 5.8k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/fengyuanchen/compressorjs. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
fengyuanchen/compressorjs có những chủ đề gì?
GitHub topics của fengyuanchen/compressorjs: "file-upload", "image-compression", "image-compressor", "javascript". TopGit xếp repo vào nhóm Frontend.
fengyuanchen/compressorjs còn đang phát triển không?
Commit gần nhất trên fengyuanchen/compressorjs là 4 tháng trước (theo timestamp GitHub). Repo có 455 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
fengyuanchen/compressorjs là gì?
fengyuanchen/compressorjs (fengyuanchen/compressorjs) là dự án JavaScript trên GitHub. Theo mô tả gốc: JavaScript image compressor.
fengyuanchen/compressorjs so với các dự án Frontend khác thế nào?
fengyuanchen/compressorjs được TopGit xếp vào nhóm Frontend, với 5.8k sao GitHub và viết bằng JavaScript. Xem trang chủ đề Frontend trên TopGit để so sánh với các dự án tương tự theo số sao và mức độ hoạt động.
fengyuanchen/compressorjs viết bằng ngôn ngữ gì?
fengyuanchen/compressorjs chủ yếu viết bằng JavaScript. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Vì sao fengyuanchen/compressorjs được xếp vào nhóm Frontend?
TopGit xếp fengyuanchen/compressorjs vào nhóm Frontend dựa trên GitHub topics và mô tả của repo (gắn thẻ: "file-upload", "image-compression", "image-compressor"). Việc phân loại dựa trên metadata thật của repo, không phải đoán theo cảm tính biên tập.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về compressorjs?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về compressorjs.