Class: Epuber::Compiler::FileTypes::ImageFile

Inherits:
SourceFile show all
Defined in:
lib/epuber/compiler/file_types/image_file.rb

Instance Attribute Summary

Attributes inherited from SourceFile

#abs_source_path, #file_request, #source_path

Attributes inherited from AbstractFile

#compilation_context, #destination_path, #final_destination_path, #group, #path_type, #pkg_destination_path, #properties

Instance Method Summary collapse

Methods inherited from SourceFile

#default_file_copy, #destination_file_exist?, #destination_file_up_to_date?, #find_dependencies, #initialize, #properties, resolve_relative_file, #source_file_up_to_date?, #update_metadata!, #write_compiled, #write_processed

Methods inherited from AbstractFile

#==, file_copy!, write_to_file, write_to_file!, write_to_file?

Constructor Details

This class inherits a constructor from Epuber::Compiler::FileTypes::SourceFile

Instance Method Details

#process(_compilation_context) ⇒ Object

Parameters:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/epuber/compiler/file_types/image_file.rb', line 13

def process(_compilation_context)
  return if destination_file_up_to_date?

  dest = final_destination_path
  source = abs_source_path

  img = Magick::Image.read(source).first

  resolution = img.columns * img.rows
  max_resolution = 3_000_000

  if resolution > max_resolution
    img = img.change_geometry("#{max_resolution}@>") do |width, height, b_img|
      UI.print_processing_debug_info(<<~MSG)
        downscaling from resolution #{b_img.columns}x#{b_img.rows} to #{width}x#{height}
      MSG
      b_img.resize!(width, height)
    end

    FileUtils.mkdir_p(File.dirname(dest))

    img.write(dest)
  else
    # file is already old
    self.class.file_copy!(source, dest)
  end

  update_metadata!
end