How to crop and upload photo using cropit jquery plugin with php
1. Saving the base64 encoded image
<?php //save your data into a variable - last part is the base64 encoded image $encoded = "data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhE"; //decode the url, because we want to use decoded characters to use explode $decoded = urldecode($encoded); //explode at ',' - the last part should be the encoded image now $exp = explode(',', $decoded); //we just get the last element with array_pop $base64 = array_pop($exp); //decode the image and finally save it $data = base64_decode($base64); $file = 'data.png'; //make sure you are the owner and have the rights to write content file_put_contents($file, $data);
2. Getting the filename of base64 encoded image
$encoded = "data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhE"; $decoded = urldecode($encoded); $exp = explode(';', $decoded); $exp = explode(':', $exp[0]); $image = array_pop($exp); echo ($image);
I got Hosch Nok's answer to work by not decoding the encoded data.Not calling:
$decoded = urldecode($encoded);
But working directly with the $encoded
variable.