-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for the "crop" method. #2721
Conversation
Two tests are failing, but I'm pretty sure neither has anything to do with me! 1. Test / RSpec and Cucumber (ruby-head, gemfiles/rails-7-1.gemfile, false) (pull_request)
2. Test / RSpec and Cucumber (jruby-head, gemfiles/rails-7-0.gemfile, true) (pull_request)
|
Only one test failing now, on JRuby-Head. Been looking at it for a while and it seems to have to do with the serialisation of ActiveRecord object; it's failing on # activerecord_spec.rb
describe CarrierWave::ActiveRecord do
def create_table(name)
ActiveRecord::Base.connection.create_table(name, force: true) do |t|
t.column :image, :string
t.column :images, :json
t.column :textfile, :string
t.column :textfiles, :json
t.column :foo, :string
end
end
...
def reset_class(class_name)
Object.send(:remove_const, class_name) rescue nil
Object.const_set(class_name, Class.new(ActiveRecord::Base))
end
before(:all) { create_table("events") }
after(:all) { drop_table("events") }
before do
@uploader = Class.new(CarrierWave::Uploader::Base)
reset_class("Event")
@event = Event.new
end
...
it "should return valid XML when to_xml is called when image is nil" do
#### THIS TEST FAILS ON @event.to_xml ####
hash = Hash.from_xml(@event.to_xml)["event"]
expect(@event[:image]).to be_nil
expect(hash.keys).to include("image")
expect(hash["image"].keys).to include("url")
expect(hash["image"]["url"]).to be_nil
end
it "should return valid XML when to_xml is called when image is present" do
#### THIS TEST ALSO FAILS ON @event.to_xml ####
@event[:image] = 'test.jpeg'
@event.save!
@event.reload
expect(Hash.from_xml(@event.to_xml)["event"]["image"]).to eq({"url" => "/uploads/test.jpeg"})
end
... It might be that a change in |
Some obeservations:
|
I could not find anywhere this is specified, except for: * A very recent test in CRuby's test/ruby/test_shape.rb that confirms a degraded "complex" shape using CRuby's "st table" hashtable will continue to be returned in insertion order. This implies that insertion order is expected. * A rubyspec added in 2021 in instance_variable_spec.rb that confirms instance variables are returned in insertion order. No links to issues or discussions is provided. Since this is a simple enough change, we'll go ahead with it, but I don't know that it has ever been formally specified (and indeed I looked through other tests of other types of variables and see that there are commits to .sort them before inspection in tests.) Fixes #7988
I've added a question to the JRuby commit mentioned in my previous comment: |
Thank you so much for taking a deep look on the JRuby failure! But is it possible to add |
I'll take a look! |
Updated "crop" method of Vips processor to retain image bottom/right edge if cropping box falls outside the image boundaries. Added tests for cropping within and beyond image boundaries for all three processors.
I have added support for image cropping to the RMagic and MiniMagic processors as well. In doing so I found a difference in behaviour between Vips and the other two: while both RMagic and MiniMagic silently accept cropping beyond the image boundaries (retaining the original image bottom/right edge), Vips would throw an def crop(left, top, width, height, combine_options: {})
width, height = resolve_dimensions(width, height)
width = vips_image.width - left if width + left > vips_image.width
height = vips_image.height - top if height + top > vips_image.height
vips! do |builder|
builder.crop(left, top, width, height)
.apply(combine_options)
end
end I added specs to all three processors to test for this case. |
RMagic should `crop!` with a bang! MiniMagic `crop` method should `.apply(combine_options)`. Fixed copy/paste error in MiniMagic `crop` method description. Removed redundant blank line in MiniMagic & RMagic `crop` method descriptions.
I'm working on updating the README to include information about Vips support and the |
You can include it into this PR 👍 |
Ok, here's what I had in mind: Manipulating images[no changes] Using MiniMagick[no changes] Using RMagick[no changes] Using VipsCarrierWave version 2.2.0 added support for the $ sudo apt install libvips You also need to tell your uploader to use Vips: class ImageFileUploader < CarrierWave::Uploader::Base
include CarrierWave::Vips
...
end List of available processing methods:Note While the intention is to provide uniform interfaces to all three processing libraries the availability and implementation of processing methods can vary slightly between them.
Supported processing methodsThe following table shows which processing methods are supported by each processing library, and which parameters they accept:
1 2 3 |
|
||
<sup>2</sup>`gravity` refers to an image position given as one of `Center`, `North`, `NorthWest`, `West`, `SouthWest`, `South`, `SouthEast`, `East`, or `NorthEast`. | ||
|
||
<sup>3</sup>`geometry_string` is an [ImageMagick geometry string](https://rmagick.github.io/imusage.html#geometry). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice comparison table 👍
Seppling... Co-authored-by: Mitsuhiro Shibuya <[email protected]>
Great work, thank you so much! |
My pleasure! |
I had a need for this functionality, and while I know I could use the
vips!
method in my uploader it felt like an omission - so I put together a PR with a (passing) spec. The test is a little basic, but I wasn't sure if it would be ok to add new image fixtures, which I would need to do in order to test that the cropping produces the correct output (my test only checks the resulting dimensions). I'd be happy to look at implementing further vips methods if desired!