Method: Juicer::ImageEmbed#embed_data_uri

Defined in:
lib/juicer/image_embed.rb

#embed_data_uri(path) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/juicer/image_embed.rb', line 87

def embed_data_uri( path )
  new_path = path

  if @force
    supported_file_matches = path.match( /(?:\.)(png|gif|jpg|jpeg)$/i )
  else
    supported_file_matches = path.match( /(?:\.)(png|gif|jpg|jpeg)(?:\?embed=true)$/i )
  end

  filetype = supported_file_matches[1] if supported_file_matches

  if ( filetype )
    filename = path.gsub('?embed=true','')

    # check if file exists, throw an error if it doesn't exist
    if File.exist?( filename )

      # read contents of file into memory
      content = File.open(filename, "rb") { |f| f.read }
      content_type = "image/#{filetype}"

      # encode the url
      new_path = Datafy::make_data_uri( content, content_type )
    else
      puts "Unable to locate file #{filename} on local file system, skipping image embedding"
    end
  end
  return new_path
end