Skip to content

Commit 1ab6277

Browse files
committed
Output as JPEG if browser doesn't support WebP
This is necessary to avoid unexpectedly large files in Safari.
1 parent 00dd125 commit 1ab6277

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cropt",
3-
"version": "0.8.4",
3+
"version": "0.8.6",
44
"description": "A lightweight JavaScript image cropper",
55
"files": [
66
"src/cropt.*"

src/cropt.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ function getArrowKeyDeltas(key: string): [number, number] {
100100
}
101101
}
102102

103+
function canvasSupportsWebP() {
104+
// https://caniuse.com/mdn-api_htmlcanvaselement_toblob_type_parameter_webp
105+
return document.createElement('canvas')
106+
.toDataURL('image/webp')
107+
.startsWith('data:image/webp');
108+
}
109+
103110
type RecursivePartial<T> = {
104111
[P in keyof T]?: RecursivePartial<T[P]>;
105112
};
@@ -252,6 +259,10 @@ export class Cropt {
252259
}
253260

254261
toBlob(size: number | null = null, type = "image/webp", quality = 1): Promise<Blob> {
262+
if (type === "image/webp" && quality < 1 && !canvasSupportsWebP()) {
263+
type = "image/jpeg";
264+
}
265+
255266
return new Promise((resolve, reject) => {
256267
this.toCanvas(size).then((canvas) => {
257268
canvas.toBlob((blob) => {

0 commit comments

Comments
 (0)