Module: Docbook::Services::ImageUtils

Defined in:
lib/docbook/services/image_utils.rb

Class Method Summary collapse

Class Method Details

.embed_as_data_url(path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/docbook/services/image_utils.rb', line 16

def self.embed_as_data_url(path)
  return nil unless path && File.exist?(path)

  mime = mime_type(path)
  return path unless mime

  data = File.binread(path)
  "data:#{mime};base64,#{Base64.strict_encode64(data)}"
rescue StandardError
  path
end

.mime_type(path) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/docbook/services/image_utils.rb', line 6

def self.mime_type(path)
  case File.extname(path).downcase
  when ".png"  then "image/png"
  when ".jpg", ".jpeg" then "image/jpeg"
  when ".gif"  then "image/gif"
  when ".svg"  then "image/svg+xml"
  when ".webp" then "image/webp"
  end
end