Base64 encoding of image files (using Terminal in OS X)

A recent protip on Coderwall inspired me to look further into Base64 encoding, and thanks to this post on StackExchange I learnt all I needed to know to create Base64 files in the OS X terminal app:

Step 1

Open Terminal and type:

openssl

and then hit enter

Step 2

At the OpenSSL prompt type: 

base64 -in /Users/your-username/Desktop/imagetobeconverted.png -out /Users/your-username/Desktop/convertedimage.b64

Note: The file path can be inserted by simply dragging the file to the Terminal app

Step 3

To test the output file in a browser simply open the file in a text editor, append to the beginning

data:image/png;base64,

and copy and paste the text into the URL bar of your browser. You can convert other image files as well, for example TIFF and JPEG, but you will need to change png to the appropriate type, i.e. tif or jpg, and you may well find that instead of loading the image directly into the browser that placing the data in the URL bar will mean that the image is created and downloaded as a regular image file.    

Comments

  1. I'm like 7 years late but thank you!!!!! its cooler to do it on your own and learn than go elsewhere thanks a lot!

    ReplyDelete

Post a Comment