I have an image on a web page constructed like so:
<img src="data:image/png;base64;...." />
The contents of the image e from the user pasting into the browser. My question is how do I then upload the image to the webserver (PHP if that matters).
I have an image on a web page constructed like so:
<img src="data:image/png;base64;...." />
The contents of the image e from the user pasting into the browser. My question is how do I then upload the image to the webserver (PHP if that matters).
Share Improve this question asked Feb 7, 2012 at 23:41 ChrisChris 27.4k49 gold badges206 silver badges357 bronze badges 4- do you mean pasting in a textarea for example? this sounds quite odd... could you explain the situation a little bit more? – giorgio Commented Feb 7, 2012 at 23:44
- I have an input that, when a user pastes image data into it (i.e. from clipboard - in Chrome only) itll assign the data to an image as a preview before it is uploaded – Chris Commented Feb 7, 2012 at 23:59
- @Chris If you want to see an actual js code show what you already have, not just a small unrelated piece of it. – Cheery Commented Feb 8, 2012 at 0:01
- i totally understand now :) @Cheery has given you a pretty good answer! – giorgio Commented Feb 8, 2012 at 0:01
2 Answers
Reset to default 71) Take the src attribute with javascript (or the data submitted by user)
2) Submit it to the server 'as is' or cut and submit everything after base64;
(AJAX or POST, method GET is probably not very suitable here for large images)
3) Decode base64 on server side (everything after base64;
if not cutted), save the result as binary - it is an image.
That's it.
ps: just a reminder - by careful with possible code injection. Check the submitted data or somebody will upload encoded php script. Disable php engine in the folder with uploads and verify that the final result is an actual image (with the help of GD library, for example). Even if the script can not run on your server it could be used for malicious requests to other servers with php scripts.
Just post the base 64 encoded text to your server.
You could save it as...
file_put_contents($image, base64_decode($str));