生成艺术项目通常需要能够将创建的图像保存到服务器。以下是涉及的步骤:
JavaScript 代码:
function saveImage() { var canvasData = canvas.toDataURL("image/png"); var ajax = new XMLHttpRequest(); ajax.open("POST", "testSave.php", false); ajax.onreadystatechange = function() { console.log(ajax.responseText); } ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); ajax.send("imgData=" + canvasData); }
PHP (testSave.php):
<?php $data = $_POST['imgData']; $file = "/path/to/file.png"; $uri = substr($data,strpos($data, ",") + 1); file_put_contents($file, base64_decode($uri)); echo $file; ?>
常见问题:
额外提示:
以上是如何将 HTML5 Canvas 图像保存到服务器?的详细内容。更多信息请关注PHP中文网其他相关文章!