FireFox:使用画布对象的图像base64数据不起作用 [英] FireFox : image base64 data using canvas object not working

查看:248
本文介绍了FireFox:使用画布对象的图像base64数据不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我编写的用于按纵横比调整图像大小的代码,它在chrome上有效,但在Firefox上却不显示,有人知道这是什么错误.

This is the code i wrote to resize the image in aspect ratio, it works on chrome but not display on firefox, does anyone know what is wrong.

var image = new Image();

        image.src = data;

        //$(image).load(function () {

            var aspectRatio = getAspectRatio(parseFloat($(image).prop('naturalWidth')),
                              parseFloat($(image).prop('naturalHeight')),
                              dstWidth,
                              dstHeight);

            var canvas = document.createElement('canvas');
            canvas.width = dstWidth;
            canvas.height = dstHeight;

            var x = (dstWidth - aspectRatio[0]) / 2;
            var y = (dstHeight - aspectRatio[1]) / 2;
            var ctx = canvas.getContext("2d");
                ctx.drawImage(image, x, y, aspectRatio[0], aspectRatio[1]);

            return canvas.toDataURL("image/png");

这是canvas.toDataURL生成的工作

This is work it generated by the canvas.toDataURL

数据:图像/PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAQEAAADACAYAAAAEL9ZYAAAA1klEQVR4nO3BAQ0AAADCoPdPbQ8HFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADwYD7QAB/UrDfgAAAABJRU5ErkJggg ==

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQEAAADACAYAAAAEL9ZYAAAA1klEQVR4nO3BAQ0AAADCoPdPbQ8HFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADwYD7QAB/UrDfgAAAABJRU5ErkJggg==

推荐答案

要使其正常工作,您将需要处理图像加载的异步特性.您将不得不在这里使用回调机制.它在Chrome中运行"的原因是偶然的;

To make it work you will need to handle the asynchronous nature of image loading. You will have to use a callback mechanism here. The reason it "works" in Chrome is accident; that is the image happen to be in the cache when you try and/or the browser is able to deliver the uncompressed/decoded image before you use the image in the drawImage call.

对于大多数用户而言,这可能在在线时不起作用,因此可以正确处理加载-

This will probably not work when it's online for most users so to properly handle loading you can do -

示例:

function getImageUri(url, callback) {

    var image = new Image();
    image.onload = function () { // handle onload

        var image = this;  // make sure we using valid image

        var aspectRatio = getAspectRatio(parseFloat($(image).prop('naturalWidth')),
        parseFloat($(image).prop('naturalHeight')),
        dstWidth,
        dstHeight);

        var canvas = document.createElement('canvas');
        canvas.width = dstWidth;
        canvas.height = dstHeight;

        var x = (dstWidth - aspectRatio[0]) / 2;
        var y = (dstHeight - aspectRatio[1]) / 2;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(image, x, y, aspectRatio[0], aspectRatio[1]);

        // use callback to provide the finished data-uri
        callback(canvas.toDataURL());
    }
    image.src = url; // set src last
}

然后以这种方式使用它:

Then use it this way:

getImageUri(myURL, function (uri) {
    console.log(uri); // contains the image as data-uri
});

这篇关于FireFox:使用画布对象的图像base64数据不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