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

New EXIF library returns an image orientation that doesn't pass (eq $o 6) #12733

Closed
snicolai-blog opened this issue Aug 9, 2024 · 4 comments · Fixed by #12742
Closed

New EXIF library returns an image orientation that doesn't pass (eq $o 6) #12733

snicolai-blog opened this issue Aug 9, 2024 · 4 comments · Fixed by #12742
Assignees
Milestone

Comments

@snicolai-blog
Copy link

I have the following fragment in a render-image.html file to set aspect ratios on images. This works in hugo prior to the new EXIF library.

{{- /* get file that matches the filename as specified as src="" in shortcode */ -}}
{{- $src := .Page.Resources.GetMatch (printf "%s" (.Destination)) -}}
{{- $imgPath := .Destination -}}
{{- if $src -}}

{{- $imgWidth := $src.Width -}}
{{- $imgHeight := $src.Height -}}
{{- with $src.Exif -}}
  {{- $o :=  .Tags.Orientation -}}
  {{- warnf (printf "%s orientation %d width %d height %d" $imgPath $o $imgWidth $imgHeight) }}
  {{- if (or (eq $o 6) (eq $o 8)) -}}
    {{- $imgWidth = $src.Height -}}
    {{- $imgHeight = $src.Width -}}
    {{- warnf "flipping width and height" -}}
  {{- end -}}
{{- end -}}

The warnings print out the orientation properly in 0.131, but the eq $o 6 test fails and so the aspect ratio on my image is wrong. Here are some example warnings where it doesn't flip an image.

WARN  birguentrance.jpg orientation 6 width 6960 height 4640
WARN  victoriagate.jpg orientation 1 width 5161 height 4167

In hugo 0.131 I have to use the following to get it to work. It looks like the type returned by .Orientation isn't the same type as before and now fails the eq test. Converting to a string with printf and comparing strings works in 0.131 and newer.

{{- /* get file that matches the filename as specified as src="" in shortcode */ -}}
{{- $src := .Page.Resources.GetMatch (printf "%s" (.Destination)) -}}
{{- $imgPath := .Destination -}}
{{- if $src -}}

{{- $imgWidth := $src.Width -}}
{{- $imgHeight := $src.Height -}}
{{- with $src.Exif -}}
  {{- $o := (printf "%d" .Tags.Orientation) -}}
  {{- warnf (printf "%s orientation %d width %d height %d" $imgPath $o $imgWidth $imgHeight) }}
  {{- if (or (eq $o "6") (eq $o "8")) -}}
    {{- $imgWidth = $src.Height -}}
    {{- $imgHeight = $src.Width -}}
    {{- warnf "flipping width and height" -}}
  {{- end -}}
{{- end -}}

These are the example warnings from this changed code that properly flips the aspect ratio:

WARN  birguentrance.jpg orientation %!!(MISSING)d(string=6) width 6960 height 4640
WARN  flipping width and height
WARN  victoriagate.jpg orientation %!!(MISSING)d(string=1) width 5161 height 4167

What I want is for the same template to work in both systems. Either document the changes I need to make to my templates to support both types (preferably in the release that breaks it). Or fix the new library to return types that are compatible with my existing template.

@bep bep self-assigned this Aug 9, 2024
@bep bep removed the NeedsTriage label Aug 9, 2024
@bep bep added this to the v0.131.0 milestone Aug 9, 2024
@bep
Copy link
Member

bep commented Aug 9, 2024

I think you can work around this by:

 {{- $o :=  .Tags.Orientation | int -}}

A note mostly to myself here:

  • In my new library, I have kept the int data types as dictated by their length, e.g. 2 bytes integer => int16.
  • I have not checked, but I'm guessing the eq function considers eq int16(6) 6 to be falsy, which smells like a bug in itself.

It's tempting to just change the EXIF types to int, but that can lead to overflow issues, which is also not great.

@bep bep modified the milestones: v0.131.0, v0.133.0, v0.132.0 Aug 9, 2024
@snicolai-blog
Copy link
Author

 {{- $o :=  .Tags.Orientation | int -}}

Works in 0.131, and I haven't tested it in versions prior to 0.130 yet, but I suspect that works there as well.

My hosting provider that runs hugo doesn't let me control when hugo is upgraded, so I need to support the older hugo version till they upgrade with my templates, but like to test with the newest version so I can see what changes I'll need in the future.

I agree that the proper fix is to fix the comparisons so that eq int16(6) 6 is true for the appropriate operators and types.

@bep
Copy link
Member

bep commented Aug 9, 2024

{{- $o := .Tags.Orientation | int -}}

The above should work with "all" Hugo versions (including future).

eq int16(6) 6

I have checked, and the failing case was eq uint16(6) 6.

bep added a commit to bep/hugo that referenced this issue Aug 10, 2024
bep added a commit to bep/hugo that referenced this issue Aug 10, 2024
bep added a commit to bep/hugo that referenced this issue Aug 10, 2024
@bep bep closed this as completed in fbfccb3 Aug 10, 2024
Copy link

github-actions bot commented Sep 1, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants