MediaFile: fix thumbnail dimension calculation.

Calculate the dimensions of thumbnails ourselves instead of letting
libvips calculate them for us. This way we know the exact size of
thumbnails, so we can set the right width and height for <img> tags. If
we let libvips calculate thumbnail sizes for us, then we can't predict
the exact size of thumbnails, because sometimes libvips rounds numbers
differently than us.
This commit is contained in:
evazion
2021-11-30 04:17:17 -06:00
parent c2e6202da6
commit e5ba6d4afc
5 changed files with 65 additions and 14 deletions

View File

@@ -42,13 +42,16 @@ class PostPreviewComponent < ApplicationComponent
end
end
def preview_dimensions
downscale_ratio = Danbooru.config.small_image_width.to_f / [post.image_width, post.image_height].max
def preview_width
variant.width
end
{
width: [(downscale_ratio * post.image_width).floor, post.image_width].min,
height: [(downscale_ratio * post.image_height).floor, post.image_height].min,
}
def preview_height
variant.height
end
def variant
@variant ||= media_asset.variant(:preview)
end
def tooltip

View File

@@ -16,7 +16,7 @@
<%= tag.picture do -%>
<%= tag.source media: "(max-width: 660px)", srcset: cropped_url -%>
<%= tag.source media: "(min-width: 660px)", srcset: post.preview_file_url -%>
<%= tag.img class: "has-cropped-#{post.has_cropped?}", src: post.preview_file_url, style: "min-width: #{preview_dimensions[:width]}px; min-height: #{preview_dimensions[:height]}px;", title: tooltip, alt: "post ##{post.id}", crossorigin: "anonymous" -%>
<%= tag.img class: "has-cropped-#{post.has_cropped?}", src: post.preview_file_url, style: "min-width: #{preview_width}px; min-height: #{preview_height}px;", title: tooltip, alt: "post ##{post.id}", crossorigin: "anonymous" -%>
<% end -%>
<% end -%>
<% if pool -%>