Skip to content

Commit

Permalink
Fix: add alpha premultiplication steps before and after resize (#302)
Browse files Browse the repository at this point in the history
* Add alpha premultiplication steps before and after resize

* Only premultiply if image has alpha

* Fix transparent images not being resized, add test
  • Loading branch information
KaarlisCaune committed Feb 19, 2024
1 parent d3efe34 commit f393067
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/jekyll_picture_tag/images/image_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ def notify
end

def resize(image)
image.resize(scale_value)
if image.has_alpha?
image = image.premultiply
image = image.resize(scale_value)
image.unpremultiply
else
image.resize(scale_value)
end
end

def crop(image)
Expand Down
Binary file added test/image_files/pestka_transparent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions test/unit/images/test_image_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ def test_resize
assert_equal(10, width)
end

def test_resize_with_alpha
restub_source('pestka_transparent', 'png')
tested

width = Vips::Image.new_from_file(filename).width

assert_equal(10, width)
end

def test_quality
# We have to use slightly larger images for the quality to make a difference
# in file size.
Expand Down

0 comments on commit f393067

Please sign in to comment.