duanhunlou7051 2016-01-20 00:57
浏览 79

使用AJAX发送图像数据并存储在服务器上

PHP isn't receiving data from ajax, I am trying to create image by using canvas HTML5 and store it on my server using PHP, but when I do, I dont receive anything.

image.php:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<canvas width="800" height="420" id="canvas"></canvas>
<script>

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

var img  = loadImage('images/quizes/sim.png' ,main);
var img1 = loadImage('images/users/alfred.jpg', main);
var img2 = loadImage('images/users/brynjar.jpg', main);


var imagesLoaded = 0;
function main() {
    imagesLoaded += 1;

    if(imagesLoaded == 3) {

        ctx.drawImage(img, 0, 0, 800, 420);

        ctx.drawImage(img1, 0, 0, 320, 320);

        ctx.drawImage(img2, 480, 0, 320, 320);

    }
}

function loadImage(src, onload) {
    var img = new Image();

    img.onload = onload;
    img.src = src;

    return img;
}

var dataURL = canvas.toDataURL();

alert(dataURL);

$.ajax({
    type: "POST",
    url: "script.php",
    data: {
        imgBase64: dataURL
    }
}).done(function(o) {
    console.log('saved');
    // If you want the file to be visible in the browser
    // - please modify the callback in javascript. All you
    // need is to return the url to the file, you just saved
    // and than put the image in your browser.
});
</script>

And I also have Script.php:

$img = $_POST['data'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$fileData = base64_decode($img);
//saving
$fileName = 'photo.png';
file_put_contents($fileName, $fileData);

But when I run image.php, $_post['data'] doesn't pick up any data.

  • 写回答

1条回答 默认 最新

  • douguai6716 2016-01-27 03:30
    关注

    You should read data from $_POST['imgBase64'] variable.

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式