diff --git a/.gitignore b/.gitignore index b61a9b05..b7afc813 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ Gemfile.lock Makefile +.DS_Store *.o *.bundle /tmp/ diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index f706c693..894c778d 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -3,6 +3,7 @@ ## unreleased * Use quality `0..100` by default in lossy mode of pngquant worker [#77](https://github.com/toy/image_optim/issues/77) [@toy](https://github.com/toy) +* Don't do anything when "optimized" version is larger and `:skip_bigger => true` [@R167](https://github.com/R167) ## v0.21.0 (2015-05-30) diff --git a/README.markdown b/README.markdown index 530ba887..c9d31c72 100644 --- a/README.markdown +++ b/README.markdown @@ -56,7 +56,7 @@ gem 'image_optim_pack' With version: ```ruby -gem 'image_optim', '~> 0.11' +gem 'image_optim', '~> 0.21' ``` If you want to check latest changes: @@ -271,6 +271,7 @@ optipng: * `:pack` — Require image\_optim\_pack or disable it, by default image\_optim\_pack will be used if available, will turn on `:skip-missing-workers` unless explicitly disabled *(defaults to `nil`)* * `:skip_missing_workers` — Skip workers with missing or problematic binaries *(defaults to `false`)* * `:allow_lossy` — Allow lossy workers and optimizations *(defaults to `false`)* +* `:skip_bigger` — Perform sanity check (esp. on jpg) and only return if new image is smaller *(defaults to `false`)* Worker can be disabled by passing `false` instead of options hash or by setting option `:disable` to `true`. diff --git a/lib/image_optim.rb b/lib/image_optim.rb index 05b634f2..115ea280 100644 --- a/lib/image_optim.rb +++ b/lib/image_optim.rb @@ -37,6 +37,9 @@ class ImageOptim # Allow lossy workers and optimizations attr_reader :allow_lossy + # Skip images where optimization makes them bigger + attr_reader :skip_bigger + # Initialize workers, specify options using worker underscored name: # # pass false to disable worker @@ -67,6 +70,7 @@ def initialize(options = {}) pack skip_missing_workers allow_lossy + skip_bigger ].each do |name| instance_variable_set(:"@#{name}", config.send(name)) $stderr << "#{name}: #{send(name)}\n" if verbose @@ -101,6 +105,7 @@ def optimize_image(original) end end return unless result + return if skip_bigger && result.size > original.size ImagePath::Optimized.new(result, original) end diff --git a/lib/image_optim/config.rb b/lib/image_optim/config.rb index 37b767c5..2c076ca9 100644 --- a/lib/image_optim/config.rb +++ b/lib/image_optim/config.rb @@ -133,6 +133,11 @@ def pack false end + # Whether or not to perfom size sanity check + def skip_bigger + !!get!(:skip_bigger) + end + # Skip missing workers, converted to boolean def skip_missing_workers if key?(:skip_missing_workers) diff --git a/lib/image_optim/runner/option_parser.rb b/lib/image_optim/runner/option_parser.rb index 7637eab8..882730b7 100644 --- a/lib/image_optim/runner/option_parser.rb +++ b/lib/image_optim/runner/option_parser.rb @@ -143,6 +143,11 @@ def wrap_regex(width) options[:pack] = pack end + op.on('--[no-]skip-bigger', 'Skip files that end up becoming larger. '\ + '(defaults to false)') do |skip_bigger| + options[:skip_bigger] = skip_bigger + end + op.separator nil op.separator ' Disabling workers:' diff --git a/spec/image_optim/config_spec.rb b/spec/image_optim/config_spec.rb index bfb7d8a3..27fcacf0 100644 --- a/spec/image_optim/config_spec.rb +++ b/spec/image_optim/config_spec.rb @@ -45,6 +45,27 @@ end end + describe :skip_bigger do + before do + allow(IOConfig).to receive(:read_options).and_return({}) + end + + it 'is true by default' do + config = IOConfig.new({}) + expect(config.skip_bigger).to be false + end + + it 'is false when false' do + config = IOConfig.new(:skip_bigger => true) + expect(config.skip_bigger).to be true + end + + it 'is false when false' do + config = IOConfig.new(:skip_bigger => false) + expect(config.skip_bigger).to be false + end + end + describe :threads do before do allow(IOConfig).to receive(:read_options).and_return({}) diff --git a/spec/image_optim_spec.rb b/spec/image_optim_spec.rb index d3dadf38..59f3f4b7 100644 --- a/spec/image_optim_spec.rb +++ b/spec/image_optim_spec.rb @@ -89,6 +89,33 @@ def temp_copy(image) end end end + + describe 'skipping larger' do + let(:original){ double(:size => 12_345) } + let(:optimized){ double(:original_size => 12_345, :size => 100_000) } + + it 'skips larger files' do + image_optim = ImageOptim.new(:skip_bigger => true) + + allow(ImageOptim::ImagePath).to receive(:convert).and_return original + allow(image_optim).to receive(:workers_for_image).with(original). + and_return([]) + allow(ImageOptim::Handler).to receive(:for).and_return optimized + + expect(image_optim.optimize_image(original)).to be nil + end + + it 'does not skips larger files when opt is off' do + image_optim = ImageOptim.new(:skip_bigger => false) + + allow(ImageOptim::ImagePath).to receive(:convert).and_return original + allow(image_optim).to receive(:workers_for_image).with(original). + and_return([]) + allow(ImageOptim::Handler).to receive(:for).and_return optimized + + expect(image_optim.optimize_image(original)).to_not be nil + end + end end it 'ignores text file' do