A platform for community discussion. Free, open, simple.
FIX: Ensure 'crop' always returns requested dimensions (#16437)
David Taylor137e06a316b8
diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rbindex 041b9604c43..901e762328f 100644--- a/app/models/optimized_image.rb+++ b/app/models/optimized_image.rb@@ -252,7 +252,7 @@ class OptimizedImage < ActiveRecord::Base-auto-orient-gravity north-background transparent- -#{thumbnail_or_resize} #{opts[:width]}+ -#{thumbnail_or_resize} #{dimensions}^-crop #{dimensions}+0+0-unsharp 2x0.5+0.7+0-interlace none@@ -290,7 +290,6 @@ class OptimizedImage < ActiveRecord::Baseenddef self.crop(from, to, width, height, opts = {})- opts[:width] = widthoptimize("crop", from, to, "#{width}x#{height}", opts)enddiff --git a/spec/models/optimized_image_spec.rb b/spec/models/optimized_image_spec.rbindex 700474ff13f..c008ca6d8a3 100644--- a/spec/models/optimized_image_spec.rb+++ b/spec/models/optimized_image_spec.rb@@ -7,22 +7,23 @@ describe OptimizedImage dodescribe '.crop' doit 'should produce cropped images (requires ImageMagick 7)' dotmp_path = "/tmp/cropped.png"+ desired_width = 5+ desired_height = 5beginOptimizedImage.crop("#{Rails.root}/spec/fixtures/images/logo.png",tmp_path,- 5,- 5+ desired_width,+ desired_height)- # we don't want to deal with something new here every time image magick- # is upgraded or pngquant is upgraded, lets just test the basics ...- # cropped image should be less than 120 bytes+ w, h = FastImage.size(tmp_path)+ expect(w).to eq(desired_width)+ expect(h).to eq(desired_height)cropped_size = File.size(tmp_path)-- expect(cropped_size).to be < 120+ expect(cropped_size).to be < 200expect(cropped_size).to be > 50ensure@@ -30,6 +31,52 @@ describe OptimizedImage doendend+ it 'should correctly crop images vertically' do+ tmp_path = "/tmp/cropped.png"+ desired_width = 100+ desired_height = 66++ begin+ OptimizedImage.crop(+ "#{Rails.root}/spec/fixtures/images/logo.png", # 244x66px+ tmp_path,+ desired_width,+ desired_height+ )++ w, h = FastImage.size(tmp_path)++ expect(w).to eq(desired_width)+ expect(h).to eq(desired_height)++ ensure+ File.delete(tmp_path) if File.exist?(tmp_path)+ end+ end++ it 'should correctly crop images horizontally' do+ tmp_path = "/tmp/cropped.png"+ desired_width = 244+ desired_height = 500++ begin+ OptimizedImage.crop(+ "#{Rails.root}/spec/fixtures/images/logo.png", # 244x66px+ tmp_path,+ desired_width,+ desired_height+ )++ w, h = FastImage.size(tmp_path)++ expect(w).to eq(desired_width)+ expect(h).to eq(desired_height)++ ensure+ File.delete(tmp_path) if File.exist?(tmp_path)+ end+ end+describe ".resize_instructions" dolet(:image) { "#{Rails.root}/spec/fixtures/images/logo.png" }