Class: ImageOptim
- Inherits:
-
Object
- Object
- ImageOptim
- Defined in:
- lib/image_optim.rb,
lib/image_optim/cmd.rb,
lib/image_optim/path.rb,
lib/image_optim/cache.rb,
lib/image_optim/space.rb,
lib/image_optim/table.rb,
lib/image_optim/timer.rb,
lib/image_optim/config.rb,
lib/image_optim/errors.rb,
lib/image_optim/runner.rb,
lib/image_optim/worker.rb,
lib/image_optim/handler.rb,
lib/image_optim/cache_path.rb,
lib/image_optim/image_meta.rb,
lib/image_optim/worker/svgo.rb,
lib/image_optim/bin_resolver.rb,
lib/image_optim/elapsed_time.rb,
lib/image_optim/hash_helpers.rb,
lib/image_optim/worker/jhead.rb,
lib/image_optim/worker/advpng.rb,
lib/image_optim/worker/oxipng.rb,
lib/image_optim/worker/pngout.rb,
lib/image_optim/optimized_path.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/pngquant.rb,
lib/image_optim/benchmark_result.rb,
lib/image_optim/bin_resolver/bin.rb,
lib/image_optim/worker/jpegoptim.rb,
lib/image_optim/option_definition.rb,
lib/image_optim/bin_resolver/error.rb,
lib/image_optim/configuration_error.rb,
lib/image_optim/runner/glob_helpers.rb,
lib/image_optim/runner/option_parser.rb,
lib/image_optim/worker/class_methods.rb,
lib/image_optim/worker/jpegrecompress.rb,
lib/image_optim/non_negative_integer_range.rb,
lib/image_optim/bin_resolver/simple_version.rb,
lib/image_optim/bin_resolver/comparable_condition.rb
Overview
Main interface
Defined Under Namespace
Modules: Cmd, ElapsedTime, Errors, HashHelpers, ImageMeta, OptionHelpers, Space Classes: BenchmarkResult, BinResolver, Cache, CachePath, Config, ConfigurationError, Error, Handler, NonNegativeIntegerRange, OptimizedPath, OptionDefinition, Path, Runner, Table, Timer, TrueFalseNil, Worker
Instance Attribute Summary collapse
-
#allow_lossy ⇒ Object
readonly
Allow lossy workers and optimizations.
-
#cache_dir ⇒ Object
readonly
Cache directory.
-
#cache_worker_digests ⇒ Object
readonly
Cache worker digests.
-
#nice ⇒ Object
readonly
Nice level.
-
#pack ⇒ Object
readonly
Use image_optim_pack.
-
#skip_missing_workers ⇒ Object
readonly
Skip workers with missing or problematic binaries.
-
#threads ⇒ Object
readonly
Number of threads to run with.
-
#timeout ⇒ Object
readonly
Timeout in seconds for each image.
-
#verbose ⇒ Object
readonly
Verbose output?.
Class Method Summary collapse
-
.full_version ⇒ Object
Full version of image_optim.
-
.method_missing(method, *args, &block) ⇒ Object
Optimization methods with default options.
- .respond_to_missing?(method, include_private = false) ⇒ Boolean
-
.version ⇒ Object
Version of image_optim gem spec loaded.
Instance Method Summary collapse
- #benchmark_image(original) ⇒ Object
- #benchmark_images(paths, &block) ⇒ Object
-
#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 OptimizedPath or nil if optimization failed.
-
#optimize_image!(original) ⇒ Object
Optimize one file in place, return original as OptimizedPath 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 path and result pairs.
-
#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 path and result pairs.
-
#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 path and result pairs.
-
#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.
-
#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)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/image_optim.rb', line 75 def initialize( = {}) config = Config.new() @verbose = config.verbose $stderr << "config:\n#{config.to_s.gsub(/^/, ' ')}" if verbose %w[ nice threads pack skip_missing_workers allow_lossy cache_dir cache_worker_digests timeout ].each do |name| instance_variable_set(:"@#{name}", config.send(name)) $stderr << "#{name}: #{send(name)}\n" if verbose end @bin_resolver = BinResolver.new(self) $stderr << "PATH: #{@bin_resolver.env_path}\n" if verbose @workers_by_format = Worker.create_all_by_format(self) do |klass| config.for_worker(klass) end @cache = Cache.new(self, @workers_by_format) log_workers_by_format if verbose config. end |
Instance Attribute Details
#allow_lossy ⇒ Object (readonly)
Allow lossy workers and optimizations
45 46 47 |
# File 'lib/image_optim.rb', line 45 def allow_lossy @allow_lossy end |
#cache_dir ⇒ Object (readonly)
Cache directory
48 49 50 |
# File 'lib/image_optim.rb', line 48 def cache_dir @cache_dir end |
#cache_worker_digests ⇒ Object (readonly)
Cache worker digests
51 52 53 |
# File 'lib/image_optim.rb', line 51 def cache_worker_digests @cache_worker_digests end |
#nice ⇒ Object (readonly)
Nice level
30 31 32 |
# File 'lib/image_optim.rb', line 30 def nice @nice end |
#pack ⇒ Object (readonly)
Use image_optim_pack
39 40 41 |
# File 'lib/image_optim.rb', line 39 def pack @pack end |
#skip_missing_workers ⇒ Object (readonly)
Skip workers with missing or problematic binaries
42 43 44 |
# File 'lib/image_optim.rb', line 42 def skip_missing_workers @skip_missing_workers end |
#threads ⇒ Object (readonly)
Number of threads to run with
33 34 35 |
# File 'lib/image_optim.rb', line 33 def threads @threads end |
#timeout ⇒ Object (readonly)
Timeout in seconds for each image
54 55 56 |
# File 'lib/image_optim.rb', line 54 def timeout @timeout end |
#verbose ⇒ Object (readonly)
Verbose output?
36 37 38 |
# File 'lib/image_optim.rb', line 36 def verbose @verbose end |
Class Method Details
.full_version ⇒ Object
Full version of image_optim
233 234 235 |
# File 'lib/image_optim.rb', line 233 def full_version "image_optim v#{version}" end |
.method_missing(method, *args, &block) ⇒ Object
Optimization methods with default options
213 214 215 216 217 218 219 |
# File 'lib/image_optim.rb', line 213 def method_missing(method, *args, &block) if optimize_image_method?(method) new.send(method, *args, &block) else super end end |
.respond_to_missing?(method, include_private = false) ⇒ Boolean
221 222 223 |
# File 'lib/image_optim.rb', line 221 def respond_to_missing?(method, include_private = false) optimize_image_method?(method) || super end |
.version ⇒ Object
Version of image_optim gem spec loaded
226 227 228 229 230 |
# File 'lib/image_optim.rb', line 226 def version Gem.loaded_specs['image_optim'].version.to_s rescue 'DEV' end |
Instance Method Details
#benchmark_image(original) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/image_optim.rb', line 167 def benchmark_image(original) src = Path.convert(original) return unless (workers = workers_for_image(src)) dst = src.temp_path begin workers.map do |worker| start = ElapsedTime.now worker.optimize(src, dst) BenchmarkResult.new(src, dst, ElapsedTime.now - start, worker) end ensure dst.unlink end end |
#benchmark_images(paths, &block) ⇒ Object
207 208 209 |
# File 'lib/image_optim.rb', line 207 def benchmark_images(paths, &block) run_method_for(paths, :benchmark_image, &block) end |
#env_path ⇒ Object
Join resolve_dir, default path and vendor path for PATH environment variable
256 257 258 |
# File 'lib/image_optim.rb', line 256 def env_path @bin_resolver.env_path end |
#optimizable?(path) ⇒ Boolean
Are there workers for file at path?
245 246 247 |
# File 'lib/image_optim.rb', line 245 def optimizable?(path) !!workers_for_image(path) end |
#optimize_image(original) ⇒ Object
Optimize one file, return new path as OptimizedPath or nil if optimization failed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/image_optim.rb', line 116 def optimize_image(original) original = Path.convert(original) return unless (workers = workers_for_image(original)) optimized = @cache.fetch(original) do timer = timeout && Timer.new(timeout) Handler.for(original) do |handler| begin workers.each do |worker| handler.process do |src, dst| worker.optimize(src, dst, timeout: timer) end end rescue Errors::TimeoutExceeded handler.result end end end return unless optimized OptimizedPath.new(optimized, original) end |
#optimize_image!(original) ⇒ Object
Optimize one file in place, return original as OptimizedPath or nil if optimization failed
143 144 145 146 147 148 149 |
# File 'lib/image_optim.rb', line 143 def optimize_image!(original) original = Path.convert(original) return unless (result = optimize_image(original)) result.replace(original) OptimizedPath.new(original, result.original_size) end |
#optimize_image_data(original_data) ⇒ Object
Optimize image data, return new data or nil if optimization failed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/image_optim.rb', line 152 def optimize_image_data(original_data) format = ImageMeta.format_for_data(original_data) return unless format Path.temp_file %W[image_optim .#{format}] do |temp| temp.binmode temp.write(original_data) temp.close if (result = optimize_image(temp.path)) result.binread 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 path and result pairs
187 188 189 |
# File 'lib/image_optim.rb', line 187 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 path and result pairs
195 196 197 |
# File 'lib/image_optim.rb', line 195 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 path and result pairs
203 204 205 |
# File 'lib/image_optim.rb', line 203 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
251 252 253 |
# File 'lib/image_optim.rb', line 251 def resolve_bin!(bin) @bin_resolver.resolve!(bin) end |