Sha256: c6b5d51e6fc9a4648f147ea9dc1fc3295b691547eb8dcf683fbf3c1605b85c8a

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

Contents

# encoding: utf-8

require 'rmagick'


module Epuber
  class Compiler
    module FileTypes
      require_relative 'source_file'

      class ImageFile < SourceFile
        # @param [Compiler::CompilationContext] compilation_context
        #
        def process(compilation_context)
          dest = final_destination_path
          source = abs_source_path

          return if self.class.file_uptodate?(source, dest)

          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("downscaling from resolution #{b_img.columns}x#{b_img.rows} to #{width}x#{height}")
              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
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
epuber-0.3.12 lib/epuber/compiler/file_types/image_file.rb
epuber-0.3.11 lib/epuber/compiler/file_types/image_file.rb
epuber-0.3.10 lib/epuber/compiler/file_types/image_file.rb
epuber-0.3.9 lib/epuber/compiler/file_types/image_file.rb
epuber-0.3.7 lib/epuber/compiler/file_types/image_file.rb