(note: while jQuery isn't needed for this small example, it is used because I will be using it in my project)
canvas HTML5 file:
Code: Select all
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Make and send PNG</title>
<script type="text/javascript" src="jquery141.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.fillStyle = '#ffffaa';
context.fillRect(0, 0, 500, 400);
context.fillStyle = '#ff0000';
context.font = '14px _sans';
context.fillText('Woo hoo! Man...', 220, 200);
$('#myButton').click(function() {
var img = canvas.toDataURL("image/png");
$.post('makefile.php', {data: img}, function(evt) {
alert("File was sent");
});
});
});
</script>
</head>
<body>
<canvas id="myCanvas" width="500" height="400">
No canvas support. Get a new freakin' browser.
</canvas>
<form>
<input type="button" id="myButton" value="Save" />
</form>
</body>
</html>Code: Select all
<?php
$imgFile = $_POST['data'];
$codedImg = base64_decode(substr($imgFile, strpos($imgFile, ",")+1));
//not using file_put_contents() for PHP4 compatiblity
$fp = fopen("images/backimage.png", 'w');
fwrite($fp, $codedImg);
fcloser($fp);
?>