Class: HtmlTo::ImageGenerate
- Inherits:
-
Object
- Object
- HtmlTo::ImageGenerate
- Includes:
- Chromium
- Defined in:
- lib/html_to/image_generate.rb
Instance Method Summary collapse
- #attach_image(record, image_name) ⇒ Object
- #call(record, serializer, options) ⇒ Object
- #generate_template(record, serializer, template, width, height) ⇒ Object
- #html_file_path ⇒ Object
- #optimize_screenshot ⇒ Object
- #screenshot_file_path ⇒ Object
- #take_screenshot(width, height) ⇒ Object
Methods included from Chromium
Instance Method Details
#attach_image(record, image_name) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/html_to/image_generate.rb', line 41 def attach_image(record, image_name) record. = true record.send(image_name).purge record.send(image_name).attach(io: optimize_screenshot, filename: optimize_screenshot, content_type: 'image/png') return unless optimize_screenshot optimize_screenshot.close end |
#call(record, serializer, options) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/html_to/image_generate.rb', line 7 def call(record, serializer, ) generate_template(record, serializer, [:template], [:width], [:height]) take_screenshot([:width], [:height]) attach_image(record, [:image_name]) rescue StandardError raise ensure FileUtils.rm_f(html_file_path) FileUtils.rm_f(optimize_screenshot) if optimize_screenshot FileUtils.rm_f(screenshot_file_path) end |
#generate_template(record, serializer, template, width, height) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/html_to/image_generate.rb', line 19 def generate_template(record, serializer, template, width, height) object = serializer.constantize.new(record) html = File.open(template) do |f| ERB.new(f.read).result(binding) end File.write(html_file_path, html) end |
#html_file_path ⇒ Object
50 51 52 |
# File 'lib/html_to/image_generate.rb', line 50 def html_file_path @html_file_path ||= Rails.root.join('tmp').join("#{SecureRandom.urlsafe_base64.downcase}.html") end |
#optimize_screenshot ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/html_to/image_generate.rb', line 58 def optimize_screenshot return nil unless File.exist?(screenshot_file_path) @optimize_screenshot ||= if image_processing_installed? ImageProcessing::MiniMagick .source(screenshot_file_path) .convert('jpg') .saver(quality: 85) .call else File.open(screenshot_file_path) end end |
#screenshot_file_path ⇒ Object
54 55 56 |
# File 'lib/html_to/image_generate.rb', line 54 def screenshot_file_path @screenshot_file_path ||= Rails.root.join('tmp').join("#{SecureRandom.urlsafe_base64.downcase}.png") end |
#take_screenshot(width, height) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/html_to/image_generate.rb', line 27 def take_screenshot(width, height) cmd = " \#{HtmlTo::Chromium.execute_path.shellescape} \\\n --headless \\\n --screenshot=\#{screenshot_file_path} \\\n --window-size=\#{width},\#{height} \\\n --disable-gpu \\\n --disable-features=NetworkService \#{html_file_path}\n BASH\n\n %x(\#{cmd})\n raise StandardError, \"html_to error result = \#{$CHILD_STATUS}; command = \#{cmd}\" unless chromium_run_success?\nend\n".chomp |