0

Hi I need to get the form data to be submitted along with the canvas image. Then use that form data to name the file saving on the server. I am just getting to grips with PHP, so any help would be amazing.

This is my working example which will save the same named file to the server.

HTML

 <form action="name.php" method="post">
        Name: <input type="text" name="fname">
        Age: <input type="text" name="age">
        <input type="button" onclick="saveImage();formSubmit();" value="Submit form">
 </form>

JS

function saveImage(){
var xmlhttp;
xmlhttp=((window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP"));
xmlhttp.onreadystatechange=function()
{
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        //do something with the response
    }
}
xmlhttp.open("POST","upload.php",true);
var oldCanvas = document.getElementById('colors_sketch').toDataURL("image/png");
var img = new Image();
img.src = oldCanvas;
xmlhttp.setRequestHeader("Content-type", "application/upload")
xmlhttp.send(oldCanvas);

}

PHP

$data = file_get_contents('php://input');

$canvasImg = imagecreatefrompng($data);
$width  = imagesx($canvasImg);
$height = imagesy($canvasImg);

$outImg = imagecreatetruecolor($width, $height);
$color = imagecolorallocatealpha($outImg, 255, 255, 255, 127);
imagefill($outImg, 0, 0, $color);
imagecopy($outImg, $canvasImg, 0, 0, 0, 0, $width, $height);

imagepng($outImg, 'test.png');
databot
  • 131
  • 3
  • 15

2 Answers2

2

Here are steps to send your form and image to upload.php

First create a data package to send that includes your serialized image and form. Here I use jquery to serialize your form (or serialize it yourself without jquery).

// serialize and concatenate both form and canvas image
var package=$("form").serialize() + "&imageDataURL="+canvas.toDataURL();

Then POST it with AJAX to your PHP server:

// ajax it
postImagePlusForm();

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

And finally catch it and process it in php:

<?php 
if ( isset($_POST["imageDataURL"]) && !empty($_POST["imageDataURL"]) ) {    
    // create $dataURL
    $dataURL = $_POST["imageDataURL"];  
    // Extract base64 data
    // we have an unneeded header, zap it
    $parts = explode(',', $dataURL);  
    $data = $parts[1];  
    // Decode
    $data = base64_decode($data);  
    // Save
    $fp = fopen('newImage.png', 'w');  
    fwrite($fp, $data);  
    fclose($fp); 
}
if ( isset($_POST["formData"]) && !empty($_POST["formData"]) ) {    
    $formData = $_POST['formData'];
    foreach ($formValue as $x) {
        // do whatever you need to do with each form value ($x)
    }
}
markE
  • 102,905
  • 11
  • 164
  • 176
  • Ok, try this variation which just concatenates both the form and image data into 1 string: var package=$("form").serialize() + "&imageDataURL="+canvas.toDataURL(); – markE Apr 24 '13 at 16:44
0

This question was asked numerous times on SO.

I already answered the same question here: https://stackoverflow.com/a/13198699/1090562

In short, this can be achieved with this, for more details - read the link

$.ajax({
  type: "POST",
  url: "script.php",
  data: { 
     imgBase64: canvas.toDataURL();
  }
}).done(function(o) {
  console.log('saved');
});
Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753