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 minScale to image config #11151

Open
nibe opened this issue Jun 22, 2023 · 3 comments
Open

Add minScale to image config #11151

nibe opened this issue Jun 22, 2023 · 3 comments
Milestone

Comments

@nibe
Copy link

nibe commented Jun 22, 2023

$image.Fill with the default smart cropping unnecessarily crops images. Example:

{{ with .Resources.GetMatch "example.jpg" }}<!-- 300x200 px -->
  {{ $smart := .Fill "150x100" }}<!-- Same aspect ratio, no need to crop! -->
  {{ $expected := .Fill "150x100 Center" }}
  <img src="{{ .RelPermalink }}">
  <img src="{{ $smart.RelPermalink }}">
  <img src="{{ $expected.RelPermalink }}">
{{ end }}

example.jpg:
example
Smart crop (feathers chopped off, unnecessary because same aspect ratio):
smart-crop
Expected:
expected

This does not just apply when the aspect ratio is the same, it unnecessarily crops whenever it feels like it. I would rather not use another anchor because smart cropping would be super useful if it only cropped when necessary. I filed this as a bug rather than a feature request because it is the default behavior and unexpected (for me).

smartcrop.js (which the smartcrop library used in Hugo is based on) has the following option, which appears to prevent this issue: minScale: minimal scale of the crop rect, set to 1.0 to prevent smaller than necessary crops (lowers the risk of chopping things off).

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.114.0+extended darwin/amd64 BuildDate=unknown

Does this issue reproduce with the latest release?

Yes.

@jmooring
Copy link
Member

jmooring commented Jun 22, 2023

Somewhat related: muesli/smartcrop#21

Without being configurable, this would change images in the wild. So, proposal instead of bug.

@bep bep added this to the v0.115.0 milestone Jun 23, 2023
@bep bep changed the title Smart cropping unnecessarily chops off images Add minScale to image config (if that helps with smartcrop cropping) Jun 23, 2023
@bep bep added Enhancement and removed Proposal labels Jun 23, 2023
@nibe
Copy link
Author

nibe commented Jun 23, 2023

I just tested it and minScale in smartcrop.go does indeed cause this issue. It's set to 0.9 and changing it to 1.0 stops unnecessary cropping. Unfortunately, it is a constant.

@nibe nibe changed the title Add minScale to image config (if that helps with smartcrop cropping) Add minScale to image config Jun 23, 2023
@nibe
Copy link
Author

nibe commented Jun 23, 2023

I found a workaround by the way. First you smart crop with the aspect ratio you want, without reducing the width or height (so that minScale does not come into play). Then you downscale. This works because maxScale is 1.0.

{{ with .Resources.GetMatch "example.jpg" }}<!-- 300x200 px -->
  {{ $smart := .Fill "200x150" }}
  {{ $workaround := (.Fill "300x225 q100").Resize "200x" }}
  <img src="{{ .RelPermalink }}">
  <img src="{{ $smart.RelPermalink }}">
  <img src="{{ $workaround.RelPermalink }}">
{{ end }}

Smart (unnecessarily crops vertically):
smart
Workaround:
workaround

@bep bep modified the milestones: v0.115.0, v0.116.0 Jun 30, 2023
@bep bep modified the milestones: v0.116.0, v0.117.0 Aug 1, 2023
@bep bep modified the milestones: v0.117.0, v0.118.0 Aug 30, 2023
@bep bep modified the milestones: v0.118.0, v0.119.0 Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 5, 2023
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@bep bep removed this from the v0.121.0 milestone Dec 6, 2023
@bep bep added this to the v0.122.0 milestone Dec 6, 2023
@bep bep modified the milestones: v0.122.0, v0.123.0, v0.124.0 Jan 27, 2024
@bep bep modified the milestones: v0.124.0, v0.125.0 Mar 4, 2024
@bep bep modified the milestones: v0.125.0, v0.137.0, v0.138.0, v0.139.0 Oct 23, 2024
@bep bep modified the milestones: v0.139.0, v0.140.0 Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants