Class: Adrift::Processor::Thumbnail

Inherits:
Object
  • Object
show all
Defined in:
lib/adrift/processor.rb

Overview

Creates a set of thumbnails of an image. To be fair, it just tells ImageMagick to do it.

Defined Under Namespace

Classes: Cli

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli = Cli.new) ⇒ Thumbnail

Creates a new Thumbnail object. cli is a wrapper around convert (see Cli).



36
37
38
39
# File 'lib/adrift/processor.rb', line 36

def initialize(cli=Cli.new)
  @processed_files = {}
  @cli = cli
end

Instance Attribute Details

#processed_filesObject (readonly)

Hash with the style names as keys and the paths as values of the files generated in the last #process.



32
33
34
# File 'lib/adrift/processor.rb', line 32

def processed_files
  @processed_files
end

Instance Method Details

#process(image_path, styles = {}) ⇒ Object

Creates a set of thumbnails for image_path with the dimensions specified in styles, which has the following general form:

{ style_name: 'definition', ... }

where ‘definition’ is an ImageMagick’s image geometry or has the form ‘widthxheight#’. For instance:

{
  fixed_width: '100',
  fixed_height: 'x100',
  max: '100x100',
  fixed: '100x100#'
}

will create, respectively, a thumbnail with a 100px width and the corresponding height to preserve the ratio, a thumbnail with a 100px height and the corresponding width to preserve the ratio, a thumbnail with at most 100px width and at most 100px height preserving the ratio, and a thumbnail with 100px width and 100px height preserving the ratio (to do that, it will resize the image trying to make it fit the specified dimensions and then will crop its center).

The thumbnail files are named after image_path prefixed with the style name and a hypen for every style. The last created thumbnails are accesible through #processed_files.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/adrift/processor.rb', line 70

def process(image_path, styles={})
  @processed_files.clear
  styles.each do |name, definition|
    thumbnail_path = File.join(
      File.dirname(image_path),
      "#{name}-#{File.basename(image_path)}"
    )
    @cli.run(image_path, thumbnail_path, options_for(definition))
    @processed_files[name] = thumbnail_path
  end
end