在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:fengyuanchen/compressorjs开源软件地址:https://github.com/fengyuanchen/compressorjs开源编程语言:JavaScript 99.8%开源软件介绍:Compressor.js
Table of contentsMain
Getting startedInstallnpm install compressorjs UsageSyntaxnew Compressor(file[, options]) file The target image file for compressing. options
The options for compressing. Check out the available options. Example<input type="file" id="file" accept="image/*"> import axios from 'axios';
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 have to access the `result` in the `success` hook function.
success(result) {
const formData = new FormData();
// The third parameter is required for server
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(err) {
console.log(err.message);
},
});
}); OptionsYou may set compressor options with strict
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:
checkOrientation
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:
maxWidth
The max-width of the output image. The value should be greater than
maxHeight
The max height of the output image. The value should be greater than minWidth
The min-width of the output image. The value should be greater than minHeight
The min-height of the output image. The value should be greater than width
The width of the output image. If not specified, the natural width of the original image will be used, or if the height
The height of the output image. If not specified, the natural height of the original image will be used, or if the resize
Sets how the size of the image should be resized to the container specified by the Note: This option only available when both the quality
The quality of the output image. It must be a number between Note: This option only available for
Examples:
mimeType
The mime type of the output image. By default, the original mime type of the source image file will be used. convertTypes
Files whose file type is included in this list, and whose file size exceeds the convertSize
Files whose file type is included in the Examples:
beforeDraw(context, canvas)
The hook function to execute before drawing the image into the canvas for compression. new Compressor(file, {
beforeDraw(context, canvas) {
context.fillStyle = '#fff';
context.fillRect(0, 0, canvas.width, canvas.height);
context.filter = 'grayscale(100%)';
},
}); drew(context, canvas)
The hook function to execute after drawing the image into the canvas for compression. new Compressor(file, {
drew(context, canvas) {
context.fillStyle = '#fff';
context.font = '2rem serif';
context.fillText('watermark', 20, canvas.height - 20);
},
}); success(result)
The hook function to execute when successful to compress the image. error(err)
The hook function executes when fails to compress the image. Methodsabort()Abort the compression process. const compressor = new Compressor(file);
// Do something...
compressor.abort(); No conflictIf you have to use another compressor with the same namespace, just call the <script src="other-compressor.js"></script>
<script src="compressor.js"></script>
<script>
Compressor.noConflict();
// Code that uses other `Compressor` can follow here.
</script> Browser support
ContributingPlease read through our contributing guidelines. VersioningMaintained under the Semantic Versioning guidelines. License |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论