So you’ve finally discovered the wonder that is the HTML5 Canvas element. Great! If you’re like me, the first thing I wanted to do with it was doodle on it. I eventually worked out how to map touch/mouse events to the canvas and draw lines, but I wanted to save my creations!
As it turns out, the Canvas element has a method called toDataURL(), which base64 encodes the entire Canvas element and returns it as a string. From there, you can just pump it over to a server and handle it from there. Here’s the step-by-step, which assumes you are also running jQuery on your site.
Step 1: Save the canvas and POST the data
var data = document.getElementById("myCanvasID").toDataURL(); $.post("process.php", { imageData : data }, function(data) { window.location = data; }); |
Step 2: Process the POST data, and save it to a file.
$data = substr($_POST['imageData'], strpos($_POST['imageData'], ",") + 1); $decodedData = base64_decode($data); $fp = fopen("canvas.png", 'wb'); fwrite($fp, $decodedData); fclose(); echo "/canvas.png"; |
Note: The first line of this script removes the header information that is sent with the encoded data.
Thats all there is to it. You can now easily save your HTML 5 awesomeness.
15 replies on “HTML 5 Canvas: Saving to a File with PHP”
That’s sweet! Very very good post – thanks guys!
I came across your blog by chance in google, love the stuff on threading. Some nice topics here.
fwrite and fclose are functions so leave the initial $ and everything works fine …
Thanks for noticing the typo. I updated the post to fix the issue.
Is this outdated? Because searching for the position of ” ,” doesn’t work for me because of the space before the comma.
My data URLs look like “data:image/jpeg;base64,/9j/4AAQSkZJRg …”
And they look like that for both FF and Chrome. So changing the code to look for “,” fixed it.
Yes, this is probably a bit outdated now. I updated the post to reflect the change. Thanks for stopping by with the correction.
Hi, thanks for the great post. I used images instead of drawing things and when I try to save the canvas, it only saves the empty canvas and doesn’t include the images! Do you have any solution for that? Thanks in advance!
its probably because you didn’t use, image onload function, i.e wait until the javascript loads the image before you draw it.
Just learning HTML5 and this topic is helpful. Thank you for sharing.
I m getting this error while saving the image… please help
SecurityError: The operation is insecure.
[Break On This Error]
var data = document.getElementById(“canvasID”).toDataURL();
You might be triggering a Same Origin policy error. Check out this. While that may not be your exact problem, it might give you something go on. Hope this helps!
But this .toDataURL(); will save the canvas data as an image and not a raw code. That means user can’t redraw the canvas in future. Is there any way where I can save the canvas data in raw mode, so that in future I can again re-edit the canvas?
It does not work for me unfortunately. The images are stored but can not be shown again. The following error message (image can not be displayed, because it contains errors.)
[…] 13 Replies […]
[…] 13 Replies […]