I'm converting an image to base64 string and sending it from android device to the server. Now, I need to change that string back to an image and save it in the database.
Any help?
Using following code to convert it into an image file: function base64_to_jpeg( $base64_string, $output_file ) { $ifp = fopen( $output_file, "wb" ); fwrite( $ifp, base64_decode( $base64_string) ); fclose( $ifp ); return( $output_file ); } $image = base64_to_jpeg( $my_base64_string, 'tmp. jpg' );
The equals sign "=" represents a padding, usually seen at the end of a Base64 encoded sequence. The size in bytes is divisible by three (bits divisible by 24): All bits are encoded normally.
Cg== is the base64 encode of the new line character in the latest position. So if you want to encode ABC you will get QUJD , however if you include a "return character" after ABC you will get QUJDCg== .
With elmah. io's free image to Base64 encoder, it's easy to copy and paste markup or style for exactly your codebase. Simply drag and drop, upload, or provide an image URL in the controls above and the encoder will quickly generate a Base64 encoded version of that image.
Try this:
import base64 imgdata = base64.b64decode(imgstring) filename = 'some_image.jpg' # I assume you have a way of picking unique filenames with open(filename, 'wb') as f: f.write(imgdata) # f gets closed when you exit the with statement # Now save the value of filename to your database
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With