Skip to content

Commit

Permalink
FIX: don't butcher GIFs
Browse files Browse the repository at this point in the history
Use 'gifsicle' instead of 'convert' to resize & optimize GIFs

FIX: don't even try to fix GIFs orientation
FIX: use 'allow_animated_thumbnails' site setting for user profile backgrounds & user cards
  • Loading branch information
ZogStriP committed Jul 22, 2015
1 parent ccdcca5 commit d456460
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
23 changes: 9 additions & 14 deletions app/models/optimized_image.rb
Expand Up @@ -99,6 +99,7 @@ def local?
def self.resize_instructions(from, to, dimensions, opts={})
# NOTE: ORDER is important!
%W{
convert
#{from}[0]
-gravity center
-background transparent
Expand All @@ -113,17 +114,18 @@ def self.resize_instructions(from, to, dimensions, opts={})

def self.resize_instructions_animated(from, to, dimensions, opts={})
%W{
gifsicle
#{from}
-coalesce
-gravity center
-thumbnail #{dimensions}^
-extent #{dimensions}
#{to}
--colors=256
--resize-fit #{dimensions}
--optimize=3
--output #{to}
}
end

def self.downsize_instructions(from, to, dimensions, opts={})
%W{
convert
#{from}[0]
-gravity center
-background transparent
Expand All @@ -133,14 +135,7 @@ def self.downsize_instructions(from, to, dimensions, opts={})
end

def self.downsize_instructions_animated(from, to, dimensions, opts={})
%W{
#{from}
-coalesce
-gravity center
-background transparent
-resize #{dimensions}#{!!opts[:force_aspect_ratio] ? "\\!" : "\\>"}
#{to}
}
resize_instructions_animated(from, to, dimensions, opts)
end

def self.resize(from, to, width, height, opts={})
Expand All @@ -164,7 +159,7 @@ def self.dimensions(width, height)
end

def self.convert_with(instructions, to)
`convert #{instructions.join(" ")} &> /dev/null`
`#{instructions.join(" ")} &> /dev/null`
return false if $?.exitstatus != 0

ImageOptim.new.optimize_image!(to)
Expand Down
6 changes: 3 additions & 3 deletions app/models/upload.rb
Expand Up @@ -65,8 +65,8 @@ def self.create_for(user_id, file, filename, filesize, options = {})
w = svg["width"].to_i
h = svg["height"].to_i
else
# fix orientation first
fix_image_orientation(file.path)
# fix orientation first (but not for GIFs)
fix_image_orientation(file.path) unless filename =~ /\.GIF$/i
# retrieve image info
image_info = FastImage.new(file) rescue nil
w, h = *(image_info.try(:size) || [0, 0])
Expand All @@ -80,7 +80,7 @@ def self.create_for(user_id, file, filename, filesize, options = {})

# crop images depending on their type
if CROPPED_IMAGE_TYPES.include?(options[:image_type])
allow_animation = false
allow_animation = SiteSetting.allow_animated_thumbnails
max_pixel_ratio = Discourse::PIXEL_RATIOS.max

case options[:image_type]
Expand Down

1 comment on commit d456460

@discoursebot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/uploaded-animated-gifs-of-4mb-or-larger-appear-very-small/147107/13

Please sign in to comment.