Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'm after an osx program that will convert an image into a base64 string (for web development).

Basically replicates this: http://webcodertools.com/imagetobase64converter/Create

I suspect there is no application, and in that case I'd be interested if I can get the terminal command which will encode, to apply to a certain file on Right-Click

Followup question: Can I give Automator apps a window?

SOLUTION

I combined the two solutions below, to create an application with the following code

for f in "$@"
do
    openssl base64 -in "$f" | pbcopy
done

And infact I made a dropzone target using Platypus, as shown in my subsequent SO question/answer

share|improve this question

3 Answers 3

up vote 4 down vote accepted

Base64 Automator Droplet

You can use Automator to create your own Base64 droplet. Mac OS X includes openssl which can encode files to Base64; this superuser question explains how, OS X: Base64 encode via command line

  1. Launch Automator.app
  2. Create a new application
  3. Add a Run Shell Script action
  4. Set Pass input: to as arguments
  5. Within the script, replace echo with the script below.
  6. Save your workflow as an application

To use your application, drag and drop files onto it. A new base64 encoded file will appear next to the original file.

Shell Script

for f in "$@"
do
    openssl base64 -in "$f" -out "$f.b64"
done

Automator Base64 droplet

To learn more about using Automator, see Apple's Mac Basics: Automator.

share|improve this answer

One of your possibilites is :

  • Create an Automator service
  • At the top select : Service receives selected image files in Finder
  • Add a Shell Script action
  • Put the following script in it
    • openssl base64 < "$1" | pbcopy
  • Save the service

Now you can from the contextual menu > Service > yourServiceName of every image in the finder get the base64 encoding copied into your clipboard !

share|improve this answer
    
Thanks. Hm, first try I couldn't get this to work. I'll have another crack when I get some time. But I did combine the use of pbcopy with another answer! –  AshCooman Apr 9 at 16:03

I wrote an Applescript for BBEdit: I only use jpg images for this, but it could be adapted for other images and other editors easily:

-- choose file
set _a to choose file of type "jpg"

-- convert path to posix path
set _b to POSIX path of _a

-- strip newlines and place in clipboard
do shell script "openssl enc -base64 -in " & _b & " | tr -d \"\\n\" | pbcopy"
set myClip to the clipboard

-- set strings with placeholder texts for BBEdit
set _start to "<img alt=\"<# some text #>\" width=\"<# width #>\" height=\"<# height #>\" src=\"data:image/jpg;base64,"
set _end to "\" />"
set _string to _start & myClip & _end

tell application "BBEdit"
    set selection to _string
end tell
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.