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 images.Overlay filter #8057

Closed
onedrawingperday opened this issue Dec 16, 2020 · 10 comments · Fixed by #8059
Closed

Add images.Overlay filter #8057

onedrawingperday opened this issue Dec 16, 2020 · 10 comments · Fixed by #8059
Assignees
Milestone

Comments

@onedrawingperday
Copy link
Contributor

onedrawingperday commented Dec 16, 2020

A while back I opened this topic in the forum.

This is a missing feature that causes me grief again and again.
So I decided to post in the GitHub issues tracker hoping that perhaps someone someday might look into it.

Use case: Square product thumbnails need to be generated from portrait product photos.

Current situation: If one uses for example {{- $thumb := ($original.Resize "1080x1080").RelPermalink }} they will end up with a distorted image since .Resize does exactly as told without any concept of respecting the aspect ratio. Similarly if one were to use {{- $thumb := ($original.Fit "1080x1080").RelPermalink }} they would end up with an image that respects the original aspect ratio but is not square.

I have sort of forgotten As far as I can tell the library that Hugo uses for image processing is https://github.com/disintegration/imaging/ if you think that this issue does not belong here, let me know and I will open an issue upstream.

@bep
Copy link
Member

bep commented Dec 16, 2020

{{- $thumb := ($original.Fit "1080x1080").RelPermalink }} they would end up with an image that respects the original aspect ratio but is not square.

What about .Fill?

@onedrawingperday
Copy link
Contributor Author

What about .Fill?

Unfortunately it crops the image with Smartcrop.

I will post all of the things I've tried for your reference:

  1. {{- $thumb := ($original.Fill "1080x1080").RelPermalink }}
    1500_hu77205ecf3c461ac981b377ddc9927806_227804_1080x1080_fill_q75_box_smart1

  2. {{- $thumb := ($original.Resize "1080x1080").RelPermalink }}
    1500_hu77205ecf3c461ac981b377ddc9927806_227804_1080x1080_resize_q75_box

  3. {{- $thumb := ($original.Fit "1080x1080").RelPermalink }}
    1500_hu77205ecf3c461ac981b377ddc9927806_227804_1080x1080_fit_q75_box

@bep
Copy link
Member

bep commented Dec 16, 2020

Unfortunately it crops the image with Smartcrop.

Sure, but that is an option -- you can put the anchor for the crop wherever you want.

@onedrawingperday
Copy link
Contributor Author

onedrawingperday commented Dec 16, 2020

Valid values are Smart, Center, TopLeft, Top, TopRight, Left, Right, BottomLeft, Bottom, BottomRight

I am afraid that I do not follow.

If I do {{- $thumb := ($original.Fill "1080x1080 center").RelPermalink }} I end up with:
1500_hu77205ecf3c461ac981b377ddc9927806_227804_1080x1080_fill_q75_box_center

With {{- $thumb := ($original.Fill "1080x1080 top").RelPermalink }} I end up with:

1500_hu77205ecf3c461ac981b377ddc9927806_227804_1080x1080_fill_q75_box_smart1

And so on for the other anchors.

My desired output would be:

square

As far as I can tell there is no way to add whitespace on the left and on the right of the original portrait image to make it square.

@bep
Copy link
Member

bep commented Dec 16, 2020

OK, I understand. I thought your main point was to get it ... square.

If you don't want to crop it, why not use Fit and make it square in CSS (e.g. center it in a square div)?

@onedrawingperday
Copy link
Contributor Author

onedrawingperday commented Dec 16, 2020

I am trying to generate a CSV Output Format with Hugo, so that it can be used by Facebook/Instagram to auto-update a product catalog.

The square format product photo is a requirement.

Oh well never mind... 🙄

I’ll just carry on running imageMagick to add the whitespace after generating a thumb with Hugo’s .Fit method -even though it turned out being a nightmare to maintain-.

@bep
Copy link
Member

bep commented Dec 16, 2020

