diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb
index 07017714b4c..d1251784ffd 100644
--- a/app/controllers/uploads_controller.rb
+++ b/app/controllers/uploads_controller.rb
@@ -67,9 +67,10 @@ class UploadsController < ApplicationController
 
       # allow users to upload large images that will be automatically reduced to allowed size
       if SiteSetting.max_image_size_kb > 0 && FileHelper.is_image?(filename) && File.size(tempfile.path) > 0
+        allow_animation = type == "avatar" ? SiteSetting.allow_animated_avatars : SiteSetting.allow_animated_thumbnails
         attempt = 5
         while attempt > 0 && File.size(tempfile.path) > SiteSetting.max_image_size_kb.kilobytes
-          OptimizedImage.downsize(tempfile.path, tempfile.path, "80%", allow_animation: SiteSetting.allow_animated_thumbnails)
+          OptimizedImage.downsize(tempfile.path, tempfile.path, "80%", filename: filename, allow_animation: allow_animation)
           attempt -= 1
         end
       end
diff --git a/app/controllers/user_avatars_controller.rb b/app/controllers/user_avatars_controller.rb
index 26664810702..1ff19e3ccfc 100644
--- a/app/controllers/user_avatars_controller.rb
+++ b/app/controllers/user_avatars_controller.rb
@@ -108,7 +108,8 @@ class UserAvatarsController < ApplicationController
       upload,
       size,
       size,
-      allow_animation: SiteSetting.allow_animated_avatars
+      filename: upload.original_filename,
+      allow_animation: SiteSetting.allow_animated_avatars,
     )
   end
 
diff --git a/app/jobs/regular/create_thumbnails.rb b/app/jobs/regular/create_thumbnails.rb
index 742d74877f6..84285333d86 100644
--- a/app/jobs/regular/create_thumbnails.rb
+++ b/app/jobs/regular/create_thumbnails.rb
@@ -22,7 +22,13 @@ module Jobs
 
     def create_thumbnails_for_avatar(upload, user)
       Discourse.avatar_sizes.each do |size|
-        OptimizedImage.create_for(upload, size, size, allow_animation: SiteSetting.allow_animated_avatars)
+        OptimizedImage.create_for(
+          upload,
+          size,
+          size,
+          filename: upload.original_filename,
+          allow_animation: SiteSetting.allow_animated_avatars
+        )
       end
     end
 
diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb
index a4bcc6112e4..c7ba61b10b6 100644
--- a/app/models/optimized_image.rb
+++ b/app/models/optimized_image.rb
@@ -152,7 +152,9 @@ class OptimizedImage < ActiveRecord::Base
 
   def self.optimize(operation, from, to, dimensions, opts={})
     method_name = "#{operation}_instructions"
-    method_name += "_animated" if !!opts[:allow_animation] && from =~ /\.GIF$/i
+    if !!opts[:allow_animation] && (from =~ /\.GIF$/i || opts[:filename] =~ /\.GIF$/i)
+      method_name += "_animated"
+    end
     instructions = self.send(method_name.to_sym, from, to, dimensions, opts)
     convert_with(instructions, to)
   end
diff --git a/app/models/upload.rb b/app/models/upload.rb
index bcc468d4450..928455e5fc5 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -29,7 +29,15 @@ class Upload < ActiveRecord::Base
 
   def create_thumbnail!(width, height)
     return unless SiteSetting.create_thumbnails?
-    thumbnail = OptimizedImage.create_for(self, width, height, allow_animation: SiteSetting.allow_animated_thumbnails)
+
+    thumbnail = OptimizedImage.create_for(
+      self,
+      width,
+      height,
+      filename: self.original_filename,
+      allow_animation: SiteSetting.allow_animated_thumbnails
+    )
+
     if thumbnail
       optimized_images << thumbnail
       self.width = width
@@ -95,7 +103,7 @@ class Upload < ActiveRecord::Base
             width, height = ImageSizer.resize(w, h, max_width: max_width, max_height: max_width)
           end
 
-          OptimizedImage.resize(file.path, file.path, width, height, allow_animation: allow_animation)
+          OptimizedImage.resize(file.path, file.path, width, height, filename: filename, allow_animation: allow_animation)
         end
 
         # optimize image