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:
@@ -38,26 +38,28 @@ class MediaFile::Image < MediaFile
|
|||||||
# @see https://github.com/jcupitt/libvips/wiki/HOWTO----Image-shrinking
|
# @see https://github.com/jcupitt/libvips/wiki/HOWTO----Image-shrinking
|
||||||
# @see http://jcupitt.github.io/libvips/API/current/Using-vipsthumbnail.md.html
|
# @see http://jcupitt.github.io/libvips/API/current/Using-vipsthumbnail.md.html
|
||||||
def preview(width, height)
|
def preview(width, height)
|
||||||
if is_animated?
|
output_file = Tempfile.new(["image-preview", ".jpg"])
|
||||||
FFmpeg.new(file).smart_video_preview
|
resized_image = preview_frame.image.thumbnail_image(width, height: height, **THUMBNAIL_OPTIONS)
|
||||||
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)
|
|
||||||
resized_image.jpegsave(output_file.path, **JPEG_OPTIONS)
|
resized_image.jpegsave(output_file.path, **JPEG_OPTIONS)
|
||||||
|
|
||||||
MediaFile::Image.new(output_file)
|
MediaFile::Image.new(output_file)
|
||||||
end
|
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?
|
def is_animated_gif?
|
||||||
file_ext == :gif && image.get("n-pages") > 1
|
file_ext == :gif && image.get("n-pages") > 1
|
||||||
|
|||||||
BIN
test/files/test-animated-256x256.png
Normal file
BIN
test/files/test-animated-256x256.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
BIN
test/files/test-animated-400x281.gif
Normal file
BIN
test/files/test-animated-400x281.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
@@ -124,6 +124,8 @@ class MediaFileTest < ActiveSupport::TestCase
|
|||||||
should "generate a preview image for an animated image" do
|
should "generate a preview image for an animated image" do
|
||||||
skip unless MediaFile.videos_enabled?
|
skip unless MediaFile.videos_enabled?
|
||||||
assert_equal([86, 52], MediaFile.open("test/files/test-animated-86x52.gif").preview(150, 150).dimensions)
|
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)
|
assert_equal([150, 150], MediaFile.open("test/files/apng/normal_apng.png").preview(150, 150).dimensions)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user