image_processing icon indicating copy to clipboard operation
image_processing copied to clipboard

Add resize_to_cover

Open brendon opened this issue 1 year ago • 4 comments

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 avatar Mar 22 '24 03:03 brendon

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 avatar Mar 24 '24 06:03 brendon

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 avatar Mar 24 '24 22:03 brendon

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 :)

brendon avatar Apr 10 '24 02:04 brendon

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

etherbob avatar Apr 25 '24 17:04 etherbob

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

brendon avatar Jun 05 '24 23:06 brendon

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?

janko avatar Jun 06 '24 07:06 janko

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

brendon avatar Jun 06 '24 09:06 brendon

@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 avatar Jun 06 '24 09:06 janko

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 🙏🏻

janko avatar Jun 06 '24 09:06 janko

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.

brendon avatar Jun 06 '24 09:06 brendon

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

janko avatar Jun 06 '24 10:06 janko

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.

brendon avatar Jun 06 '24 10:06 brendon

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 avatar Jun 06 '24 10:06 janko

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.

janko avatar Jun 15 '24 21:06 janko

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

brendon avatar Jun 15 '24 23:06 brendon

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

brendon avatar Jul 24 '24 01:07 brendon

@brendon Just released 1.13.0 with this change 😉

janko avatar Jul 24 '24 16:07 janko

Thanks @janko, much appreciated :)

brendon avatar Jul 24 '24 21:07 brendon