gpt4 book ai didi

ajax - 无法将 Canvas 数据从 Javascript AJAX 发送到 PHP 页面

转载 作者:行者123 更新时间:2023-12-02 04:51:38 24 4
gpt4 key购买 nike

我的页面上有一个视频元素,显示网络摄像头流。接下来,我有一个 canvas,它具有拍摄快照 并在 Canvas 中显示图像的功能。现在我需要将此 Canvas 图像发送到服务器并使用 PHP 将其保存到文件中。我在网上结合了各种教程,最后得到了下面的代码,但是保存在服务器文件夹中的图像只是空白,所以我猜 PHP 文件没有正确接收要显示的数据...如果可以,请提供帮助:) 只有 Jquery + AJAX 不传输 post 变量才是问题...这是代码:

var int=self.setInterval(function(){snapshot()},2000);

function snapshot() {
// Draws current image from the video element into the canvas
ctx.drawImage(output, 0,0, canvas.width, canvas.height);
var dataURL = canvas.toDataURL();
dataURL = dataURL.replace('data:image/png;base64,','');

$.ajax({
type: 'POST',
url: 'onlinewebcams.php',
data: postData,
contentType: 'application/json'
});

}

然后有一个单独的 PHP 文件,但包含在保存 POST 图像的同一页面上(不起作用):

这是 PHP 文件代码:

sleep(5); // wait 5 seconds
define('UPLOAD_DIR', 'allwebcams/');
$img = $_POST['dataURL'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR .uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';

最佳答案

一些事情:

客户端:

在您的 ajax POST 中,您没有定义 postData,因此 ajax 正在发送一个空字符串。

您应该改为发送 dataURL。你的数据包就是一个未命名的字符串。

我更喜欢将数据包装到一个对象中。这样,如果我以后想发送更多信息而不仅仅是编码图像,我只需向对象添加另一个属性。例如,您可能想要发送生成图像的网络摄像头的 ID 以及生成图像的时间。

顺便说一句,发送的内容类型不是 json,它是 base64 编码的图像数据。您可以省略内容类型,因为您的 PHP 知道将要处理的数据类型。

此外,您可能希望从 PHP 获得指示成功/失败的响应,或者可能是它刚刚保存的文件的名称。您可以使用 ajax POST 中的 .done 响应回调处理程序来做到这一点。

试试这个 AJAX POST:

// create a dataUrl from the canvas
var dataURL= canvas.toDataURL();

// post the dataUrl to php
$.ajax({
type: "POST",
url: "onlinewebcams.php",
data: {image: dataURL}
}).done(function( respond ) {
// you will get back the temp file name
// or "Unable to save this image."
console.log(respond);
});

您可以在 ajax 之前或在 PHP 内部去除数据类型 header (data:image/png;base64)。

在您的代码中,您在客户端代码和 PHP 代码中都删除了它——不必要的 ;)

PHP

我不太确定你为什么在 php 代码中 sleep ——无论如何......

在您的 PHP 中,您应该始终在尝试处理数据之前检查数据是否存在且不为空。某些 php 服务器在收到空数据包时会特别不高兴。

您的 $img=$_POST['dataURL'] 正在请求一个不存在的命名数据。这就是 php 创建空文件的原因。

试试这个 PHP 代码:

// make sure the image-data exists and is not empty
// xampp is particularly sensitive to empty image-data
if ( isset($_POST["image"]) && !empty($_POST["image"]) ) {

// get the dataURL
$dataURL = $_POST["image"];

// the dataURL has a prefix (mimetype+datatype)
// that we don't want, so strip that prefix off
$parts = explode(',', $dataURL);
$data = $parts[1];

// Decode base64 data, resulting in an image
$data = base64_decode($data);

// create a temporary unique file name
$file = UPLOAD_DIR . uniqid() . '.png';

// write the file to the upload directory
$success = file_put_contents($file, $data);

// return the temp file name (success)
// or return an error message just to frustrate the user (kidding!)
print $success ? $file : 'Unable to save this image.';

}

还有常见的陷阱:

  • 确保您已正确设置上传目录。
  • 确保您已正确设置上传目录的权限。

关于ajax - 无法将 Canvas 数据从 Javascript AJAX 发送到 PHP 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18736023/

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