Class: ImageOptim
- Inherits:
-
Object
- Object
- ImageOptim
- Defined in:
- lib/image_optim.rb,
lib/image_optim/config.rb,
lib/image_optim/runner.rb,
lib/image_optim/worker.rb,
lib/image_optim/handler.rb,
lib/image_optim/railtie.rb,
lib/image_optim/image_path.rb,
lib/image_optim/worker/svgo.rb,
lib/image_optim/bin_resolver.rb,
lib/image_optim/hash_helpers.rb,
lib/image_optim/worker/jhead.rb,
lib/image_optim/worker/advpng.rb,
lib/image_optim/worker/pngout.rb,
lib/image_optim/option_helpers.rb,
lib/image_optim/true_false_nil.rb,
lib/image_optim/worker/optipng.rb,
lib/image_optim/worker/gifsicle.rb,
lib/image_optim/worker/jpegtran.rb,
lib/image_optim/worker/pngcrush.rb,
lib/image_optim/worker/jpegoptim.rb,
lib/image_optim/configuration_error.rb
Defined Under Namespace
Modules: HashHelpers, OptionHelpers Classes: BadBinVersion, BinNotFoundError, BinResolver, Config, ConfigurationError, Handler, ImagePath, Railtie, Runner, TrueFalseNil, Worker
Instance Attribute Summary collapse
-
#nice ⇒ Object
readonly
Nice level.
-
#threads ⇒ Object
readonly
Number of threads to run with.
Class Method Summary collapse
-
.method_missing(method, *args, &block) ⇒ Object
Optimization methods with default options.
-
.version ⇒ Object
Version of image_optim gem spec loaded.
Instance Method Summary collapse
-
#env_path ⇒ Object
Join resolve_dir, default path and vendor path for PATH environment variable.
-
#initialize(options = {}) ⇒ ImageOptim
constructor
Initialize workers, specify options using worker underscored name:.
-
#optimizable?(path) ⇒ Boolean
Are there workers for file at path?.
-
#optimize_image(original) ⇒ Object
Optimize one file, return new path as OptimizedImagePath or nil if optimization failed.
-
#optimize_image!(original) ⇒ Object
Optimize one file in place, return original as OptimizedImagePath or nil if optimization failed.
-
#optimize_image_data(original_data) ⇒ Object
Optimize image data, return new data or nil if optimization failed.
-
#optimize_images(paths, &block) ⇒ Object
Optimize multiple images if block given yields path and result for each image and returns array of yield results else return array of results.
-
#optimize_images!(paths, &block) ⇒ Object
Optimize multiple images in place if block given yields path and result for each image and returns array of yield results else return array of results.
-
#optimize_images_data(datas, &block) ⇒ Object
Optimize multiple image datas if block given yields original and result for each image data and returns array of yield results else return array of results.
-
#resolve_bin!(bin) ⇒ Object
Check existance of binary, create symlink if ENV contains path for key XXX_BIN where XXX is upper case bin name.
-
#verbose? ⇒ Boolean
Verbose output?.
-
#workers_for_image(path) ⇒ Object
Get workers for image.
Constructor Details
#initialize(options = {}) ⇒ ImageOptim
Initialize workers, specify options using worker underscored name:
pass false to disable worker
ImageOptim.new(:pngcrush => false)
or hash with options to worker
ImageOptim.new(:advpng => {:level => 3}, :optipng => {:level => 2})
use :threads to set number of parallel optimizers to run (passing true or nil determines number of processors, false disables parallel processing)
ImageOptim.new(:threads => 8)
use :nice to specify optimizers nice level (true or nil makes it 10, false makes it 0)
ImageOptim.new(:nice => 20)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/image_optim.rb', line 38 def initialize( = {}) @bin_resolver = BinResolver.new config = Config.new() @nice = config.nice @threads = config.threads @verbose = config.verbose @workers_by_format = {} Worker.klasses.each do |klass| if = config.for_worker(klass) worker = klass.new(self, ) worker.image_formats.each do |format| @workers_by_format[format] ||= [] @workers_by_format[format] << worker end end end @workers_by_format.values.each(&:sort!) config. puts config if verbose? end |
Instance Attribute Details
#nice ⇒ Object (readonly)
Nice level
11 12 13 |
# File 'lib/image_optim.rb', line 11 def nice @nice end |
#threads ⇒ Object (readonly)
Number of threads to run with
14 15 16 |
# File 'lib/image_optim.rb', line 14 def threads @threads end |
Class Method Details
.method_missing(method, *args, &block) ⇒ Object
Optimization methods with default options
130 131 132 133 134 135 136 |
# File 'lib/image_optim.rb', line 130 def self.method_missing(method, *args, &block) if method_defined?(method) && method.to_s =~ /^optimize_image/ new.send(method, *args, &block) else super end end |
.version ⇒ Object
Version of image_optim gem spec loaded
139 140 141 |
# File 'lib/image_optim.rb', line 139 def self.version Gem.loaded_specs['image_optim'].version.to_s rescue 'DEV' end |
Instance Method Details
#env_path ⇒ Object
Join resolve_dir, default path and vendor path for PATH environment variable
154 155 156 |
# File 'lib/image_optim.rb', line 154 def env_path @bin_resolver.env_path end |
#optimizable?(path) ⇒ Boolean
Are there workers for file at path?
144 145 146 |
# File 'lib/image_optim.rb', line 144 def optimizable?(path) !!workers_for_image(path) end |
#optimize_image(original) ⇒ Object
Optimize one file, return new path as OptimizedImagePath or nil if optimization failed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/image_optim.rb', line 69 def optimize_image(original) original = ImagePath.convert(original) if workers = workers_for_image(original) handler = Handler.new(original) workers.each do |worker| handler.process do |src, dst| worker.optimize(src, dst) end end handler.cleanup if handler.result ImagePath::Optimized.new(handler.result, original) end end end |
#optimize_image!(original) ⇒ Object
Optimize one file in place, return original as OptimizedImagePath or nil if optimization failed
86 87 88 89 90 91 92 |
# File 'lib/image_optim.rb', line 86 def optimize_image!(original) original = ImagePath.convert(original) if result = optimize_image(original) result.replace(original) ImagePath::Optimized.new(original, result.original_size) end end |
#optimize_image_data(original_data) ⇒ Object
Optimize image data, return new data or nil if optimization failed
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/image_optim.rb', line 95 def optimize_image_data(original_data) format = ImageSize.new(original_data).format ImagePath.temp_file %W[image_optim .#{format}] do |temp| temp.binmode temp.write(original_data) temp.close if result = optimize_image(temp.path) result.open('rb', &:read) end end end |
#optimize_images(paths, &block) ⇒ Object
Optimize multiple images if block given yields path and result for each image and returns array of yield results else return array of results
111 112 113 |
# File 'lib/image_optim.rb', line 111 def optimize_images(paths, &block) run_method_for(paths, :optimize_image, &block) end |
#optimize_images!(paths, &block) ⇒ Object
Optimize multiple images in place if block given yields path and result for each image and returns array of yield results else return array of results
118 119 120 |
# File 'lib/image_optim.rb', line 118 def optimize_images!(paths, &block) run_method_for(paths, :optimize_image!, &block) end |
#optimize_images_data(datas, &block) ⇒ Object
Optimize multiple image datas if block given yields original and result for each image data and returns array of yield results else return array of results
125 126 127 |
# File 'lib/image_optim.rb', line 125 def optimize_images_data(datas, &block) run_method_for(datas, :optimize_image_data, &block) end |
#resolve_bin!(bin) ⇒ Object
Check existance of binary, create symlink if ENV contains path for key XXX_BIN where XXX is upper case bin name
149 150 151 |
# File 'lib/image_optim.rb', line 149 def resolve_bin!(bin) @bin_resolver.resolve!(bin) end |
#verbose? ⇒ Boolean
Verbose output?
17 18 19 |
# File 'lib/image_optim.rb', line 17 def verbose? @verbose end |
#workers_for_image(path) ⇒ Object
Get workers for image
64 65 66 |
# File 'lib/image_optim.rb', line 64 def workers_for_image(path) @workers_by_format[ImagePath.convert(path).format] end |