]> cat aescling's git repositories - mastodon.git/commitdiff
Force convert to JPG for preview card thumbnails to avoid animations (#7109)
authorEugen Rochko <eugen@zeonfederated.com>
Sat, 21 Apr 2018 19:34:36 +0000 (21:34 +0200)
committerGitHub <noreply@github.com>
Sat, 21 Apr 2018 19:34:36 +0000 (21:34 +0200)
* Force convert to JPG for preview card thumbnails to avoid animations

Fix #7093

* Conditionally convert to JPG only if original is GIF
Coalesce and strip on all formats to ensure no animated APNGs

app/models/preview_card.rb

index 0c82f06ce0e7d607006d1be82da05e85aa8ee439..0ffa6b10fd2aec60ef40d7d465d77f9969de5fee 100644 (file)
@@ -34,7 +34,7 @@ class PreviewCard < ApplicationRecord
 
   has_and_belongs_to_many :statuses
 
-  has_attached_file :image, styles: { original: { geometry: '400x400>', file_geometry_parser: FastGeometryParser } }, convert_options: { all: '-quality 80 -strip' }
+  has_attached_file :image, styles: ->(f) { image_styles(f) }, convert_options: { all: '-quality 80 -strip' }
 
   include Attachmentable
 
@@ -52,6 +52,23 @@ class PreviewCard < ApplicationRecord
     save!
   end
 
+  class << self
+    private
+
+    def image_styles(f)
+      styles = {
+        original: {
+          geometry: '400x400>',
+          file_geometry_parser: FastGeometryParser,
+          convert_options: '-coalesce -strip',
+        },
+      }
+
+      styles[:original][:format] = 'jpg' if f.instance.image_content_type == 'image/gif'
+      styles
+    end
+  end
+
   private
 
   def extract_dimensions