php - 将 Canvas 图像保存到 mysql

标签 php javascript html mysql ajax

我每 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/

相关文章:

URL 查询的 PHP parse_str 返回键名中包含 'amp' 的数组键

javascript - 类型错误 : Cannot read property 'ref' of undefined Firebase-storage react

javascript - 在一个移动市场上上传应用程序时出错

javascript - 对 DOM 事件监听器感到困惑

javascript - jQuery:当点击的元素都共享相同的 CSS 类时,我如何定位它们?

PHP 文件不会在浏览器中更新

php - Yii 中的多个 CJuiAutocomplete - 项目不呈现

php - 如何获得 laravel orm 的最后增量?

javascript - jQuery 延迟 AJAX 调用返回值

html - 如何防止 W3C Validator 解析内部 CSS?