Class: MMThumb::Converter
- Inherits:
-
Object
- Object
- MMThumb::Converter
- Defined in:
- lib/mmthumb.rb
Overview
See README for practical examples.
Instance Attribute Summary collapse
-
#config ⇒ Hash
Instance configuration.
Instance Method Summary collapse
-
#add_output(key, opts = {}, &block) ⇒ Object
Add output.
-
#convert(path, opts = {}) ⇒ Hash<Symbol, Hash>
Convert image to given outputs.
-
#del_output(key) ⇒ Array?
Remove output.
-
#del_postprocess! ⇒ nil
Remove postprocess block.
-
#del_preprocess! ⇒ nil
Remove preprocess block.
-
#initialize(config = {}) ⇒ Converter
constructor
New converter.
-
#output_path(opts) ⇒ String
private
Calculate output path.
-
#outputs ⇒ Hash<Symbol, Array>
Get outputs.
-
#postprocess(&blk) ⇒ Block
Add postprocess block.
-
#postprocess? ⇒ Boolean
Check if postprocess block is present.
-
#preprocess(&blk) ⇒ Block
Add preprocess block.
-
#preprocess? ⇒ Boolean
Check if preprocess block is present.
-
#reset! ⇒ Hash
Reset instance config to defaults.
-
#setup ⇒ Hash
private
Setup instance.
Constructor Details
#initialize(config = {}) ⇒ Converter
New converter
For options see setup. The options you pass here will be
preserved as defaults.
29 30 31 32 |
# File 'lib/mmthumb.rb', line 29 def initialize(config = {}) @default = config setup end |
Instance Attribute Details
#config ⇒ Hash
Instance configuration
19 20 21 |
# File 'lib/mmthumb.rb', line 19 def config @config end |
Instance Method Details
#add_output(key, opts = {}, &block) ⇒ Object
Add output
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.
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
88 |
# File 'lib/mmthumb.rb', line 88 def del_output(key); @outputs.delete(key); end |
#del_postprocess! ⇒ nil
Remove postprocess block
68 |
# File 'lib/mmthumb.rb', line 68 def del_postprocess!; @after = nil; end |
#del_preprocess! ⇒ nil
Remove preprocess block
51 |
# File 'lib/mmthumb.rb', line 51 def del_preprocess!; @before = nil; end |
#output_path(opts) ⇒ String (private)
Calculate output path
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 |
#outputs ⇒ Hash<Symbol, Array>
Get outputs
93 |
# File 'lib/mmthumb.rb', line 93 def outputs; @outputs; end |
#postprocess(&blk) ⇒ Block
Add postprocess block
The block will be executed after outputs.
63 |
# File 'lib/mmthumb.rb', line 63 def postprocess(&blk); @after = blk; end |
#postprocess? ⇒ Boolean
Check if postprocess block is present
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.
46 |
# File 'lib/mmthumb.rb', line 46 def preprocess(&blk); @before = blk; end |
#preprocess? ⇒ Boolean
Check if preprocess block is present
56 |
# File 'lib/mmthumb.rb', line 56 def preprocess?; @before ? true : false; end |
#reset! ⇒ Hash
Reset instance config to defaults
39 |
# File 'lib/mmthumb.rb', line 39 def reset!; setup; end |
#setup ⇒ Hash (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.
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 |