Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to base64 encode image in linux bash / shell

People also ask

How do I run base64 in bash?

You can run a base64 encoded string in bash by piping the output of the base64 package for coreutils to bash. For example the command base64 -D <<< bHMgLWwgLwo= | sh would run the command ls -l .

How use base64 in Linux?

You can encode any text data by using base64 in the command line. When you want to encode any data using base64 then using -e or –encode option is optional. So, if you don't mention any option with base64 then it will work for encoding.

How do I use base64 in terminal?

To base64 encode string you can pipe an echo command into the base64 command-line tool. To ensure no extra, hidden characters are added use the -n flag. Without the -n flag you may capture a hidden characters, like line returns or spaces, which will corrupt your base64 encoding.


You need to use cat to get the contents of the file named 'DSC_0251.JPG', rather than the filename itself.

test="$(cat DSC_0251.JPG | base64)"

However, base64 can read from the file itself:

test=$( base64 DSC_0251.JPG )

Encode

On Linux

Single line result:

base64 -w 0 DSC_0251.JPG

For HTML:

echo "data:image/jpeg;base64,$(base64 -w 0 DSC_0251.JPG)"

As file:

base64 -w 0 DSC_0251.JPG > DSC_0251.JPG.base64

In variable:

IMAGE_BASE64="$(base64 -w 0 DSC_0251.JPG)"

In variable for HTML:

IMAGE_BASE64="data:image/jpeg;base64,$(base64 -w 0 DSC_0251.JPG)"

On OSX

On OSX, the base64 binary is different, and the parameters are different. If you want to use it on OSX, you should remove -w 0.

Single line result:

base64 DSC_0251.JPG

For HTML:

echo "data:image/jpeg;base64,$(base64 DSC_0251.JPG)"

As file:

base64 DSC_0251.JPG > DSC_0251.JPG.base64

In variable:

IMAGE_BASE64="$(base64 DSC_0251.JPG)"

In variable for HTML:

IMAGE_BASE64="data:image/jpeg;base64,$(base64 DSC_0251.JPG)"

Generic OSX/Linux

As Shell Function

@base64() {
  if [[ "${OSTYPE}" = darwin* ]]; then
    # OSX
    if [ -t 0 ]; then
      base64 "$@"
    else
      cat /dev/stdin | base64 "$@"
    fi
  else
    # Linux
    if [ -t 0 ]; then
      base64 -w 0 "$@"
    else
      cat /dev/stdin | base64 -w 0 "$@"
    fi
  fi
}

# Usage
@base64 DSC_0251.JPG
cat DSC_0251.JPG | @base64

As Shell Script

Create base64.sh file with following content:

#!/usr/bin/env bash
if [[ "${OSTYPE}" = darwin* ]]; then
  # OSX
  if [ -t 0 ]; then
    base64 "$@"
  else
    cat /dev/stdin | base64 "$@"
  fi
else
  # Linux
  if [ -t 0 ]; then
    base64 -w 0 "$@"
  else
    cat /dev/stdin | base64 -w 0 "$@"
  fi
fi

Make it executable:

chmod a+x base64.sh

Usage:

./base64.sh DSC_0251.JPG
cat DSC_0251.JPG | ./base64.sh

Decode

Get you readable data back:

base64 -d DSC_0251.base64 > DSC_0251.JPG 

There is a Linux command for that: base64

base64 DSC_0251.JPG >DSC_0251.b64

To assign result to variable use

test=`base64 DSC_0251.JPG`

If you need input from termial, try this

lc=`echo -n "xxx_${yyy}_iOS" |  base64`

-n option will not input "\n" character to base64 command.


Base 64 for html:

file="DSC_0251.JPG"
type=$(identify -format "%m" "$file" | tr '[A-Z]' '[a-z]')
echo "data:image/$type;base64,$(base64 -w 0 "$file")"

To base64 it and put it in your clipboard:

file="test.docx"
base64 -w 0 $file  | xclip -selection clipboard