Class: MMThumb::Converter

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

Overview

See README for practical examples.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Converter

New converter

For options see setup. The options you pass here will be preserved as defaults.

Parameters:

  • config (Hash) (defaults to: {})

    Configuration options

See Also:



29
30
31
32
# File 'lib/mmthumb.rb', line 29

def initialize(config = {})
  @default = config
  setup
end

Instance Attribute Details

#configHash

Instance configuration

Returns:

  • (Hash)

    the current value of config



19
20
21
# File 'lib/mmthumb.rb', line 19

def config
  @config
end

Instance Method Details

#add_output(key, opts = {}, &block) ⇒ Object

Add output

Parameters:

  • key (Symbol, String)

    Unique key for the output

  • opts (Hash) (defaults to: {})

    Configuration options

  • block (Block)

    Code for the output

See Also:



81
82
83
# File 'lib/mmthumb.rb', line 81

def add_output(key, opts = {}, &block)
  @outputs[key] = [opts, block]
end

#convert(path, opts = {}) ⇒ Hash<Symbol, Hash>

Convert image to given outputs

Configuration options are chained and calculated on each convert with the following hierarchy: instance options - output options - convert call options (this list is lowest-to-highest priority order). Thus you can easily override any option.

Will raise Error if given file is unreadable, it won't raise exceptions on any other errors. Instead error information will be returned with the result.

The returned hash keys correspond to the output keys, and each value will be another hash; which will contain either :done => true and :path => String when successful, or :done => false and :error => Exception if not.

Parameters:

  • path (String)

    Path to file to convert

  • opts (Hash) (defaults to: {})

    Configuration options

Returns:

  • (Hash<Symbol, Hash>)

    Results

Raises:

See Also:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/mmthumb.rb', line 115

def convert(path, opts = {})
  return nil if @outputs.empty?

  input = File.absolute_path(path)
  raise Error, 'File is unreadable' unless File.readable? input

  res = Hash.new
  @outputs.each_pair do |key, (cfg, blk)|
    config = @config.merge(cfg).merge(opts)
    config[:path]     ||= File.dirname(input)
    config[:basename] ||= File.basename(path, File.extname(path))
    config[:suffix]   ||= '_' + key.to_s
    output = output_path(config)

    begin
      img = MiniMagick::Image.open(input)
      @before.call(img, config) if @before

      img.format(config[:format])
      blk.call(img, config) if blk

      @after.call(img, config) if @after

      img.quality(config[:quality])
      img.write(output)
    rescue StandardError => e
      res[key] = {
        :done => false,
        :error => e,
      }
    else
      res[key] = {
        :done => true,
        :path => output,
      }
    end
  end

  res
end

#del_output(key) ⇒ Array?

Remove output

Returns:

  • (Array, nil)


88
# File 'lib/mmthumb.rb', line 88

def del_output(key); @outputs.delete(key); end

#del_postprocess!nil

Remove postprocess block

Returns:

  • (nil)


68
# File 'lib/mmthumb.rb', line 68

def del_postprocess!; @after = nil; end

#del_preprocess!nil

Remove preprocess block

Returns:

  • (nil)


51
# File 'lib/mmthumb.rb', line 51

def del_preprocess!; @before = nil; end

#output_path(opts) ⇒ String (private)

Calculate output path

Parameters:

  • opts (Hash)

    Configuration options

Returns:

  • (String)

    Full output file path

See Also:



197
198
199
200
201
202
# File 'lib/mmthumb.rb', line 197

def output_path(opts)
  File.join(opts[:path], opts[:prefix] +\
            opts[:basename] +\
            opts[:suffix] + '.' +\
            opts[:format])
end

#outputsHash<Symbol, Array>

Get outputs

Returns:

  • (Hash<Symbol, Array>)


93
# File 'lib/mmthumb.rb', line 93

def outputs; @outputs; end

#postprocess(&blk) ⇒ Block

Add postprocess block

The block will be executed after outputs.

Returns:

  • (Block)

    Given block



63
# File 'lib/mmthumb.rb', line 63

def postprocess(&blk); @after = blk; end

#postprocess?Boolean

Check if postprocess block is present

Returns:

  • (Boolean)


73
# File 'lib/mmthumb.rb', line 73

def postprocess?; @after ? true : false; end

#preprocess(&blk) ⇒ Block

Add preprocess block

The block will be executed before outputs.

Returns:

  • (Block)

    Given block



46
# File 'lib/mmthumb.rb', line 46

def preprocess(&blk); @before = blk; end

#preprocess?Boolean

Check if preprocess block is present

Returns:

  • (Boolean)


56
# File 'lib/mmthumb.rb', line 56

def preprocess?; @before ? true : false; end

#reset!Hash

Reset instance config to defaults

Returns:

  • (Hash)

    Configuration options

See Also:



39
# File 'lib/mmthumb.rb', line 39

def reset!; setup; end

#setupHash (private)

Setup instance

You can keep any options you want inside the @config hash, but these keys are used internally for:

  • :format - output format as an extension without a dot (e.g. 'jpg')
  • :quality - string indicating JPEG quality (e.g. '80')
  • :path - string indicating target directory
  • :basename - basename of the output file
  • :prefix - prefix to add to the output filename
  • :suffix - suffix to add to the output filename

If :path => nil then outputs will be saved in the directory where the source file is located.

If :basename => nil then the source file basename will be used.

If :suffix => nil then the :key of the output will be added (after an _) to the output filename.

Returns:

  • (Hash)

    Configuration options



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/mmthumb.rb', line 179

def setup
  @before = nil
  @outputs = Hash.new
  @after = nil

  @config = {
    :format => FORMAT,
    :quality => QUALITY,
    :path => nil,
    :prefix => '',
  }.merge!(@default)
end