gpt4 book ai didi

php - 将 Canvas 图像保存到 mysql

转载 作者:行者123 更新时间:2023-11-28 03:34:38 24 4
gpt4 key购买 nike

我每 5 秒运行一次 javascript 来调用 update.php 文件(这有望将 base64 代码添加到 mysql - 尽管尚未弄清楚)。

目前,我只是将 base64 代码写入名为 debugConsole 的文本区域。

我只是不明白应该如何将 javascript 变量传递到我的 php 更新文件。

HTML:

<script type="text/javascript">
function saveViaAJAX()
{
var testCanvas = document.getElementById("testCanvas");
var canvasData = testCanvas.toDataURL("image/png");
var postData = "canvasData="+canvasData;
var debugConsole= document.getElementById("debugConsole");
debugConsole.value=canvasData;

//alert("canvasData ="+canvasData );
var ajax = new XMLHttpRequest();
//ajax.open("POST",'testSave.php',true);
ajax.setRequestHeader('Content-Type', 'canvas/upload');
//ajax.setRequestHeader('Content-TypeLength', postData.length);


ajax.onreadystatechange=function()
{
if (ajax.readyState == 4)
{
//alert(ajax.responseText);
// Write out the filename.
document.getElementById("debugFilenameConsole").innerHTML="Saved as<br><a target='_blank' href='"+ajax.responseText+"'>"+ajax.responseText+"</a><br>Reload this page to generate new image or click the filename to open the image file.";
}
}

ajax.send(postData);
}
setInterval(function() { saveViaAJAX(); }, 5000);
</script>

<script language='javascript' type='text/javascript'>
//Every 5 seconds calls php update
//syncronized jax:
function myjax() {
oXhr = new XMLHttpRequest();
oXhr.open("POST", "update.php?game=<?php echo $game; ?>", false);
oXhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
oXhr.send(null);
}

//set an interval each 5 seconds to call your myjax method
setInterval(function() { myjax(); }, 5000);
</script>
</head>



<div>
<canvas id="testCanvas" width="300" height="300"></canvas>
</div>
<div>
<textarea id="debugConsole" rows="10" cols="60">Data</textarea>


</div>
<script type="text/javascript">
// This portion of the code simply draws random circles into the canvas (it has nothing todo with saving the canvas).
var canvas = document.getElementById("testCanvas");
if (canvas.getContext)
{
var canvasContext = canvas.getContext("2d");
for (i=0; i<150; i++)
{
canvasContext.fillStyle="rgb("+(parseInt(Math.random()*255))+","+(parseInt(Math.random()*255))+","+(parseInt(Math.random()*255))+")";
canvasContext.beginPath();
canvasContext.arc(Math.random()*350,Math.random()*350,Math.random()*20, 0,Math.PI*2,true);
canvasContext.fill();
}
}
</script>

PHP:

<?php

$random = rand();
$game = $_GET["game"];

mysql_query("UPDATE paint_s SET paint_points='$random' WHERE id ='$game'") or die(mysql_error());
echo "Updated";
?>

抱歉,我缺乏知识,我一直在做一些教程,只是想扩展教程。

最佳答案

您要做的第一件事是打开您的请求

ajax.open("POST",'testSave.php',true);

然后想要将您的数据作为application/x-www-url-form-encoded 发送,所以在内容类型中设置它

ajax.setRequestHeader('Content-Type', 'application/x-www-url-form-encoded');

要确保您的数据正确编码,请使用 encodeURIComponent

var postData = "canvasData="+encodeURIComponent(canvasData);

您还应该检查状态以查看请求是否真正成功

if (ajax.readyState == 4)
{
if (ajax.status == 200){
//success
}
else{
//failure
}
}

我不知道每 5 秒保存一次 base64 编码的图像是否是个好主意。

关于php - 将 Canvas 图像保存到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15256703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com