Class: DocumentToRichHtml::ImageConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/document_to_rich_html/image_converter.rb

Class Method Summary collapse

Class Method Details

.convert(file_path) ⇒ Object



8
9
10
11
# File 'lib/document_to_rich_html/image_converter.rb', line 8

def self.convert(file_path)
  content = extract_content(file_path)
  HtmlFormatter.format(content)
end

.extract_content(file_path) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/document_to_rich_html/image_converter.rb', line 13

def self.extract_content(file_path)
  mime_type = MIME::Types.type_for(file_path).first.content_type
  base64_image = Base64.strict_encode64(File.read(file_path))
  file_name = File.basename(file_path)
  file_size = File.size(file_path)
  
  "<img src='data:#{mime_type};base64,#{base64_image}' alt='Embedded Image' data-trix-attachment='{\"contentType\":\"#{mime_type}\",\"filename\":\"#{file_name}\",\"filesize\":#{file_size},\"height\":auto,\"width\":auto,\"url\":\"data:#{mime_type};base64,#{base64_image}\"}' data-trix-attributes='{\"presentation\":\"gallery\"}'>"
end