如何将画布图像“data:image/jpeg;base64,..”转换为普通图像文件 - javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27602819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert canvas image "data:image/jpeg;base64,.." to normal image file - javascript
提问by doniyor
I have
我有
image = canvas.toDataURL("image/png", 1);
but this is returning
但这又回来了
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDA.......
but I need normal image like src="myimage.png"
但我需要正常的图像 src="myimage.png"
how can I get it from canvas? i googled a lot, cannot seem to find what i need...
我怎样才能从画布上得到它?我用谷歌搜索了很多,似乎找不到我需要的东西......
回答by C???
The string that you're getting canbe used as the source attribute for an image, you just have to create a new Image
element to which you can assign it:
您获得的字符串可以用作图像的源属性,您只需创建一个新Image
元素即可将其分配给它:
var image = new Image();
image.src = canvas.toDataURL("image/png", 1);
// append image to DOM
EDIT: Given the comments, you want to turn this into something that be stored in a database. You should POST
the result of toDataURL
to your server. Use your server-side technology's IO/image/graphics libraries to decode the base64-encoded part of the data string and write that out to a new file. You can now store that path in your database.
编辑:根据评论,您想将其转换为存储在数据库中的内容。您应该POST
将结果toDataURL
发送到您的服务器。使用服务器端技术的 IO/图像/图形库对数据字符串的 base64 编码部分进行解码,并将其写入新文件。您现在可以将该路径存储在您的数据库中。
Alternative: just store that whole data string in your database. Then when you render your <img>
tag you can simply dump it into the src
attribute:
替代方案:只需将整个数据字符串存储在您的数据库中。然后当你渲染你的<img>
标签时,你可以简单地将它转储到src
属性中:
<img src="<< base-64 encoded data string>>" />