This and the next post will demonstrate how to draw on a canvas and then save the file on a server using PHP. Here is the very simple server side code:
The next post will be the slightly more complicated front-end code. This was particularly designed for a Sencha Touch iPad app that will allow people to sign with their finger. It has some flaws, but the basics are pretty reusable and good!
Your result may look something like this:

Merciça m'a bien servi :)
ReplyDeleteWeird, this is not working for me. I get a blank .png file. I am using the jQuery Webcam Plugin for the canvas. When I print out my Base64 encoded data it looks fine to me. It even has that beginning few characters that you strip out.
ReplyDeleteAny ideas?
@murko. all i can say is to check the http://www.xarg.org/project/jquery-webcam-plugin/ docs for their suggestion about how to do it. they're using flash and doing their own things with the canvas. it's possible they use more than one canvas, make sure you're grabbing data from the correct one.
ReplyDeletePlease check out some of these examples: http://www.nihilogic.dk/labs/canvas2image/
you saved my day !
ReplyDeleteactually this one was not working for me:
http://wiki.phonegap.com/w/page/39539478/Upload%20an%20Image%20to%20a%20PHP%20Script
thx
Thanks a lot.
ReplyDeletePlease send me a code to capture the image form webcam and should store that image in database using PHP and Flex 4.5 .Waiting for a good response from u people. Thx in advance.
ReplyDeleteThank you greatly for this code.
ReplyDeleteNice post works for me...
ReplyDeleteCool post, Jamund. I got inspired that I made a whole phonegap plugin. See below. Ciao.
ReplyDeletehttp://bit.ly/Na5iAB
thanks men - this was a great help.
ReplyDeletedanny
Thanks Jamund useful post!!!!!!!!
ReplyDeleteSuperb!!!!!!!
ReplyDeleteThanks
What does the $_POST['img'] string contain?
ReplyDeleteI'm trying to do this exact task but client-side (in an Android app, in case they are offline). I have the Base64 encoded PNG in a JSONArray in my Java code, but haven't figured out how to turn that into an actual .PNG file to save on the user's phone/tablet. Any suggestions?
ReplyDelete@Erin, not exactly what you're looking for, but you might want to check this out: http://thomasjbradley.ca/lab/signature-pad/
ReplyDelete@Einar, it would contain the base64 encoding of the PNG image: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA6IAAAEOC.................and on and on......."
@Jamund! Thanks!
Thank you man, that's incredibly simple and neat!
ReplyDeleteThis just saved me so much time. Thanks for putting this together. The logic you use is easy to understand as well, so +1 for making it readable!
ReplyDeleteThanks, Jamund.
ReplyDeletethis works with jpeg?
ReplyDeleteYes... its work with jpeg. :)
DeleteThank you so much sir, you helped a lot !
ReplyDeleteHowever, this code didn't work the first time I runned it. I had to change the permission on my folder, so that the function file_put_contents could work.
Have a great day