0

我打算将画布图像与表单信息一起发送到服务器。然后,这将保存一个以表单中的信息命名的 PNG。

目前,写入的 PNG 无法打开,只是一个 0 kb 的杂乱文件。

完整的网站在这里



HTML - 提交时写入信息并激活 javascript 的表单

<form action="PHPtestupload.php" method="post">
  Name <input type="text" name="name" value="" style="width:230px;  margin-left: -160px; font-size:1em; "/>
  Email <input type="email" name="email" value="" style="width:230px;  margin-top: 12px; margin-left: -160px; font-size:1em; "/>

  <input type="submit" onclick="postImagePlusForm();" value="Submit" style=" opacity:1; -webkit-appearance: none; width:100px; height:50px; margin-left: -50px;">
</form>



JS

    // sends image to server
    // serialize your canvas
    var dataURL=document.getElementById('colors_sketch').toDataURL('image/png');

    // serialize your form
    var str = $("form").serializeArray();

    // wrap both in an object
    var package={ formData: str, imageDataURL: dataURL }




    // ajax it 
    postImagePlusForm();

    function postImagePlusForm(){
    $.ajax({  
     type: "POST",  
     url: "PHPtestupload.php",  
     data: package,  
     success: function(value) {  
        // ...we have success!
    }
   });
  }



PHP

    $dataURL = $_POST["imageDataURL"];
    $dataURL = str_replace('data:image/png;base64,', '', $dataURL);
    $dataURL = str_replace(' ', '+', $dataURL);
    $image = base64_decode($dataURL);
    $filename = 'newImage.png';
    file_put_contents('image/' . $filename, $image);

    $name = $_POST["formData"];
    $target = 'newImage.png';
    rename($target, $newName);
4

1 回答 1

0

这对我有用,图像字符串取自您的网站

<?php
header("Content-type: image/png");
echo base64_decode("iVBORw0KGgoAAAANSUhEUgAAAlgAAAJECAYAAADKclo8AAAFXElEQVR4nO3BMQEAAADCoPVPbQ0PoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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