Ich habe ein Problem mit dem Speichern Canvas Bild in PHP. Ich bekomme eine leere .png
Datei. Ich suche viel nach diesem Problem, kann aber nichts Nützliches finden. Warum speichert es ein leeres Bild anstelle eines echten Bildes?beim Speichern Canvas-Bild Server-Seite, aus einer Base64-Daten-String, es leere Bild erzeugen
JavaScript-Code:
html2canvas([document.getElementById('dadycool')], {
onrendered: function (canvas) {
var data = canvas.toDataURL();
var image = new Image();
image.src = data;
document.getElementById('imagec').appendChild(image);
console.log(data);
$.ajax({
type: "POST",
url: "up.php",
data: {
imgBase64: data
}
}).done(function(o) {
console.log('saved');
});
}
PHP-Code:
<?php
// requires php5
define('localhost/samp/sample2/uploads', 'images/');
$img = $_POST['imgBase64'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = localhost/samp/sample2/uploads . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
standardmäßig so weit ich als Know, Retouren toDataURL Bild png, ' $ img = str_replace ('daten: image/jpg; base64,', '', $ img); sollte '$ img = str_replace sein ('daten: image/png; base64,', '', $ img); ' –
ja richtig !! Ich habe vergessen, die Update-Version meines Codes zu schreiben: D aber mein Problem existiert noch. – mkafiyan
Sie können den PHP-Teil durch [PHP-FileUpload] (https://github.com/delight-im/PHP-FileUpload) und dessen ['DataUriUpload'] ersetzen (https://github.com/delight-im/ PHP-DateiUpload/blob/d0065c47dcda18c4965ed900bb15190f7af30e79/src/DataUriUpload.php) Komponente, wie [hier dokumentiert] (https://github.com/delight-im/PHP-FileUpload/tree/d0065c47dcda18c4965ed900bb15190f7af30e79#data-uri-uploads). Es kümmert sich auch um andere Dinge, wie beispielsweise Dateigrößenbeschränkungen und mehrere akzeptierte MIME-Typen. – caw