使用canvas.todataurl()使用jQuery后尺寸限制



我正在尝试使用jQuery Post和PHP上传Canvas.todataurl()image(getUsermedia)到服务器,以处理数据,但我遇到了一些问题。我上传的所有图像最终都会损坏,一半的图像丢失了。我还有一个MySQL数据库,其中我要存储与图像有关的数据(标题,文本,日期等)。看来我拥有相关数据的越多,图像损坏就越多。

因此,我想知道这是浏览器的限制,或者与jQuery帖子有关。我还检查了php max_postrongize,它是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()字符串约为700 000个字母。

您可能想尝试一下:

<?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/en/function.base64-decode.php#92980

代码基本上试图部分解码base64字符串。我还没有测试过。我从来没有像您正在使用的base64图像一样大。

将其拆分,使用两个,变量并在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);

最新更新