Skip to content

Commit 1c34372

Browse files
committed
add option to change initial quality
1 parent 8f149b5 commit 1c34372

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ const options = {
5151
maxIteration: number, // optional, max number of iteration to compress the image (default: 10)
5252
exifOrientation: number, // optional, see https://stackoverflow.com/a/32490603/10395024
5353
onProgress: Function, // optional, a function takes one progress argument (percentage from 0 to 100)
54-
fileType: string // optional, fileType override
54+
fileType: string, // optional, fileType override
55+
initialQuality: number // optional, initial quality value during compression
5556
}
5657

5758
imageCompression(file: File, options): Promise<File | Blob>

lib/image-compression.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import {
1313
* Compress an image file.
1414
*
1515
* @param {File} file
16-
* @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }
16+
* @param {Object} options
1717
* @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]
1818
* @param {number} [options.maxWidthOrHeight=undefined]
19+
* @param {boolean} [options.useWebWorker=false]
1920
* @param {number} [options.maxIteration=10]
2021
* @param {number} [options.exifOrientation] - default to be the exif orientation from the image file
2122
* @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)
2223
* @param {string} [options.fileType] - default to be the original mime type from the image file
24+
* @param {number} [options.initialQuality=1.0]
2325
* @returns {Promise<File | Blob>}
2426
*/
2527
export default async function compress (file, options) {
@@ -57,7 +59,7 @@ export default async function compress (file, options) {
5759
const orientationFixedCanvas = (await isAutoOrientationInBrowser) ? maxWidthOrHeightFixedCanvas : followExifOrientation(maxWidthOrHeightFixedCanvas, options.exifOrientation)
5860
incProgress()
5961

60-
let quality = 1
62+
let quality = options.initialQuality || 1.0
6163

6264
let tempFile = await canvasToFile(orientationFixedCanvas, options.fileType || file.type, file.name, file.lastModified, quality)
6365
incProgress()

lib/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ interface Options {
1818
onProgress?: (progress: number) => void;
1919
/** Default to be the original mime type from the image file */
2020
fileType?: string;
21+
/** @default 1.0 */
22+
initialQuality?: number;
2123
}
2224

2325
declare function imageCompression(image: File | Blob, options: Options): Promise<File | Blob>;

0 commit comments

Comments
 (0)