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

images: Opacity problem with Resize + Padding with PNG output #12536

Open
rnwst opened this issue May 24, 2024 · 4 comments
Open

images: Opacity problem with Resize + Padding with PNG output #12536

rnwst opened this issue May 24, 2024 · 4 comments
Labels
Milestone

Comments

@rnwst
Copy link

rnwst commented May 24, 2024

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

$ hugo version
hugo v0.126.1+extended linux/amd64

Does this issue reproduce with the latest release?

Yes.

Report

The images.Padding function currently ignores the transparency setting when padding a PNG image. While the documentation does state 'conversion to WEBP is required to support transparency' (under Other recipes), I can't see a reason why PNG shouldn't be supported, given that it has an alpha channel. For the moment I have resorted to first converting the image to WEBP, then applying transparent padding, and then converting it back to PNG, but this does not feel like an elegant solution.

@rnwst
Copy link
Author

rnwst commented May 24, 2024

It may be worth noting that the images.Overlay function does not require conversion to WEBP and works fine when overlaying two transparent PNGs.

@jmooring
Copy link
Member

jmooring commented May 24, 2024

This works as expected:

{{ with resources.Get "a.png" }}
  {{ $filter := images.Padding 20 "#ff000055" }}
  {{ with . | images.Filter $filter }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

So you must be doing something different. Please provide example.

Here's a minimal example that does not reproduce the problem:

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

@rnwst
Copy link
Author

rnwst commented May 24, 2024

Many thanks for putting together the minimal example, that's awesome!

I spent the last 3 hours trying to work out what's going on. While I still have no idea why this is happening, I submitted a PR which hopefully includes some examples that don't work as expected: jmooring/hugo-testing#10

Results seem to be dependent on what size the image is resized to. Problems also seem to go away when using the WEBP format instead of PNG.

@jmooring
Copy link
Member

jmooring commented May 24, 2024

This works as expected (border is ~ 50% opaque blue):

{{ $i := resources.Get "a.jpg" }}
{{ $filters := slice
  (images.Process "resize x200 png")
  (images.Padding 20 "#00f7")
}}
{{ $i = images.Filter $filters $i }}
<img src="{{ $i.RelPermalink }}" width="{{ $i.Width }}" height="{{ $i.Height }}" alt="">

This does not work as expected (border is some other transparent color):

{{ $i := resources.Get "a.jpg" }}
{{ $i = $i.Resize "x200 png" }}
{{ $filters := slice
  (images.Padding 20 "#00f7")
}}
{{ $i = images.Filter $filters $i }}
<img src="{{ $i.RelPermalink }}" width="{{ $i.Width }}" height="{{ $i.Height }}" alt="">

But this (convert to webp using second construct) works fine:

{{ $i := resources.Get "images/a.jpg" }}
{{ $i = $i.Resize "x200 webp" }}
{{ $filters := slice
  (images.Padding 20 "#00f7")
}}
{{ $i = images.Filter $filters $i }}
<img src="{{ $i.RelPermalink }}" width="{{ $i.Width }}" height="{{ $i.Height }}" alt="">

So, for now, use the first construct.

Example:

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

@jmooring jmooring changed the title images.Padding does not support transparency when using PNG format images: Opacity problem with Resize + Padding with PNG output May 24, 2024
@bep bep added this to the v0.131.0 milestone Jul 22, 2024
@bep bep modified the milestones: v0.131.0, v0.133.0 Aug 9, 2024
@bep bep modified the milestones: v0.133.0, Unscheduled Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants