使用canvas.todataurl()的jQuery帖子大小限制

时间:2012-11-05 09:03:46

标签: php jquery canvas

我正在尝试使用jQuery post和php上传canvas.todataurl()图像(getUserMedia)到服务器来处理数据,但我遇到了一些问题。我上传的所有图片最终都被破坏了,一半图像丢失了。我还有一个MySQL数据库,我存储与图像相关的数据(标题,文本,日期等)。似乎我拥有的相关数据越多,图像就越破坏。

因此,我想知道这是浏览器限制还是与jQuery帖子有关。我还检查了PHP max_post_size并且它是16mb,所以这应该不是问题。我无权访问服务器设置。我很困惑,我该怎么办?是否可以将canvas.todataurl()划分为多个部分,然后发布?

的JavaScript

window.addEventListener('DOMContentLoaded', function() {

var video = document.getElementById('videoStream');
var canvas = document.getElementById('canvasImage');
var status = document.getElementById('status');
var button = document.getElementById('button');
//var others = document.getElementById('others');
var imageHolder;
document.getElementById('form').style.display = 'none';
var image = null; //  kuvan datauri joka lähtee php:lle

window.URL || (window.URL = window.webkitURL || window.mozURL || window.msURL); 
navigator.getUserMedia || (navigator.getUserMedia = navigator.mozGetUserMedia || navigator.webkitGetUserMedia || navigator.msGetUserMedia);

// toString : function() {return "video,audio";} canarya varten
if (navigator.getUserMedia) {
    navigator.getUserMedia({video: true, audio: false, toString : function() {return "video,audio";}}, onSuccess, onError);
} else {
    status.innerText = "getUserMedia is not supported in your browser, sorry :(";
}

function onSuccess(stream) {

    var source;
    if (window.webkitURL) {
        source = window.webkitURL.createObjectURL(stream);
    } else {
        source = stream; // Opera ja Firefox
    }

    video.width = 500;
    video.height = 375;
    video.autoplay = true;
    video.src = source;

}

function onError() {

    status.innerText = "Please allow access to your webcam.";

}

button.addEventListener('mousedown', function() {

    // Poistetaan aikaisempi kuva jos sellaista on
    //document.body.removeChild(imageHolder);

    // luodaan kuva uudestaan
    imageHolder = document.createElement('figure');
    imageHolder.id = 'imageHolder';
    document.body.appendChild(imageHolder);
    img = document.createElement('img');
    imageHolder.appendChild(img); 

    // kuva on yhtäsuuri kuin video
    canvas.width = video.width;
    canvas.height = video.height;
    img.width = 350;
    img.height = 225;

    // piirretään canvasille kuva videosta
    var context = canvas.getContext('2d');
    context.drawImage(video, 0, 0, canvas.width, canvas.height);

}, false);

button.addEventListener('mouseup', function() {      
    // Canvasilta kuvaksi levylle tallentamista varten
    canvas.style.display = 'none';
    video.style.display = 'none';
    button.style.display = 'none';
    others.style.display = 'none';
    document.getElementById('form').style.display = 'block';
    image = canvas.toDataURL('image/png');
    img.src = image;
}, false);

// jquery post
$('#send').click(function(){
    var image2 = image.replace('data:image/png;base64,', '');
    $.post('upload.php',
    {
            title: $('#title').val(),
            blog: $('#blog').val(),
            category: $('#category').val(),
            author: $('#author').val(),
            imagename: image2
    });
});


}, false);

PHP upload.php

define('UPLOAD_DIR', 'images/');
$img = $_POST['imagename'];
$img = str_replace(' ','+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png'; 
$success = file_put_contents($file, $data);
print $success ? $file : 'Tiedoston tallennus ei sitten onnistu millään...';
$imagename = $file; // this is the file name for the MySQL database

我的问题是(我认为)image = canvas.toDataURL('image / png');和jQuery帖子。 canvas.toDataUrl()字符串长约70万个字母。

2 个答案:

答案 0 :(得分:0)

你可能想试试这个:

<?php 
$decoded = ""; 
for ($i=0; $i < ceil(strlen($encoded)/256); $i++) 
   $decoded = $decoded . base64_decode(substr($encoded,$i*256,256)); 
?> 

我是从这里得到的:http://www.php.net/manual/en/function.base64-decode.php#92980

代码基本上尝试部分解码base64字符串。我没有测试过这个。我从未处理过与你正在使用的一样大的base64图像。

答案 1 :(得分:0)

拆分它,使用两个变量并在php中合并,工作正常。 ; - )

var resourcelength_all = resource.length;
var resourcelength_split = resourcelength_all / 2;
var resource_part1 = resource.substr(0, resourcelength_split);
var resource_part2 = resource.substr(resourcelength_split, resourcelength_all);