Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas drawImage Crash on large images

I am creating a mobile app., where the user can choose a picture from mobile then I blur it.... The problem is that when the user choose a large picture(More than 2 MB), the app. crash.

JS Code:

convert_local_image_base64: function(url, callback) {
    var canvas = document.createElement('CANVAS'),
            ctx = canvas.getContext('2d'),
            img = new Image;
    img.crossOrigin = 'anonymous';
    img.onload = function() {
        canvas.height = img.height;
        canvas.width = img.width;
        ctx.drawImage(img, 0, 0);
        var dataURL = canvas.toDataURL('image/png');
        callback.call(this, dataURL);
        canvas = null;
    };
    img.src = url;
},

So does there are another way to achieve similar operation?!.

like image 302
Moussawi7 Avatar asked May 13 '14 11:05

Moussawi7


1 Answers

ON PC: -
I tried multiple large sized images (more than 2 MB) to draw on a canvas. I also tried to get its dataURL and redraw it on different canvas. Every thing worked fine for me. (Tried on IE11, Chrome and Firefox.)

Please make sure you not messing with Cross-origin resource sharing (CORS).

Also I found some posts here on stackoverflow related to resolutions on canvas.

Maximum size of a <canvas> element

Is there a max size for images being loaded onto canvas on iPhone?

EDITED: ON Mobile devices-
Here is the restrictions for the canvas for mobile devices:-

The maximum size for a canvas element is 3 megapixels for devices with less than 256 MB RAM and 5 megapixels for devices with greater or equal than 256 MB RAM.

So for example - if you want to support Apple’s older hardware, the size of your canvas cannot exceed 2048×1464.

Hope these resources will help you to pull you out.

like image 115
Manish Gupta Avatar answered Sep 30 '22 04:09

Manish Gupta