Class: Middleman::Images::Image

Inherits:
Sitemap::Resource
  • Object
show all
Defined in:
lib/middleman-images/image.rb

Constant Summary collapse

IGNORE_RESIZING =
{
  ".svg" => "WARNING: We did not resize %{file}. Resizing SVG files will lead to ImageMagick creating an SVG with an embedded binary image thus making the file way bigger.",
  ".gif" => "WARNING: We did not resize %{file}. Resizing GIF files will remove the animation. If your GIF file is not animated, use JPG or PNG instead.",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, path, source, options = {}) ⇒ Image

Returns a new instance of Image.



11
12
13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



9
10
11
# File 'lib/middleman-images/image.rb', line 9

def app
  @app
end

#original_source_fileObject (readonly)

Returns the value of attribute original_source_file.



9
10
11
# File 'lib/middleman-images/image.rb', line 9

def original_source_file
  @original_source_file
end

Instance Method Details

#binary?Boolean

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.

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/middleman-images/image.rb', line 34

def binary?
  process
  super
end

#processObject



22
23
24
25
26
27
28
29
30
# 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_fileObject

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



45
46
47
# File 'lib/middleman-images/image.rb', line 45

def processed_source_file
  source_file
end

#static_file?Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/middleman-images/image.rb', line 39

def static_file?
  process
  super
end