Skip to content

Instantly share code, notes, and snippets.

@jdugan
Created October 5, 2017 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdugan/253ca045584764fd1173491c651d17a6 to your computer and use it in GitHub Desktop.
Save jdugan/253ca045584764fd1173491c651d17a6 to your computer and use it in GitHub Desktop.
Converting image data to a JSON-compliant string

Simple images can be POSTed to JSON API endpoints by converting the binary data to a Base64 encoded string.

The functions required to do should be available in all popular programming languages.

An example in Ruby is provided below.

Conversion Class

The folowing ruby class has two public functions. One to encode an image file as a base64 string; another to decode a base64 string into an image file.

require 'base64'

class Converter

  #--------------------------------------------------------
  # Class Methods
  #--------------------------------------------------------

  class << self

    # Ruby expressions are evaluated from the inside out, so
    # here the following actions are being executed:
    #
    # 1. The `File` class (which extends `IO`) is used to read
    #    the file object at `path` into a temporary file
    #    structure in memory.  The `rb` option means we are
    #    opening the file to _r_ead in _b_inary mode.
    # 2. We `read` the temporary file to retrieve the binary
    #    data as a string.
    # 3. We use the `Base64` module to convert the binary
    #    string to a base64 string.
    #
    def encode(path)
      Base64.encode64( File.open(path, 'rb').read )
    end

    # Ruby expressions are evaluated from the inside out, so
    # here the following actions are being executed:
    #
    # 1. We use the `Base64` module to convert the base64 string
    #    back to the string representation of the binary data.
    # 2. We write the binary data to a temporary file structure
    #    created by `File.open`.  The `wb` option means we are
    #    opening the file to _w_rite in _b_inary mode.
    # 3. The temporary file is used to create a physical file
    #    on the hard drive at the location we've specified.
    #
    def decode(str)
      File.open('output.jpeg', 'wb') do |f|
        f.write( Base64.decode64(str) )
      end
    end

  end

end

Further information on the classes and modules used in this example can be found at the following locations:

Practical Example

The following example is run from a OSX directory containing the Converter class above in a file named converter.rb and a JPEG image named input.jpeg.

# Start an interactive ruby session and load the converter file into memory.
$ irb -r ./converter.rb

# Determine the full path to your image file
> path = File.expand_path('input.jpeg')
 => "/absolute/path/to/your/directory/input.jpeg"

# Encode the image file
> str = Converter.encode(path)
 => "[very long base64 string...]"
 
# Decode the base64 string. Check file system for result.
> Converter.decode(str)
 => "[Length of new file]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment