Skip to content
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

Add resize_to_cover #120

Merged
merged 7 commits into from
Jun 6, 2024
Merged

Add resize_to_cover #120

merged 7 commits into from
Jun 6, 2024

Conversation

brendon
Copy link
Contributor

@brendon brendon commented Mar 22, 2024

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
Copy link
Contributor Author

brendon commented Mar 24, 2024

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

if operations.dig(0, 0).to_s.start_with?("resize_") &&

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
Copy link
Contributor Author

brendon commented Mar 24, 2024

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
Copy link
Contributor Author

brendon commented Apr 10, 2024

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
Copy link
Contributor

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

@brendon
Copy link
Contributor Author

brendon commented Jun 5, 2024

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

@janko
Copy link
Owner

janko commented Jun 6, 2024

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
Copy link
Contributor Author

brendon commented Jun 6, 2024

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

@janko
Copy link
Owner

janko commented Jun 6, 2024

@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
Copy link
Owner

janko commented Jun 6, 2024

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
Copy link
Contributor Author

brendon commented Jun 6, 2024

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
Copy link
Owner

janko commented Jun 6, 2024

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 janko merged commit 8c85ad9 into janko:master Jun 6, 2024
7 of 9 checks passed
@brendon
Copy link
Contributor Author

brendon commented Jun 6, 2024

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
Copy link
Owner

janko commented Jun 6, 2024

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
Copy link
Owner

janko commented Jun 15, 2024

FYI, in 9d72e46 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
Copy link
Contributor Author

brendon commented Jun 15, 2024

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

@brendon
Copy link
Contributor Author

brendon commented Jul 24, 2024

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

@janko
Copy link
Owner

janko commented Jul 24, 2024

@brendon Just released 1.13.0 with this change 😉

@brendon
Copy link
Contributor Author

brendon commented Jul 24, 2024

Thanks @janko, much appreciated :)

@brendon brendon deleted the resize-to-cover branch August 2, 2024 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants