简体   繁体   English

无法将完整的画布图像保存到服务器

[英]Can't save full canvas image to server

I am having a problem saving a canvas image to my server. 我在将画布图像保存到服务器时遇到问题。 I have tried multiple things and the only thing that seems to work right now is using one suggestion I found looking around which was to fill a hidden text field in a form and submit it to my php page that can take the post and convert it and save it on the server. 我已经尝试了多种方法,现在看来唯一可行的方法是使用一个建议,我发现环顾四周的建议是在表单中填充一个隐藏的文本字段,然后将其提交到我的php页面,该页面可以接收帖子并将其转换为将其保存在服务器上。 While this does work, it is only posting 1/8th of the image. 虽然这确实有效,但仅发布图像的1/8。

When I check the toDataURL() contents it is the same amount of data that is passed on to the PHP page so I'm not sure where the image is getting chopped up and the image size only ends up about 380kb. 当我检查toDataURL()的内容时,传递给PHP页面的数据量是相同的,因此我不确定图像在何处被切掉,图像大小最终仅约380kb。

Here is what I have: 这是我所拥有的:

Javascript: 使用Javascript:

var canvas = document.getElementById("myCanvas");
var image = canvas.toDataURL();
document.getElementById("savecarddata").value = image;

PHP PHP

$upload_dir = 'mydirectory'; 
$img = $_POST['savecarddata'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = $upload_dir.rand().".png";
$success = file_put_contents($file, $data);

This does save the image but only the top of it. 这确实会保存图像,但只会保存图像的顶部。 I've tried ajax but I can't seem to get it to work right so this was the next best solution for me. 我已经尝试过ajax,但似乎无法正常工作,因此这对我来说是下一个最佳解决方案。 I would love any help! 我希望有任何帮助!

Thanks in advance! 提前致谢!

This is how I did it: 这是我的方法:

Using ajax 使用ajax

 var canvas = document.getElementById("myCanvas");
    var canvasData = canvas.toDataURL('image/png');
    var ajax = new XMLHttpRequest();            
    var postdata='saveimage.php';
    ajax.open('POST', postdata, false);
    ajax.setRequestHeader('Content-Type', 'application/upload');
    ajax.send(canvasData);  

then in saveimage.php 然后在saveimage.php中

if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
$imgdata = $GLOBALS['HTTP_RAW_POST_DATA'];

    $filteredData=substr($imgdata, strpos($imgdata, ",")+1);// Remove the headers (data:,) part.
    $data = base64_decode($filteredData);//decode data
    $im = imagecreatefromstring($data); //create image
    if ($im !== false) { 
        imagesavealpha($im, true);
        imagepng($im, "result.png",9); 
    }
    else {
        echo 'An error occurred.';
    }

    }

you can also pass arguments as GET variables in the ajax 您也可以在ajax中将参数作为GET变量传递

var postdata="saveimage.php?imagename=" + document.getElementById("someFeild").value;

I don't know if it's relevant but check how you set the canvas width and height. 我不知道它是否相关,但是请检查如何设置画布的宽度和高度。 You should set them inside the canvas tag. 您应该将它们设置在canvas标签内。

<canvas width='yourValue' height='yourValue'></canvas>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM