Sha256: 1217692b83f6bfb1035217f5bf62e9de4fbc169607602eb17d1d7711de6d5588

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require 'rmagick'

module Epuber
  class Compiler
    module FileTypes
      require_relative 'source_file'

      class ImageFile < SourceFile
        # @param [Compiler::CompilationContext] _compilation_context
        #
        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("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

          update_metadata!
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
epuber-0.7.4 lib/epuber/compiler/file_types/image_file.rb
epuber-0.7.3 lib/epuber/compiler/file_types/image_file.rb
epuber-0.7.2 lib/epuber/compiler/file_types/image_file.rb
epuber-0.7.1 lib/epuber/compiler/file_types/image_file.rb
epuber-0.7.0 lib/epuber/compiler/file_types/image_file.rb