Skip to content

Commit fc8e929

Browse files
committed
Release 1.0.13
1 parent 787321a commit fc8e929

19 files changed

+558
-366
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## v1.0.13 (11 Aug 2020)
2+
* added: new option for setting initial quality level [#64](https://github.com/Donaldcwl/browser-image-compression/issues/64), [#78](https://github.com/Donaldcwl/browser-image-compression/issues/78)
3+
* fixed: options object being altered by the compress func [#71](https://github.com/Donaldcwl/browser-image-compression/pull/71)
4+
* fixed: issue with output size of png compression [#57](https://github.com/Donaldcwl/browser-image-compression/issues/57)
5+
16
## v1.0.12 (4 June 2020)
27
* fixed: issue with SSR [#58](https://github.com/Donaldcwl/browser-image-compression/issues/58)
38

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ or
4747
const options = {
4848
maxSizeMB: number, // (default: Number.POSITIVE_INFINITY)
4949
maxWidthOrHeight: number, // compressedFile will scale down by ratio to a point that width or height is smaller than maxWidthOrHeight (default: undefined)
50+
onProgress: Function, // optional, a function takes one progress argument (percentage from 0 to 100)
5051
useWebWorker: boolean, // optional, use multi-thread web worker, fallback to run in main-thread (default: true)
52+
53+
// following options are for advanced user
5154
maxIteration: number, // optional, max number of iteration to compress the image (default: 10)
5255
exifOrientation: number, // optional, see https://stackoverflow.com/a/32490603/10395024
53-
onProgress: Function, // optional, a function takes one progress argument (percentage from 0 to 100)
5456
fileType: string, // optional, fileType override
55-
initialQuality: number // optional, initial quality value during compression
57+
initialQuality: number // optional, initial quality value between 0 and 1 (default: 1)
5658
}
5759

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

coverage/badge.svg

Lines changed: 1 addition & 1 deletion
Loading

coverage/coverage-final.json

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

coverage/coverage-summary.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{"total": {"lines":{"total":206,"covered":167,"skipped":0,"pct":81.07},"statements":{"total":220,"covered":169,"skipped":0,"pct":76.82},"functions":{"total":28,"covered":24,"skipped":0,"pct":85.71},"branches":{"total":105,"covered":60,"skipped":0,"pct":57.14}}
2-
,"D:\\Projects\\browser-image-compression\\lib\\image-compression.js": {"lines":{"total":47,"covered":47,"skipped":0,"pct":100},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":47,"covered":47,"skipped":0,"pct":100},"branches":{"total":24,"covered":20,"skipped":0,"pct":83.33}}
1+
{"total": {"lines":{"total":219,"covered":173,"skipped":0,"pct":79},"statements":{"total":226,"covered":175,"skipped":0,"pct":77.43},"functions":{"total":28,"covered":24,"skipped":0,"pct":85.71},"branches":{"total":107,"covered":61,"skipped":0,"pct":57.01}}
2+
,"D:\\Projects\\browser-image-compression\\lib\\image-compression.js": {"lines":{"total":53,"covered":53,"skipped":0,"pct":100},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":53,"covered":53,"skipped":0,"pct":100},"branches":{"total":26,"covered":21,"skipped":0,"pct":80.77}}
33
,"D:\\Projects\\browser-image-compression\\lib\\index.js": {"lines":{"total":30,"covered":27,"skipped":0,"pct":90},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":30,"covered":27,"skipped":0,"pct":90},"branches":{"total":19,"covered":13,"skipped":0,"pct":68.42}}
4-
,"D:\\Projects\\browser-image-compression\\lib\\utils.js": {"lines":{"total":129,"covered":93,"skipped":0,"pct":72.09},"functions":{"total":23,"covered":19,"skipped":0,"pct":82.61},"statements":{"total":143,"covered":95,"skipped":0,"pct":66.43},"branches":{"total":62,"covered":27,"skipped":0,"pct":43.55}}
4+
,"D:\\Projects\\browser-image-compression\\lib\\utils.js": {"lines":{"total":136,"covered":93,"skipped":0,"pct":68.38},"functions":{"total":23,"covered":19,"skipped":0,"pct":82.61},"statements":{"total":143,"covered":95,"skipped":0,"pct":66.43},"branches":{"total":62,"covered":27,"skipped":0,"pct":43.55}}
55
}

coverage/image-compression.js.html

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ <h1><a href="index.html">All files</a> image-compression.js</h1>
2525
<div class='fl pad1y space-right2'>
2626
<span class="strong">100% </span>
2727
<span class="quiet">Statements</span>
28-
<span class='fraction'>47/47</span>
28+
<span class='fraction'>53/53</span>
2929
</div>
3030

3131

3232
<div class='fl pad1y space-right2'>
33-
<span class="strong">83.33% </span>
33+
<span class="strong">80.77% </span>
3434
<span class="quiet">Branches</span>
35-
<span class='fraction'>20/24</span>
35+
<span class='fraction'>21/26</span>
3636
</div>
3737

3838

@@ -46,7 +46,7 @@ <h1><a href="index.html">All files</a> image-compression.js</h1>
4646
<div class='fl pad1y space-right2'>
4747
<span class="strong">100% </span>
4848
<span class="quiet">Lines</span>
49-
<span class='fraction'>47/47</span>
49+
<span class='fraction'>53/53</span>
5050
</div>
5151

5252

@@ -170,7 +170,19 @@ <h1><a href="index.html">All files</a> image-compression.js</h1>
170170
<a name='L111'></a><a href='#L111'>111</a>
171171
<a name='L112'></a><a href='#L112'>112</a>
172172
<a name='L113'></a><a href='#L113'>113</a>
173-
<a name='L114'></a><a href='#L114'>114</a></td><td class="line-coverage quiet"><span class="cline-any cline-neutral">&nbsp;</span>
173+
<a name='L114'></a><a href='#L114'>114</a>
174+
<a name='L115'></a><a href='#L115'>115</a>
175+
<a name='L116'></a><a href='#L116'>116</a>
176+
<a name='L117'></a><a href='#L117'>117</a>
177+
<a name='L118'></a><a href='#L118'>118</a>
178+
<a name='L119'></a><a href='#L119'>119</a>
179+
<a name='L120'></a><a href='#L120'>120</a>
180+
<a name='L121'></a><a href='#L121'>121</a>
181+
<a name='L122'></a><a href='#L122'>122</a>
182+
<a name='L123'></a><a href='#L123'>123</a>
183+
<a name='L124'></a><a href='#L124'>124</a></td><td class="line-coverage quiet"><span class="cline-any cline-neutral">&nbsp;</span>
184+
<span class="cline-any cline-neutral">&nbsp;</span>
185+
<span class="cline-any cline-neutral">&nbsp;</span>
174186
<span class="cline-any cline-neutral">&nbsp;</span>
175187
<span class="cline-any cline-neutral">&nbsp;</span>
176188
<span class="cline-any cline-neutral">&nbsp;</span>
@@ -203,8 +215,8 @@ <h1><a href="index.html">All files</a> image-compression.js</h1>
203215
<span class="cline-any cline-neutral">&nbsp;</span>
204216
<span class="cline-any cline-neutral">&nbsp;</span>
205217
<span class="cline-any cline-neutral">&nbsp;</span>
206-
<span class="cline-any cline-yes">15x</span>
207-
<span class="cline-any cline-yes">15x</span>
218+
<span class="cline-any cline-yes">9x</span>
219+
<span class="cline-any cline-yes">9x</span>
208220
<span class="cline-any cline-neutral">&nbsp;</span>
209221
<span class="cline-any cline-neutral">&nbsp;</span>
210222
<span class="cline-any cline-yes">6x</span>
@@ -237,6 +249,9 @@ <h1><a href="index.html">All files</a> image-compression.js</h1>
237249
<span class="cline-any cline-yes">6x</span>
238250
<span class="cline-any cline-yes">6x</span>
239251
<span class="cline-any cline-neutral">&nbsp;</span>
252+
<span class="cline-any cline-yes">6x</span>
253+
<span class="cline-any cline-yes">6x</span>
254+
<span class="cline-any cline-neutral">&nbsp;</span>
240255
<span class="cline-any cline-neutral">&nbsp;</span>
241256
<span class="cline-any cline-neutral">&nbsp;</span>
242257
<span class="cline-any cline-neutral">&nbsp;</span>
@@ -253,25 +268,30 @@ <h1><a href="index.html">All files</a> image-compression.js</h1>
253268
<span class="cline-any cline-neutral">&nbsp;</span>
254269
<span class="cline-any cline-yes">2x</span>
255270
<span class="cline-any cline-yes">2x</span>
256-
<span class="cline-any cline-yes">9x</span>
257-
<span class="cline-any cline-yes">9x</span>
271+
<span class="cline-any cline-yes">3x</span>
272+
<span class="cline-any cline-yes">3x</span>
258273
<span class="cline-any cline-neutral">&nbsp;</span>
259274
<span class="cline-any cline-neutral">&nbsp;</span>
260-
<span class="cline-any cline-yes">9x</span>
275+
<span class="cline-any cline-yes">3x</span>
261276
<span class="cline-any cline-neutral">&nbsp;</span>
262-
<span class="cline-any cline-yes">9x</span>
277+
<span class="cline-any cline-yes">3x</span>
263278
<span class="cline-any cline-neutral">&nbsp;</span>
264-
<span class="cline-any cline-yes">9x</span>
265-
<span class="cline-any cline-yes">2x</span>
279+
<span class="cline-any cline-yes">3x</span>
280+
<span class="cline-any cline-yes">3x</span>
266281
<span class="cline-any cline-neutral">&nbsp;</span>
267-
<span class="cline-any cline-yes">9x</span>
282+
<span class="cline-any cline-yes">3x</span>
268283
<span class="cline-any cline-neutral">&nbsp;</span>
269-
<span class="cline-any cline-yes">9x</span>
284+
<span class="cline-any cline-yes">3x</span>
270285
<span class="cline-any cline-neutral">&nbsp;</span>
271-
<span class="cline-any cline-yes">9x</span>
286+
<span class="cline-any cline-yes">3x</span>
272287
<span class="cline-any cline-neutral">&nbsp;</span>
273-
<span class="cline-any cline-yes">9x</span>
274-
<span class="cline-any cline-yes">9x</span>
288+
<span class="cline-any cline-yes">3x</span>
289+
<span class="cline-any cline-yes">3x</span>
290+
<span class="cline-any cline-neutral">&nbsp;</span>
291+
<span class="cline-any cline-yes">2x</span>
292+
<span class="cline-any cline-yes">1x</span>
293+
<span class="cline-any cline-yes">1x</span>
294+
<span class="cline-any cline-yes">1x</span>
275295
<span class="cline-any cline-neutral">&nbsp;</span>
276296
<span class="cline-any cline-neutral">&nbsp;</span>
277297
<span class="cline-any cline-yes">2x</span>
@@ -298,13 +318,15 @@ <h1><a href="index.html">All files</a> image-compression.js</h1>
298318
* Compress an image file.
299319
*
300320
* @param {File} file
301-
* @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }
321+
* @param {Object} options
302322
* @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]
303323
* @param {number} [options.maxWidthOrHeight=undefined]
324+
* @param {boolean} [options.useWebWorker=false]
304325
* @param {number} [options.maxIteration=10]
305326
* @param {number} [options.exifOrientation] - default to be the exif orientation from the image file
306327
* @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)
307328
* @param {string} [options.fileType] - default to be the original mime type from the image file
329+
* @param {number} [options.initialQuality=1.0]
308330
* @returns {Promise&lt;File | Blob&gt;}
309331
*/
310332
export default async function compress (file, options) {
@@ -337,14 +359,17 @@ <h1><a href="index.html">All files</a> image-compression.js</h1>
337359
incProgress()
338360
&nbsp;
339361
// exifOrientation
340-
options.exifOrientation = options.exifOrientation || <span class="branch-1 cbranch-no" title="branch not covered" >await getExifOrientation(file)</span>
362+
const exifOrientation = options.exifOrientation || <span class="branch-1 cbranch-no" title="branch not covered" >await getExifOrientation(file)</span>
341363
incProgress()
342-
const orientationFixedCanvas = (await isAutoOrientationInBrowser) ? <span class="branch-0 cbranch-no" title="branch not covered" >maxWidthOrHeightFixedCanvas </span>: followExifOrientation(maxWidthOrHeightFixedCanvas, options.exifOrientation)
364+
const orientationFixedCanvas = (await isAutoOrientationInBrowser) ? <span class="branch-0 cbranch-no" title="branch not covered" >maxWidthOrHeightFixedCanvas </span>: followExifOrientation(maxWidthOrHeightFixedCanvas, exifOrientation)
343365
incProgress()
344366
&nbsp;
345-
let quality = 1
367+
let quality = options.initialQuality || 1.0
346368
&nbsp;
347-
let tempFile = await canvasToFile(orientationFixedCanvas, options.fileType || file.type, file.name, file.lastModified, quality)
369+
const tempFileType = 'image/jpeg'
370+
const outputFileType = options.fileType || file.type
371+
&nbsp;
372+
let tempFile = await canvasToFile(orientationFixedCanvas, outputFileType, file.name, file.lastModified, quality)
348373
incProgress()
349374
&nbsp;
350375
const origExceedMaxSize = tempFile.size &gt; maxSizeByte
@@ -374,10 +399,10 @@ <h1><a href="index.html">All files</a> image-compression.js</h1>
374399
&nbsp;
375400
ctx.drawImage(canvas, 0, 0, newWidth, newHeight)
376401
&nbsp;
377-
if (file.type === 'image/jpeg') {
402+
<span class="missing-if-branch" title="else path not taken" >E</span>if (tempFileType === 'image/jpeg') {
378403
quality *= 0.95
379404
}
380-
compressedFile = await canvasToFile(newCanvas, options.fileType || file.type, file.name, file.lastModified, quality)
405+
compressedFile = await canvasToFile(newCanvas, tempFileType, file.name, file.lastModified, quality)
381406
&nbsp;
382407
cleanupCanvasMemory(canvas)
383408
&nbsp;
@@ -386,6 +411,11 @@ <h1><a href="index.html">All files</a> image-compression.js</h1>
386411
currentSize = compressedFile.size
387412
setProgress(Math.min(99, Math.floor((renderedSize - currentSize) / (renderedSize - maxSizeByte) * 100)))
388413
}
414+
if (tempFileType !== outputFileType) {
415+
compressedFile = new Blob([compressedFile], { type: outputFileType })
416+
compressedFile.name = file.name
417+
compressedFile.lastModified = file.lastModified
418+
}
389419
&nbsp;
390420
cleanupCanvasMemory(canvas)
391421
cleanupCanvasMemory(newCanvas)
@@ -403,7 +433,7 @@ <h1><a href="index.html">All files</a> image-compression.js</h1>
403433
<div class='footer quiet pad2 space-top1 center small'>
404434
Code coverage generated by
405435
<a href="https://istanbul.js.org/" target="_blank">istanbul</a>
406-
at Thu Jun 04 2020 13:37:49 GMT+0800 (香港標準時間)
436+
at Sun Nov 08 2020 23:03:52 GMT+0800 (Hong Kong Standard Time)
407437
</div>
408438
</div>
409439
<script src="prettify.js"></script>

coverage/index.html

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ <h1>All files</h1>
2323
<div class='clearfix'>
2424

2525
<div class='fl pad1y space-right2'>
26-
<span class="strong">76.82% </span>
26+
<span class="strong">77.43% </span>
2727
<span class="quiet">Statements</span>
28-
<span class='fraction'>169/220</span>
28+
<span class='fraction'>175/226</span>
2929
</div>
3030

3131

3232
<div class='fl pad1y space-right2'>
33-
<span class="strong">57.14% </span>
33+
<span class="strong">57.01% </span>
3434
<span class="quiet">Branches</span>
35-
<span class='fraction'>60/105</span>
35+
<span class='fraction'>61/107</span>
3636
</div>
3737

3838

@@ -44,9 +44,9 @@ <h1>All files</h1>
4444

4545

4646
<div class='fl pad1y space-right2'>
47-
<span class="strong">81.07% </span>
47+
<span class="strong">79% </span>
4848
<span class="quiet">Lines</span>
49-
<span class='fraction'>167/206</span>
49+
<span class='fraction'>173/219</span>
5050
</div>
5151

5252

@@ -78,13 +78,13 @@ <h1>All files</h1>
7878
<div class="chart"><div class="cover-fill cover-full" style="width: 100%"></div><div class="cover-empty" style="width: 0%"></div></div>
7979
</td>
8080
<td data-value="100" class="pct high">100%</td>
81-
<td data-value="47" class="abs high">47/47</td>
82-
<td data-value="83.33" class="pct high">83.33%</td>
83-
<td data-value="24" class="abs high">20/24</td>
81+
<td data-value="53" class="abs high">53/53</td>
82+
<td data-value="80.77" class="pct high">80.77%</td>
83+
<td data-value="26" class="abs high">21/26</td>
8484
<td data-value="100" class="pct high">100%</td>
8585
<td data-value="3" class="abs high">3/3</td>
8686
<td data-value="100" class="pct high">100%</td>
87-
<td data-value="47" class="abs high">47/47</td>
87+
<td data-value="53" class="abs high">53/53</td>
8888
</tr>
8989

9090
<tr>
@@ -113,8 +113,8 @@ <h1>All files</h1>
113113
<td data-value="62" class="abs low">27/62</td>
114114
<td data-value="82.61" class="pct high">82.61%</td>
115115
<td data-value="23" class="abs high">19/23</td>
116-
<td data-value="72.09" class="pct medium">72.09%</td>
117-
<td data-value="129" class="abs medium">93/129</td>
116+
<td data-value="68.38" class="pct medium">68.38%</td>
117+
<td data-value="136" class="abs medium">93/136</td>
118118
</tr>
119119

120120
</tbody>
@@ -125,7 +125,7 @@ <h1>All files</h1>
125125
<div class='footer quiet pad2 space-top1 center small'>
126126
Code coverage generated by
127127
<a href="https://istanbul.js.org/" target="_blank">istanbul</a>
128-
at Thu Jun 04 2020 13:37:49 GMT+0800 (香港標準時間)
128+
at Sun Nov 08 2020 23:03:52 GMT+0800 (Hong Kong Standard Time)
129129
</div>
130130
</div>
131131
<script src="prettify.js"></script>

coverage/index.js.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ <h1><a href="index.html">All files</a> index.js</h1>
333333
imageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight
334334
imageCompression.followExifOrientation = followExifOrientation
335335
imageCompression.cleanupMemory = cleanupCanvasMemory
336-
imageCompression.version = '1.0.12'
336+
imageCompression.version = '1.0.13'
337337
&nbsp;
338338
export default imageCompression
339339
&nbsp;</pre></td></tr></table></pre>
@@ -343,7 +343,7 @@ <h1><a href="index.html">All files</a> index.js</h1>
343343
<div class='footer quiet pad2 space-top1 center small'>
344344
Code coverage generated by
345345
<a href="https://istanbul.js.org/" target="_blank">istanbul</a>
346-
at Thu Jun 04 2020 13:37:49 GMT+0800 (香港標準時間)
346+
at Sun Nov 08 2020 23:03:52 GMT+0800 (Hong Kong Standard Time)
347347
</div>
348348
</div>
349349
<script src="prettify.js"></script>

0 commit comments

Comments
 (0)