我在使用php保存png图像时遇到问题。 我遵循了@drew010的建议,如下所述: https://stackoverflow.com/a/11511605/9117408 但是当我尝试打开保存的图像时,我将错误用作我问题的标题
这是将图像发送到php的JS代码:
canvas.className = "photo-canvas";
canvas.width = camera.videoWidth;
canvas.height = camera.videoHeight;
canvas.getContext('2d').drawImage(camera, 0, 0);
if (hat.checked)
{
console.log(canvas.toDataURL());
var xhttp = new XMLHttpRequest();
xhttp.open("POST","backend/functions.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("photo="+canvas.toDataURL());
...
}
这里处理并保存图像的php代码:
$data = $_POST['photo'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents("$_SERVER[DOCUMENT_ROOT]/userphoto/".getdate()[0]."_".$_SESSION['user']['iduser'].".png", $data);
我尝试从计算机打开图像时出现的错误
我尝试将base64图像直接打开到浏览器中,我能够成功查看图像。我有库存问题或者我做错了吗? 感谢