Skip to content

Commit

Permalink
FIX - don't attempt to optimized animated images (#11031)
Browse files Browse the repository at this point in the history
* FIX - don't attempt to optimized animated images

* ensure_safe_paths before calling ImageMagick
  • Loading branch information
jbrw committed Oct 26, 2020
1 parent d9a5d56 commit aeb24bd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
23 changes: 22 additions & 1 deletion lib/upload_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def create_for(user_id)
if is_image
@upload.thumbnail_width, @upload.thumbnail_height = ImageSizer.resize(*@image_info.size)
@upload.width, @upload.height = @image_info.size
@upload.animated = FastImage.animated?(@file)
@upload.animated = animated?(@file)
end

add_metadata!
Expand Down Expand Up @@ -177,6 +177,25 @@ def extract_image_info!
end
end

def animated?(file)
OptimizedImage.ensure_safe_paths!(file.path)

# TODO - find out why:
# FastImage.animated?(@file)
# doesn't seem to identify all animated gifs
@frame_count ||= begin
command = [
"identify",
"-format", "%n\\n",
file.path
]

frames = Discourse::Utils.execute_command(*command).to_i rescue 1
end

@frame_count > 1
end

MIN_PIXELS_TO_CONVERT_TO_JPEG ||= 1280 * 720

def convert_png_to_jpeg?
Expand Down Expand Up @@ -267,6 +286,8 @@ def execute_convert(from, to, opts = {})
end

def should_alter_quality?
return false if animated?(@file)

@upload.target_image_quality(@file.path, SiteSetting.recompress_original_jpg_quality).present?
end

Expand Down
Binary file added spec/fixtures/images/animated.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 30 additions & 9 deletions spec/lib/upload_creator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def image_quality(path)
let(:small_filename) { "logo.png" }
let(:small_file) { file_from_fixtures(small_filename) }

let(:animated_filename) { "animated.gif" }
let(:animated_file) { file_from_fixtures(animated_filename) }

before do
SiteSetting.png_to_jpg_quality = 1
end
Expand Down Expand Up @@ -174,19 +177,37 @@ def image_quality(path)
expect(upload.original_filename).to eq('should_be_jpeg.jpg')
end

it 'should alter the image quality' do
SiteSetting.png_to_jpg_quality = 75
SiteSetting.recompress_original_jpg_quality = 40
SiteSetting.image_preview_jpg_quality = 10
context "jpeg image quality settings" do
before do
SiteSetting.png_to_jpg_quality = 75
SiteSetting.recompress_original_jpg_quality = 40
SiteSetting.image_preview_jpg_quality = 10
end

it 'should alter the image quality' do
upload = UploadCreator.new(file, filename, force_optimize: true).create_for(user.id)

upload = UploadCreator.new(file, filename, force_optimize: true).create_for(user.id)
expect(image_quality(upload.url)).to eq(SiteSetting.recompress_original_jpg_quality)

expect(image_quality(upload.url)).to eq(SiteSetting.recompress_original_jpg_quality)
upload.create_thumbnail!(100, 100)
upload.reload

upload.create_thumbnail!(100, 100)
upload.reload
expect(image_quality(upload.optimized_images.first.url)).to eq(SiteSetting.image_preview_jpg_quality)
end

expect(image_quality(upload.optimized_images.first.url)).to eq(SiteSetting.image_preview_jpg_quality)
it 'should not convert animated images' do
expect do
UploadCreator.new(animated_file, animated_filename,
force_optimize: true
).create_for(user.id)
end.to change { Upload.count }.by(1)

upload = Upload.last

expect(upload.extension).to eq('gif')
expect(File.extname(upload.url)).to eq('.gif')
expect(upload.original_filename).to eq('animated.gif')
end
end
end

Expand Down

0 comments on commit aeb24bd

Please sign in to comment.