Skip to content
Advertisement

How to save canvas image to server with JavaScript and PHP?

I’m struggling with saving my canvas image to server using JavaScript and PHP. I’ve tried multiple examples for both JS and PHP and it always fails. There’s conflicting advice on how to send image data to PHP script (base64, blob, FormData) and I’m not sure how best to communicate back to JS. Currently, zero bytes PNG files are being saved to server and I’m not sure why. I’d like to save generated canvas as a PNG on server and execute a command in JS on success. How best to approach it?

JS:

JavaScript

PHP:

JavaScript

Advertisement

Answer

After some fiddling with multiple options on both JS and PHP, this is what finally worked:

JS

JavaScript

PHP

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement