mirror of
https://github.com/discourse/discourse.git
synced 2025-02-17 01:42:46 +08:00
FIX: Use ImageMagick to detect animated images (#11702)
This is a fallback when FastImage cannot be used (animated WEBP images).
This commit is contained in:
parent
26337408a9
commit
74b95c88ac
|
@ -126,7 +126,7 @@ class UploadCreator
|
||||||
if is_image
|
if is_image
|
||||||
@upload.thumbnail_width, @upload.thumbnail_height = ImageSizer.resize(*@image_info.size)
|
@upload.thumbnail_width, @upload.thumbnail_height = ImageSizer.resize(*@image_info.size)
|
||||||
@upload.width, @upload.height = @image_info.size
|
@upload.width, @upload.height = @image_info.size
|
||||||
@upload.animated = FastImage.animated?(@file)
|
@upload.animated = animated?
|
||||||
end
|
end
|
||||||
|
|
||||||
add_metadata!
|
add_metadata!
|
||||||
|
@ -267,13 +267,13 @@ class UploadCreator
|
||||||
end
|
end
|
||||||
|
|
||||||
def should_alter_quality?
|
def should_alter_quality?
|
||||||
return false if FastImage.animated?(@file)
|
return false if animated?
|
||||||
|
|
||||||
@upload.target_image_quality(@file.path, SiteSetting.recompress_original_jpg_quality).present?
|
@upload.target_image_quality(@file.path, SiteSetting.recompress_original_jpg_quality).present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def should_downsize?
|
def should_downsize?
|
||||||
max_image_size > 0 && filesize >= max_image_size && !FastImage.animated?(@file)
|
max_image_size > 0 && filesize >= max_image_size && !animated?
|
||||||
end
|
end
|
||||||
|
|
||||||
def downsize!
|
def downsize!
|
||||||
|
@ -351,7 +351,7 @@ class UploadCreator
|
||||||
end
|
end
|
||||||
|
|
||||||
def should_crop?
|
def should_crop?
|
||||||
return false if ['profile_background', 'card_background', 'custom_emoji'].include?(@opts[:type]) && FastImage.animated?(@file)
|
return false if ['profile_background', 'card_background', 'custom_emoji'].include?(@opts[:type]) && animated?
|
||||||
|
|
||||||
TYPES_TO_CROP.include?(@opts[:type])
|
TYPES_TO_CROP.include?(@opts[:type])
|
||||||
end
|
end
|
||||||
|
@ -427,4 +427,30 @@ class UploadCreator
|
||||||
@upload.secure = UploadSecurity.new(@upload, @opts).should_be_secure?
|
@upload.secure = UploadSecurity.new(@upload, @opts).should_be_secure?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def animated?
|
||||||
|
return @animated if @animated != nil
|
||||||
|
|
||||||
|
@animated ||= begin
|
||||||
|
is_animated = FastImage.animated?(@file)
|
||||||
|
type = @image_info.type.to_s
|
||||||
|
|
||||||
|
if is_animated != nil
|
||||||
|
# FastImage will return nil if it cannot determine if animated
|
||||||
|
is_animated
|
||||||
|
elsif type == "gif" || type == "webp"
|
||||||
|
# Only GIFs, WEBPs and a few other unsupported image types can be animated
|
||||||
|
OptimizedImage.ensure_safe_paths!(@file.path)
|
||||||
|
|
||||||
|
command = ["identify", "-format", "%n\\n", @file.path]
|
||||||
|
frames = Discourse::Utils.execute_command(*command).to_i rescue 1
|
||||||
|
|
||||||
|
frames > 1
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
BIN
spec/fixtures/images/animated.webp
vendored
Normal file
BIN
spec/fixtures/images/animated.webp
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 519 KiB |
|
@ -137,6 +137,9 @@ RSpec.describe UploadCreator do
|
||||||
let(:animated_filename) { "animated.gif" }
|
let(:animated_filename) { "animated.gif" }
|
||||||
let(:animated_file) { file_from_fixtures(animated_filename) }
|
let(:animated_file) { file_from_fixtures(animated_filename) }
|
||||||
|
|
||||||
|
let(:animated_webp_filename) { "animated.webp" }
|
||||||
|
let(:animated_webp_file) { file_from_fixtures(animated_webp_filename) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
SiteSetting.png_to_jpg_quality = 1
|
SiteSetting.png_to_jpg_quality = 1
|
||||||
end
|
end
|
||||||
|
@ -208,6 +211,20 @@ RSpec.describe UploadCreator do
|
||||||
expect(File.extname(upload.url)).to eq('.gif')
|
expect(File.extname(upload.url)).to eq('.gif')
|
||||||
expect(upload.original_filename).to eq('animated.gif')
|
expect(upload.original_filename).to eq('animated.gif')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should not convert animated WEBP images' do
|
||||||
|
expect do
|
||||||
|
UploadCreator.new(animated_webp_file, animated_webp_filename,
|
||||||
|
force_optimize: true
|
||||||
|
).create_for(user.id)
|
||||||
|
end.to change { Upload.count }.by(1)
|
||||||
|
|
||||||
|
upload = Upload.last
|
||||||
|
|
||||||
|
expect(upload.extension).to eq('webp')
|
||||||
|
expect(File.extname(upload.url)).to eq('.webp')
|
||||||
|
expect(upload.original_filename).to eq('animated.webp')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user