简体   繁体   English

将图片上传,调整大小并编码为base64会使Chrome在移动设备上崩溃

[英]Image upload, resizing and encoding to base64 crashes Chrome on mobile

My users can select an image using the file-upload HTML input element - 我的用户可以使用文件上传HTML输入元素选择图片-

1. From there I downscale 1.从那里我缩小

2. I convert to base64 2.我转换为base64

For some reason Chrome mobile & Android browser completely crash - and display an 'Out of Memory error'. 由于某些原因,Chrome移动和Android浏览器完全崩溃-并显示“内存不足错误”。

If the browser runs on a more 'modern/capable' device all goes perfectly fine. 如果浏览器在更“现代/功能更强”的设备上运行,则一切都会很好。

What could be causing the error here - can it be fixed? 这是什么导致错误-可以解决吗?


Here is the function that downscales(whilst keeping aspect ratios) and returns a Base64 string of the image. 这是缩小(同时保持宽高比)并返回图像的Base64字符串的函数。

function resizeAndConvertB64(img, maxWidth, maxHeight) {
    var imgWidth = img.width, 
        imgHeight = img.height;

    var ratio = 1, ratio1 = 1, ratio2 = 1;
    ratio1 = maxWidth / imgWidth;
    ratio2 = maxHeight / imgHeight;

    // Use the smallest ratio that the image best fit into the maxWidth x maxHeight box.
    if (ratio1 < ratio2) {
        ratio = ratio1;
    }
    else {
        ratio = ratio2;
    }
    var canvas = document.createElement("canvas");
    var canvasContext = canvas.getContext("2d");
    var canvasCopy = document.createElement("canvas");
    var copyContext = canvasCopy.getContext("2d");
    var canvasCopy2 = document.createElement("canvas");
    var copyContext2 = canvasCopy2.getContext("2d");
    canvasCopy.width = imgWidth;
    canvasCopy.height = imgHeight;  
    copyContext.drawImage(img, 0, 0);

    // init
    canvasCopy2.width = imgWidth;
    canvasCopy2.height = imgHeight;        
    copyContext2.drawImage(canvasCopy, 0, 0, canvasCopy.width, canvasCopy.height, 0, 0, canvasCopy2.width, canvasCopy2.height);


    var rounds = 1;
    var roundRatio = ratio * rounds;
    for (var i = 1; i <= rounds; i++) {


        // tmp
        canvasCopy.width = imgWidth * roundRatio / i;
        canvasCopy.height = imgHeight * roundRatio / i;

        copyContext.drawImage(canvasCopy2, 0, 0, canvasCopy2.width, canvasCopy2.height, 0, 0, canvasCopy.width, canvasCopy.height);

        // copy back
        canvasCopy2.width = imgWidth * roundRatio / i;
        canvasCopy2.height = imgHeight * roundRatio / i;
        copyContext2.drawImage(canvasCopy, 0, 0, canvasCopy.width, canvasCopy.height, 0, 0, canvasCopy2.width, canvasCopy2.height);

    } // end for


    // return Base64 string of the downscaled image
    canvas.width = imgWidth * roundRatio / rounds;
    canvas.height = imgHeight * roundRatio / rounds;
    canvasContext.drawImage(canvasCopy2, 0, 0, canvasCopy2.width, canvasCopy2.height, 0, 0, canvas.width, canvas.height);
    var dataURL = canvas.toDataURL();
    return dataURL;


}

I think it is an error of Chrome mobile. 我认为这是Chrome移动版的错误。
Chrome mobile is not stable and my handy crashes often when I use it. Chrome移动版不稳定,当我使用它时,经常会崩溃。
The out of memory message says that your device hasn't got enough memory for this script. 内存不足消息表明您的设备没有足够的内存用于此脚本。
Check if it works with Dolphin Browser. 检查它是否与Dolphin Browser一起使用。
I think it will work but I don't know it. 我认为它将起作用,但我不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM