Skip to content

Commit f82bcfe

Browse files
committed
feat: alwaysKeepResolution - keep the resolution during compression and reduce the quality only
options.alwaysKeepResolution (default: false) - keep the resolution (width and height) during compression and reduce the quality only (note that options.maxWidthOrHeight is still applied if set) re #127
1 parent 3fcd57d commit f82bcfe

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

lib/image-compression.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
* @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)
2323
* @param {string} [options.fileType] - default to be the original mime type from the image file
2424
* @param {number} [options.initialQuality=1.0]
25+
* @param {boolean} [options.alwaysKeepResolution=false]
2526
* @param {number} previousProgress - for internal try catch rerunning start from previous progress
2627
* @returns {Promise<File | Blob>}
2728
*/
@@ -88,15 +89,17 @@ export default async function compress(file, options, previousProgress = 0) {
8889
const renderedSize = tempFile.size;
8990
let currentSize = renderedSize;
9091
let compressedFile;
91-
let newCanvas; let
92-
ctx;
92+
let newCanvas;
93+
let ctx;
9394
let canvas = orientationFixedCanvas;
95+
const shouldReduceResolution = !options.alwaysKeepResolution && origExceedMaxSize;
9496
while (remainingTrials-- && (currentSize > maxSizeByte || currentSize > sourceSize)) {
95-
const newWidth = origExceedMaxSize ? canvas.width * 0.95 : canvas.width;
96-
const newHeight = origExceedMaxSize ? canvas.height * 0.95 : canvas.height;
97+
const newWidth = shouldReduceResolution ? canvas.width * 0.95 : canvas.width;
98+
const newHeight = shouldReduceResolution ? canvas.height * 0.95 : canvas.height;
9799
if (process.env.BUILD === 'development') {
98100
console.log('current width', newWidth);
99101
console.log('current height', newHeight);
102+
console.log('current quality', quality);
100103
}
101104
[newCanvas, ctx] = getNewCanvasAndCtx(newWidth, newHeight);
102105

lib/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ interface Options {
1919
fileType?: string;
2020
/** @default 1.0 */
2121
initialQuality?: number;
22+
/** @default false */
23+
alwaysKeepResolution?: boolean;
2224
}
2325

2426
declare function imageCompression(image: File, options: Options): Promise<File>;

lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import compressOnWebWorker from './web-worker';
3030
* @param {number} [options.exifOrientation] - default to be the exif orientation from the image file
3131
* @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)
3232
* @param {string} [options.fileType] - default to be the original mime type from the image file
33+
* @param {number} [options.initialQuality=1.0]
34+
* @param {boolean} [options.alwaysKeepResolution=false]
3335
* @returns {Promise<File | Blob>}
3436
*/
3537
async function imageCompression(file, options) {

0 commit comments

Comments
 (0)