简体   繁体   English

来自画布的无效base64字符串

[英]Invalid base64 string from canvas

I'm making a web application that'll be used on an iPad. 我正在制作将在iPad上使用的Web应用程序。 Users will be invited to color an image. 将邀请用户为图像着色。 I'm using html5 Canvas for this and basing the coloring on the tutorial I found HERE . 我为此使用html5 Canvas并将颜色基于我在这里找到教程 Eventually I want to upload this image to a Database along with the name of the user. 最终,我想将此图像连同用户名一起上传到数据库。

How I'm doing it 我怎么做

When the user is done coloring he hits 'save' and I convert the canvas using toDataURL(). 当用户完成着色后,他单击“保存”,然后我使用toDataURL()转换了画布。 I then insert the data into an invisible form element and submit the form to go to a new page. 然后,我将数据插入不可见的表单元素中,然后提交表单以进入新页面。 Like so: 像这样:

    document.getElementById('my_hidden').value = canvas.toDataURL('image/png');
    document.forms["form1"].submit();

Here I ask the user's name and e-mail. 在这里,我询问用户的姓名和电子邮件。 I also insert the canvas data into a new invisible form element. 我还将画布数据插入到新的不可见表单元素中。 Like so: 像这样:

    value="<?php echo $_POST['my_hidden'];?>"

I then submit this form to the database on the next php page. 然后,我将此表单提交到下一个php页面上的数据库。 The canvas data is stored in a Blob. 画布数据存储在Blob中。

This all works fine. 这一切都很好。

What's going wrong 怎么了

When I try to display the image on that final page I get a 'Failed to load resource'-error on the image ('kCFErrorDomainCFNetwork error'). 当我尝试在最后一页上显示图像时,图像上出现“无法加载资源”错误(“ kCFErrorDomainCFNetwork错误”)。 I'm trying to display it this way: 我试图以这种方式显示它:

    var canvas = document.getElementById("myCanvas");
    var ctx = canvas.getContext("2d");
    var image = new Image();

    image.src = "<?php echo str_replace("\n", "", $_POST['canvas']); ?>";
    ctx.drawImage(image, 0, 0);

Now I suspected that maybe the canvas data I got from the sever is bad. 现在我怀疑从服务器获取的画布数据可能不好。 So I checked it this way: 所以我以这种方式检查了它:

    if ( base64_encode(base64_decode($canvas)) === $canvas){
       echo '$data is valid';
    } else {
       echo '$data is NOT valid';
    }

And it is, indeed, not valid. 这确实是无效的。 I then used that same method to check it after the first form and it's already invalid then. 然后,我使用相同的方法在第一种形式之后对其进行检查,然后它已经无效。 So I suppose either the first submitting through that hidden field is messing it up or the toDataURL() is. 因此,我想通过该隐藏字段进行的第一个提交会将其弄乱了,或者是toDataURL()。

I'm by no means an expert programmer but I'd hate to get this far and not finish this. 我绝不是专家程序员,但我不希望自己走这么远,而不能完成此工作。 So any help would be truly, greatly appreciated! 因此,任何帮助将得到我们的真正感谢! Thanks. 谢谢。

Looks like you're forgetting to snip off the start of the data URI to leave yourself with just the data 好像您忘了剪掉数据URI的开头,只剩下数据了

var uri = canvas.toDataURL('image/png'),   // data:image/png;base64,blahblahblah
    b64 = uri.slice(uri.indexOf(',') + 1); // blahblahblah
document.getElementById('my_hidden').value = b64;

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

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