media file: fix overly large thumbnails for animated GIFs.

Fix regression in ef2857667 that caused animated GIFs and PNGs to
generate thumbnails that were larger than 150x150.

Also fix a bug with cropped previews not being generated for animated
GIFs and PNGs.
This commit is contained in:
evazion
2021-09-05 07:43:31 -05:00
parent 540a3e111a
commit 13f98c02e3
4 changed files with 19 additions and 15 deletions

View File

@@ -38,26 +38,28 @@ class MediaFile::Image < MediaFile
# @see https://github.com/jcupitt/libvips/wiki/HOWTO----Image-shrinking
# @see http://jcupitt.github.io/libvips/API/current/Using-vipsthumbnail.md.html
def preview(width, height)
if is_animated?
FFmpeg.new(file).smart_video_preview
else
output_file = Tempfile.new(["image-preview", ".jpg"])
resized_image = image.thumbnail_image(width, height: height, **THUMBNAIL_OPTIONS)
resized_image.jpegsave(output_file.path, **JPEG_OPTIONS)
MediaFile::Image.new(output_file)
end
end
def crop(width, height)
output_file = Tempfile.new(["image-crop", ".jpg"])
resized_image = image.thumbnail_image(width, height: height, **CROP_OPTIONS)
output_file = Tempfile.new(["image-preview", ".jpg"])
resized_image = preview_frame.image.thumbnail_image(width, height: height, **THUMBNAIL_OPTIONS)
resized_image.jpegsave(output_file.path, **JPEG_OPTIONS)
MediaFile::Image.new(output_file)
end
private
def crop(width, height)
output_file = Tempfile.new(["image-crop", ".jpg"])
resized_image = preview_frame.image.thumbnail_image(width, height: height, **CROP_OPTIONS)
resized_image.jpegsave(output_file.path, **JPEG_OPTIONS)
MediaFile::Image.new(output_file)
end
def preview_frame
if is_animated?
FFmpeg.new(file).smart_video_preview
else
self
end
end
def is_animated_gif?
file_ext == :gif && image.get("n-pages") > 1

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -124,6 +124,8 @@ class MediaFileTest < ActiveSupport::TestCase
should "generate a preview image for an animated image" do
skip unless MediaFile.videos_enabled?
assert_equal([86, 52], MediaFile.open("test/files/test-animated-86x52.gif").preview(150, 150).dimensions)
assert_equal([150, 105], MediaFile.open("test/files/test-animated-400x281.gif").preview(150, 150).dimensions)
assert_equal([150, 150], MediaFile.open("test/files/test-animated-256x256.png").preview(150, 150).dimensions)
assert_equal([150, 150], MediaFile.open("test/files/apng/normal_apng.png").preview(150, 150).dimensions)
end