Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将HTML5画布保存到服务器上的文件夹_Javascript_Php_Ajax_Html_Canvas - Fatal编程技术网

Javascript 将HTML5画布保存到服务器上的文件夹

Javascript 将HTML5画布保存到服务器上的文件夹,javascript,php,ajax,html,canvas,Javascript,Php,Ajax,Html,Canvas,我使用HTML5画布编写了一个基于JavaScript的小型绘图程序。我现在尝试实现的是将图形保存到服务器上的文件夹中的功能。文件名应该是这样的:“艺术家”+“标题”+“日期”(我使用提示获取艺术家名称和标题)。 我该怎么做?我知道我必须这样做: var dataURL = canvas.toDataURL(); 然后使用Ajax调用PHP脚本。例如: $.ajax({ type: "POST", url: "saveImg.php", data: {

我使用HTML5画布编写了一个基于JavaScript的小型绘图程序。我现在尝试实现的是将图形保存到服务器上的文件夹中的功能。文件名应该是这样的:“艺术家”+“标题”+“日期”(我使用提示获取艺术家名称和标题)。 我该怎么做?我知道我必须这样做:

var dataURL = canvas.toDataURL();
然后使用Ajax调用PHP脚本。例如:

$.ajax({
    type: "POST",
    url: "saveImg.php",
    data: { 
        imgBase64: dataURL
    }
}).done(function(o) {
console.log('saved'); 
});
但是如何获得PHP脚本的艺术家名称和标题呢?据我所知,文件名是在PHP脚本中定义的,对吗


问候

这可以通过以下方式实现

ᴊᴀᴠᴀꜱᴄʀɪᴘᴛ

var dataURL = canvas.toDataURL();
$.post('saveImg.php', {
    imgBase64: dataURL,
    artist: 'ramy',
    title: 'paint',
    date: new Date().toDateString()
}, function(o) {
    console.log('saved');
});
ᴘʜᴘ

<?php
$img = $_POST['imgBase64'];
$artist = $_POST['artist'];
$title = $_POST['title'];
$date = $_POST['date'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$fileData = base64_decode($img);
$fileName = 'images/'.$artist.'_'.$title.'_'.$date.'.png';
file_put_contents($fileName, $fileData);

向数据对象添加更多属性并相应地分配值
数据:{imgBase64:dataURL,艺术家:'artist name'}