class Middleman::Images::Image

Constants

IGNORE_RESIZING

Attributes

app[R]
original_source_file[R]

Public Class Methods

new(store, path, source, options = {}) click to toggle source
Calls superclass method
# File lib/middleman-images/image.rb, line 11
def initialize(store, path, source, options = {})
  @original_source_file = source

  processed_source_file = File.join(store.app.root, options.delete(:cache_dir), path)
  FileUtils.mkdir_p File.dirname(processed_source_file)

  @processing_options = options

  super(store, path, processed_source_file)
end

Public Instance Methods

binary?() click to toggle source

We want to process images as late as possible. Before Middleman works with our source file, it will check whether it is binary? or static_file?. That's when we need to ensure the processed source files exist.

Calls superclass method
# File lib/middleman-images/image.rb, line 34
def binary?
  process
  super
end
process() click to toggle source
# File lib/middleman-images/image.rb, line 22
def process
  return if File.exist?(processed_source_file) && File.mtime(original_source_file) < File.mtime(processed_source_file)

  app.logger.info "== Images: Processing #{@path}"

  FileUtils.copy(original_source_file, processed_source_file)
  resize(processed_source_file, @processing_options[:resize]) unless @processing_options[:resize].nil?
  optimize(processed_source_file, @processing_options[:image_optim]) if @processing_options[:optimize]
end
processed_source_file() click to toggle source

The processed source file is the new source file for middleman.

# File lib/middleman-images/image.rb, line 45
def processed_source_file
  source_file
end
static_file?() click to toggle source
Calls superclass method
# File lib/middleman-images/image.rb, line 39
def static_file?
  process
  super
end

Private Instance Methods

optimize(image_path, options) click to toggle source
# File lib/middleman-images/image.rb, line 71
def optimize(image_path, options)
  begin
    require "image_optim"
  rescue LoadError
    raise "The gem 'image_option' is required for image optimization. Please install the gem 'image_optim' or set the option optimize: false."
  end

  ImageOptim.new(options).optimize_image!(image_path)
end
resize(image_path, options) click to toggle source
# File lib/middleman-images/image.rb, line 51
def resize(image_path, options)
  begin
    require "mini_magick"
  rescue LoadError
    raise 'The gem "mini_magick" is required for image resizing. Please install "mini_magick" or remove the resize option.'
  end

  image_ext = File.extname(image_path)
  if IGNORE_RESIZING.keys.include? image_ext
    app.logger.warn("== Images: " + (IGNORE_RESIZING[image_ext] % { file: image_path }))
    return
  end

  image = MiniMagick::Image.new(image_path) do |i|
    i.resize(options)
    i.define("jpeg:preserve-settings")
  end
  image.write image_path
end