Donaldcwl / browser-image-compression

Image compression in web browser
MIT License
1.3k stars 160 forks source link

[bug] specify the maximum size of Canvas supported by each browser #112

Closed raccoonback closed 3 years ago

raccoonback commented 3 years ago

Purpose

Each browser limits the maximum size of a Canvas object. So, need to resize the image to less than the maximum size that each browser restricts. (However, the proportion/ratio of the image remains.)

you can test it using image

Cause

If the maximum size of Canvas supported by a browser that does not support OfflineCanvas is exceeded, canvas.toDataURL() returns "data:,".

https://github.com/Donaldcwl/browser-image-compression/blob/39665bd16eb5c4a2fdd2c40aa8fee0e2fd435ef8/lib/utils.js#L161-L164

If the height or width of the canvas is 0 or larger than the maximum canvas size, the string "data:," is returned.

Therefore, the error i[0].match(/:(.*?);/)[1] in getFilefromDataUrl().

https://github.com/Donaldcwl/browser-image-compression/blob/39665bd16eb5c4a2fdd2c40aa8fee0e2fd435ef8/lib/utils.js#L63-L69

*So when we create an Image object, if we set width, height within the maximum size of Canvas supported by the browser, we can prevent not only Black image, but also `i[0].match(/:(.?) ;/)[1]errors. ( theproportion/ratio` of the image remains.)**

How to

Releated issue