janko / image_processing

High-level image processing wrapper for libvips and ImageMagick/GraphicsMagick
MIT License
863 stars 76 forks source link

Add resize_to_cover #120

Closed brendon closed 4 months ago

brendon commented 6 months ago

This method allows one to provide a desired size to cover (width and height) and the image will be resized maintaining the ratio so that it covers that box without cropping the other oversized dimension. This behaves similar to object-fit: cover; in CSS.

I've tested that this works in my Active Storage code

image.file.variant(resize_to_cover: [696, 464])

but unfortunately I'm a bit confused about how the tests work. I need to know the dimensions of the image for the ratio calculations but the tests provide the file as a string. Is there something I can call in my resize_to_cover method to load the image so I can be sure I have the dimensions?

brendon commented 6 months ago

It seems to be related to assumptions around method names. Pretty tricky :D

https://github.com/janko/image_processing/blob/c20d147765a064840d4cad0ff6b7ebcfe65eeaa8/lib/image_processing/processor.rb#L9

My method requires knowing the dimensions of the image, so it needs to be loaded. Changing the method name so that it doesn't start with resize_ makes the test pass, but that seems like a bad workaround.

I see this PR too, that seems slightly related: https://github.com/janko/image_processing/pull/75/files

brendon commented 6 months ago

I've gone with renaming the method to cover and have added the method for MiniMagick also (along with tests and a CHANGELOG). I don't see anywhere for documentation other than inline so hopefully that's ok?

If you're happy, I think this one is ready to go.

brendon commented 5 months ago

Hi @janko, just wondering if you needed me to do anything else to this PR? I'm happy to discuss the reasons behind it if it's not making sense :)

etherbob commented 5 months ago

I'll close mine my PR in favor of this one.

brendon commented 4 months ago

Hi @janko, just following this up again. Do you have any feedback on the PR? Happy to change what needs changing.

janko commented 4 months ago

Thanks for the PR, and sorry for the delay. The implementation looks good to me, would you mind adding documentation to docs/minimagick.md and docs/vips.md?

brendon commented 4 months ago

No worries at all. I've just pushed that change now.

janko commented 4 months ago

@brendon I was under the impression it would be called #resize_to_cover, for consistency with other resizing methods 🤔 I read your previous comments, but didn't understand exactly what was the blocker.

janko commented 4 months ago

Oh right, re-read it now and got the issue. OK, I will see if I can fix that after merging, so that it can be called #resize_to_cover. Appreciate the documentation update 🙏🏻

brendon commented 4 months ago

Thanks @janko, yea it was a bit of a mind-bender at the time :D Weird test failures going on there (not related to this PR by the looks...)

Also happy to rename it in this PR if you make your mod on main first.

janko commented 4 months ago

Not sure what's up with Ruby 2.6 & 2.7 failures, but I'll go ahead and merge this, and fix that afterwards.

brendon commented 4 months ago

Thanks again :) Locally I was getting random test failures because of:

Errno::EMFILE: Too many open files - magick

Re-running usually let them pass. That's on macOS but I suppose it could be a similar issue on the CI.

janko commented 4 months ago

Oh, maybe the test suite should be explicitly cleaning out tempfiles between runs. I might have upped the OS limit for file descriptors somewhere. I'm also on macOS.

janko commented 3 months ago

FYI, in https://github.com/janko/image_processing/commit/9d72e46bce6546f3e5344c810bdcd62cfc07328c I added support for resize-on-load and renamed it back to #resize_to_cover. Plan to release it in the next week or two.

brendon commented 3 months ago

Nice @janko! :) Looking forward to using it :D

brendon commented 2 months ago

Hi @janko, no major rush, but would you mind doing a release with this update in it in the next few weeks? :)

janko commented 2 months ago

@brendon Just released 1.13.0 with this change 😉

brendon commented 2 months ago

Thanks @janko, much appreciated :)