How to save a PNG image server-side, from a base64 data URI

You need to extract the base64 image data from that string, decode it and then you can save it to disk, you don’t need GD since it already is a png. $data=”data:image/png;base64,AAAFBfj42Pj4″; list($type, $data) = explode(‘;’, $data); list(, $data) = explode(‘,’, $data); $data = base64_decode($data); file_put_contents(‘/tmp/image.png’, $data); And as a one-liner: $data = base64_decode(preg_replace(‘#^data:image/\w+;base64,#i’, ”, … Read more

Convert between UIImage and Base64 string

Swift 5 Encoding func convertImageToBase64String (img: UIImage) -> String { return img.jpegData(compressionQuality: 1)?.base64EncodedString() ?? “” } Decoding func convertBase64StringToImage (imageBase64String:String) -> UIImage { let imageData = Data(base64Encoded: imageBase64String) let image = UIImage(data: imageData!) return image! } Note: Tested in xcode 10.2 Swift 4 Encoding func convertImageToBase64String (img: UIImage) -> String { let imageData:NSData = UIImageJPEGRepresentation(img, … Read more

How do I encode and decode a base64 string?

Encode public static string Base64Encode(string plainText) { var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); return System.Convert.ToBase64String(plainTextBytes); } Decode public static string Base64Decode(string base64EncodedData) { var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData); return System.Text.Encoding.UTF8.GetString(base64EncodedBytes); }

How do I do base64 encoding on iOS?

At the time this question was originally posted, people were understandably directing you to third-party base 64 libraries because of the lack of any native routines. But iOS 7 introduced base 64 encoding routines (which actually simply just exposes private methods iOS had going back to iOS 4). So, you can use the NSData method … Read more

Encoding as Base64 in Java

You need to change the import of your class: import org.apache.commons.codec.binary.Base64; And then change your class to use the Base64 class. Here’s some example code: byte[] encodedBytes = Base64.encodeBase64(“Test”.getBytes()); System.out.println(“encodedBytes ” + new String(encodedBytes)); byte[] decodedBytes = Base64.decodeBase64(encodedBytes); System.out.println(“decodedBytes ” + new String(decodedBytes)); Then read why you shouldn’t use sun.* packages. Update (2016-12-16) You can … Read more

Decode Base64 data in Java

As of Java 8, there is an officially supported API for Base64 encoding and decoding. In time this will probably become the default choice. The API includes the class java.util.Base64 and its nested classes. It supports three different flavors: basic, URL safe, and MIME. Sample code using the “basic” encoding: import java.util.Base64; byte[] bytes = … Read more

Embedding Base64 Images

Update: 2017-01-10 Data URIs are now supported by all major browsers. IE supports embedding images since version 8 as well. http://caniuse.com/#feat=datauri Data URIs are now supported by the following web browsers: Gecko-based, such as Firefox, SeaMonkey, XeroBank, Camino, Fennec and K-Meleon Konqueror, via KDE’s KIO slaves input/output system Opera (including devices such as the Nintendo … Read more

How to display Base64 images in HTML

My suspect is of course the actual Base64 data. Otherwise it looks good to me. See this fiddle where a similar scheme is working. You may try specifying the character set. <div> <p>Taken from wikpedia</p> <img src=”data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO 9TXL0Y4OHwAAAABJRU5ErkJggg==” alt=”Red dot” /> </div> You can try this Base64 decoder to see if your Base64 … Read more