简体   繁体   English

base64图像不解码php

[英]base64 image not decoding php

I have an image in HTML5 canvas which is taken from webcam's live stream. 我在HTML5画布中有一个图像,它来自网络摄像头的直播。 When I get a frame as snapshot and save it in canvas I do this 当我将帧作为快照并将其保存在画布中时,我会这样做

var myImage = canvas.toDataURL("image/png");
dom.write('<form method="post" action="upload.php"><input type="myImage" value="'+myImage+'" /><input type="submit" value="Submit" /></form>')

and in upload.php I do this 并在upload.php我这样做

header("Content-type: image/png");
$img= $_GET["myImage"];
echo '<img src="data:image/gif;base64,'.$img.'" />';

doing this I get a popup window when page is loaded and when the form is submitted, it give me 这样做我在加载页面时提供了一个弹出窗口,当提交表单时,它会给我

data:image/png;base64,"VERY HUGE STRING"

I want to get it as an image. 我想把它作为一个图像。 Please Help 请帮忙

Your call to header suggests you want to output the actual PNG image as the response to the request. 您对header调用表明您希望输出实际的PNG图像作为对请求的响应。 However, you then write out an HTML tag, which is not valid in a PNG file. 但是,然后您写出一个HTML标记,该标记在PNG文件中无效。

If you want to write out the PNG image itself, presuming $img contains a valid PNG-format image, you can decode the base64 with the base64_decode function: 如果你想写出PNG图像本身,假设$img包含一个有效的PNG格式图像,你可以使用base64_decode函数解码base64:

echo base64_decode($img);

If you want to show the image on an HTML page, change your header call to read Content-Type: text/html . 如果要在HTML页面上显示图像,请将header调用更改为“ Content-Type: text/html

canvas.toDataURL()已包含data:image/***;base64, part,因此将其从输出中删除。

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

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