javascript - canvas.toDataURL() 通过尝试调整 base64 字符串大小来生成黑色图像

标签 javascript image canvas base64 image-resizing

我目前正在尝试调整 base64 图像的大小,因为图像文件太大,无法稍后使用 php 进行处理。我找到了一种通过使用 Canvas 调整图像大小来实现此目的的方法。不幸的是,我得到的图像只是一个黑场,宽 300 像素,高 150 像素。也许它与 img.onloadcanvas.toDataURL() 顺序有关,或者我只是使用了错误的事件(img.onload >)。知道错误可能出在哪里吗?

function exportImg(val){  
                var imageData = $('#image-cropper').cropit('export', {originalSize: true});
                imageData = imageData.replace(/^data:image\/[a-z]+;base64,/, "");

                var imageDataRes = resize(imageData);


                $.post('php/upload.php', { imageDataRes: imageDataRes });
        }



function resize(base64){
            // Max size for thumbnail
            var maxWidth = 900;
            var maxHeight = 900;

            // Create and initialize two canvas
            var canvas = document.createElement("canvas");
            var ctx = canvas.getContext("2d");
            var canvasCopy = document.createElement("canvas");
            var copyContext = canvasCopy.getContext("2d");

            // Create original image
            var img = new Image();
            img.src = base64;

            img.onload = function(){

                // Determine new ratio based on max size
                var ratio = 1;
                if(img.width > maxWidth) {
                    ratio = maxWidth / img.width;
                }
                else if(img.height > maxHeight) {
                    ratio = maxHeight / img.height;
                }


            // Draw original image in second canvas
            canvasCopy.width = img.width;
            canvasCopy.height = img.height;
            copyContext.drawImage(img, 0, 0);

            // Copy and resize second canvas to first canvas
            canvas.width = img.width * ratio;
            canvas.height = img.height * ratio;
            ctx.drawImage(canvasCopy, 0, 0, canvasCopy.width, canvasCopy.height, 0, 0, canvas.width, canvas.height);

            }
            alert(canvas.toDataURL());

            return canvas.toDataURL();
        }

编辑:

在这种情况下什么是异步以及如何解决它?抱歉,但不幸的是我不知道这对我有什么进一步的帮助。 $.post 工作完美,我得到了图像。我只是不明白 img.onloadtoDataURL() 以及如何将它们从一个函数解析为另一个函数。起初,我得到了一个空白结果,根本没有字符串(只有 data,),但是通过添加这个 img.onload 我终于得到了一些 base64 字符串......但只是黑屏。

最佳答案

使用全局变量保存数据并调用上传函数后,需要等待onload事件。

试试这个:

function exportImg(val){  
                var imageData = $('#image-cropper').cropit('export', {originalSize: true});
                imageData = imageData.replace(/^data:image\/[a-z]+;base64,/, "");

                  resize(imageData);

        }


var SendWhenisReady = function(imageDataRes){

 $.post('php/upload.php', { imageDataRes: imageDataRes });

};

function resize(base64){
            // Max size for thumbnail
            var maxWidth = 900;
            var maxHeight = 900;

            // Create and initialize two canvas
            var canvas = document.createElement("canvas");
            var ctx = canvas.getContext("2d");
            var canvasCopy = document.createElement("canvas");
            var copyContext = canvasCopy.getContext("2d");

            // Create original image
            var img = new Image();
            img.src = base64;

            img.onload = function(){

                // Determine new ratio based on max size
                var ratio = 1;
                if(img.width > maxWidth) {
                    ratio = maxWidth / img.width;
                }
                else if(img.height > maxHeight) {
                    ratio = maxHeight / img.height;
                }


            // Draw original image in second canvas
            canvasCopy.width = img.width;
            canvasCopy.height = img.height;
            copyContext.drawImage(img, 0, 0);

            // Copy and resize second canvas to first canvas
            canvas.width = img.width * ratio;
            canvas.height = img.height * ratio;
            ctx.drawImage(canvasCopy, 0, 0, canvasCopy.width, canvasCopy.height, 0, 0, canvas.width, canvas.height);


                 alert(canvas.toDataURL());
                 window['imageDataRes'] = canvas.toDataURL();
                 SendWhenisReady(window['imageDataRes'])
            }



        }

关于javascript - canvas.toDataURL() 通过尝试调整 base64 字符串大小来生成黑色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44380039/

相关文章:

javascript - 如何确保父组件将填充的 props 传递给 VueJS 中的子组件

javascript - 函数调用时出现无效参数错误,特别是 IE 和 FF

javascript - TestCafe beforeEach 钩子(Hook) - 如何执行一个函数并声明一个变量

javascript - `for of` 循环是否重新评估过滤后的数组?

css - IE 不支持图像的 height=auto,我应该使用什么?

php - 在 mySQL 数据库中存储图像的问题

html - 清除 Canvas 矩形(但保留背景)

javascript - HTML Canvas 在两点之间绘制圆弧

css - 背景大小不工作的CSS

java - 将 MouseListener 与 vlcj 一起使用