Thinking about this, I think this fits better in the .Overlay method we discussed earlier. That has several use cases. of course (one being adding text to images?), but I imagine you could do something ala:

{{ $img := $someImage | $whiteSquare.Overlay }}

I'm not totally sure I got the order of the arguments right in the above, but the concept should be clear?

It was discussed here #4595

@bep
Copy link
Member

bep commented Dec 16, 2020

I will add a PR with a new Overlay filter function that you can test out later today... I think it will work great and have lots of other use cases.

@onedrawingperday
Copy link
Contributor Author

onedrawingperday commented Dec 16, 2020

As I understand it the .Overlay method in the Disintegration Doc and the related OverlayCenter a user can define a background image in the desired dimensions and use another image to overlay it on top.

For example in my use case something like:

{{/* whiteSquare.jpg is a white background 1080x1080px fetched as a .Resource */}}
{{ $whiteSquare := resources.Get "/whiteSquare.jpg" }}
{{- $thumb := ($original.Fit "1080x1080").RelPermalink | $whiteSquare.Overlay }}

Would do the job nicely.

Looking forward to your Pull Request. I will test it as soon as possible.

@bep bep added Enhancement and removed Proposal labels Dec 16, 2020
@bep bep self-assigned this Dec 16, 2020
@bep bep added this to the v0.80 milestone Dec 16, 2020
@bep bep changed the title Image Processing: Option for adding whitespace around an image Add images.Overlay filter Dec 16, 2020
bep added a commit to bep/hugo that referenced this issue Dec 16, 2020
This allows for constructs ala:

```
{{ $overlay := $img.Filter ($logo | images.Overlay 50 50 )}}
```
Or:

```
{{ $logoFilter := ($logo | images.Overlay 50 50 ) }}
{{ $overlay := $img | images.Filter $logoFilter }}
```

Which will overlay the logo in the top left corner (x=50, y=50) of `$img`.

Fixes gohugoio#8057
bep added a commit to bep/hugo that referenced this issue Dec 16, 2020
This allows for constructs ala:

```
{{ $overlay := $img.Filter (images.Overlay $logo 50 50 )}}
```
Or:

```
{{ $logoFilter := (images.Overlay $logo 50 50 ) }}
{{ $overlay := $img | images.Filter $logoFilter }}
```

Which will overlay the logo in the top left corner (x=50, y=50) of `$img`.

Fixes gohugoio#8057
bep added a commit to bep/hugo that referenced this issue Dec 16, 2020
This allows for constructs ala:

```
{{ $overlay := $img.Filter (images.Overlay $logo 50 50 )}}
```
Or:

```
{{ $logoFilter := (images.Overlay $logo 50 50 ) }}
{{ $overlay := $img | images.Filter $logoFilter }}
```

Which will overlay the logo in the top left corner (x=50, y=50) of `$img`.

Fixes gohugoio#8057
Fixes gohugoio#4595
Updates gohugoio#6731
bep added a commit to bep/hugo that referenced this issue Dec 16, 2020
This allows for constructs ala:

```
{{ $overlay := $img.Filter (images.Overlay $logo 50 50 )}}
```
Or:

```
{{ $logoFilter := (images.Overlay $logo 50 50 ) }}
{{ $overlay := $img | images.Filter $logoFilter }}
```

Which will overlay the logo in the top left corner (x=50, y=50) of `$img`.

Fixes gohugoio#8057
Fixes gohugoio#4595
Updates gohugoio#6731
@bep bep closed this as completed in #8059 Dec 17, 2020
bep added a commit that referenced this issue Dec 17, 2020
This allows for constructs ala:

```
{{ $overlay := $img.Filter (images.Overlay $logo 50 50 )}}
```
Or:

```
{{ $logoFilter := (images.Overlay $logo 50 50 ) }}
{{ $overlay := $img | images.Filter $logoFilter }}
```

Which will overlay the logo in the top left corner (x=50, y=50) of `$img`.

Fixes #8057
Fixes #4595
Updates #6731
@github-actions
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 25, 2022
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