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

Create AutoOrient image filter #11717

Closed
jmooring opened this issue Nov 17, 2023 · 7 comments · Fixed by #11819
Closed

Create AutoOrient image filter #11717

jmooring opened this issue Nov 17, 2023 · 7 comments · Fixed by #11819

Comments

@jmooring
Copy link
Member

This would allow users to fix orientation issues when .Exif.Tags.Orientation is one of 2, 4, 5 or 7.

Ref: https://discourse.gohugo.io/t/image-processing-rotates-images/

@bep bep removed the NeedsTriage label Nov 17, 2023
@bep bep added this to the v0.121.0 milestone Nov 17, 2023
@bep
Copy link
Member

bep commented Nov 17, 2023

Yea, that's a good idea. It's Friday night here and I'm a bit tired, but I suspect there may be a way to make this one filter? Could we not just have a images.Orientation filter that respected the EXIF values?

@jmooring
Copy link
Member Author

Misc thoughts...

Is there much of a performance hit for reading the EXIF info every time?

Is there a circumstance where users might want to flip x/y even when .Exif.Tags.Orientation is 0 or 1?

@bep
Copy link
Member

bep commented Nov 17, 2023

Is there much of a performance hit for reading the EXIF info every time?

No (there is a one time cost)

I borrowed this from somewhere:

image

Is there a circumstance where users might want to flip x/y even when .Exif.Tags.Orientation is 0 or 1?

Maybe, but the use case we're talking about here is to get the image back in normal orientation, so a images.Orientationthat by default took no arguments should be good.

I suspect that images.Flip would also be useful, but slightly hard to use for the use case above.

@bep bep modified the milestones: v0.121.0, v0.122.0 Dec 6, 2023
@jmooring jmooring changed the title Create filters: images.FlipHorizontal and images.FlipVertical Create filter: images.Orientation Dec 15, 2023
@jmooring
Copy link
Member Author

jmooring commented Dec 15, 2023

I have a working implementation of this as a function...

{{ with resources.Get "Landscape_7.jpg" }}
  {{ with images.AutoOrient . }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

...but have not been able to figure out how to access the EXIF data from a filter.

tpl/images/images.go
// AutoOrient returns the given image, rotating and flipping as needed per the
// EXIF orientation value if present.
func (ns *Namespace) AutoOrient(i images.ImageResource) (images.ImageResource, error) {
	var filters = map[int]gift.Filter{
		2: gift.FlipHorizontal(),
		3: gift.Rotate180(),
		4: gift.FlipVertical(),
		5: gift.Transpose(),
		6: gift.Rotate270(),
		7: gift.Transverse(),
		8: gift.Rotate90(),
	}

	if i.Exif() != nil {
		orientation, ok := i.Exif().Tags["Orientation"].(int)
		if ok && orientation >= 2 && orientation <= 8 {
			return i.Filter(filters[orientation])
		}
	}

	return i, nil
}

Testing example (8 landscape orientations, 8 portrait orientations):

git clone --single-branch -b hugo-github-issue-11717 https://github.com/jmooring/hugo-testing hugo-github-issue-11717
cd hugo-github-issue-11717
hugo server

@bep
Copy link
Member

bep commented Dec 17, 2023

@jmooring re. filter vs method, I think I had the same "head scratcher" when implementing the Process filter, which I implemented using an interface to pass the "filter options" back to where the filters are applied:

https://github.com/gohugoio/hugo/blob/master/resources/images/process.go#L26

You could e.g. create a new interface ala:

type ImageFilterFromOrientationProvider interface {
   AutoOrient(orientation int) gift.Filter
}

And then adjust imageResource.Filter to check for the above interface.

@bep
Copy link
Member

bep commented Dec 18, 2023

I deleted my last comment, that doesn't make sense (we cannot modify the filter), I suggest we do as suggested above, with that we can return nil for "no orientation change".

@jmooring jmooring changed the title Create filter: images.Orientation Create AutoOrient image filter Dec 18, 2023
jmooring added a commit to jmooring/hugo that referenced this issue Dec 19, 2023
jmooring added a commit to jmooring/hugo that referenced this issue Dec 19, 2023
jmooring added a commit to jmooring/hugo that referenced this issue Dec 19, 2023
jmooring added a commit to jmooring/hugo that referenced this issue Dec 20, 2023
bep pushed a commit that referenced this issue Dec 20, 2023
Copy link

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 Jan 11, 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