Skip to content

Commit

Permalink
Picture Ingredient: Fix NaN error with free height
Browse files Browse the repository at this point in the history
When indicating that a picture can be cropped, but needs to have a
certain height or width but leaves the other dimension free, current
Alchemy throws a `NaN` error because of float division by Zero.

This commit handles this case by returning nil if the calculated mask is
Not a Number.

(cherry picked from commit 1fc477a)
  • Loading branch information
mamhoff committed Sep 18, 2023
1 parent 7d396cd commit c840f53
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/concerns/alchemy/picture_thumbnails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def default_crop_size
return nil unless settings[:crop] && settings[:size]

mask = inferred_dimensions_from_string(settings[:size])
return if mask.nil?

zoom = thumbnail_zoom_factor(mask)
return nil if zoom.zero?

Expand Down Expand Up @@ -150,6 +152,8 @@ def inferred_dimensions_from_string(string)
width, height = dimensions_from_string(string)
ratio = image_file_width.to_f / image_file_height.to_i

return if ratio.nan?

if width.zero? && ratio.is_a?(Float)
width = height * ratio
end
Expand Down
20 changes: 20 additions & 0 deletions lib/alchemy/test_support/having_picture_thumbnails_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,26 @@
size: "160x120"
)
end

context "with settings indicating free height" do
let(:settings) do
{
crop: true,
size: "800x"
}
end

it "returns default thumbnail options" do
is_expected.to eq(
crop: true,
crop_from: nil,
crop_size: nil,
flatten: true,
format: "jpg",
size: "160x120"
)
end
end
end
end

Expand Down

0 comments on commit c840f53

Please sign in to comment.