From 92e6446f0f5da7f0cfe98d9edac88b786c6e3325 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9gis=20Hanol?= <regis@hanol.fr>
Date: Sat, 21 Feb 2015 18:37:37 +0100
Subject: [PATCH] FEATURE: new  site setting

---
 app/jobs/regular/resize_emoji.rb |  6 +++++-
 app/models/optimized_image.rb    | 25 ++++++++++++++-----------
 config/locales/server.en.yml     |  1 +
 config/site_settings.yml         |  2 ++
 4 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/app/jobs/regular/resize_emoji.rb b/app/jobs/regular/resize_emoji.rb
index 7e1b7f1a0c3..61ae0248c2e 100644
--- a/app/jobs/regular/resize_emoji.rb
+++ b/app/jobs/regular/resize_emoji.rb
@@ -6,8 +6,12 @@ module Jobs
       path = args[:path]
       return unless File.exists?(path)
 
+      opts = {
+        allow_animation: true,
+        force_aspect_ratio: SiteSetting.enforce_square_emoji
+      }
       # make sure emoji aren't too big
-      OptimizedImage.downsize(path, path, 60, 60, true)
+      OptimizedImage.downsize(path, path, 60, 60, opts)
     end
   end
 
diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb
index 4373c7318b3..f6cdde8c995 100644
--- a/app/models/optimized_image.rb
+++ b/app/models/optimized_image.rb
@@ -38,7 +38,7 @@ class OptimizedImage < ActiveRecord::Base
         FileUtils.cp(original_path, temp_path)
         resized = true
       else
-        resized = resize(original_path, temp_path, width, height, opts[:allow_animation])
+        resized = resize(original_path, temp_path, width, height, opts)
       end
 
       if resized
@@ -81,9 +81,9 @@ class OptimizedImage < ActiveRecord::Base
     end
   end
 
-  def self.resize_instructions(from, to, width, height, allow_animation=false)
+  def self.resize_instructions(from, to, width, height, opts={})
     # NOTE: ORDER is important!
-    if allow_animation && from =~ /\.GIF$/i
+    if !!opts[:allow_animation] && from =~ /\.GIF$/i
       %W{
         #{from}
         -coalesce
@@ -108,14 +108,17 @@ class OptimizedImage < ActiveRecord::Base
     end
   end
 
-  def self.downsize_instructions(from, to, max_width, max_height, allow_animation=false)
-    if allow_animation && from =~ /\.GIF$/i
+  def self.downsize_instructions(from, to, max_width, max_height, opts={})
+    dimensions = "#{max_width}x#{max_height}"
+    dimensions += !!opts[:force_aspect_ratio] ? "\\!" : "\\>"
+
+    if !!opts[:allow_animation] && from =~ /\.GIF$/i
       %W{
         #{from}
         -coalesce
         -gravity center
         -background transparent
-        -thumbnail #{max_width}x#{max_height}\\>
+        -thumbnail #{dimensions}
         -layers optimize
         #{to}
       }
@@ -124,19 +127,19 @@ class OptimizedImage < ActiveRecord::Base
         #{from}[0]
         -gravity center
         -background transparent
-        -thumbnail #{max_width}x#{max_height}\\>
+        -thumbnail #{dimensions}
         #{to}
       }
     end
   end
 
-  def self.resize(from, to, width, height, allow_animation=false)
-    instructions = resize_instructions(from, to, width, height, allow_animation)
+  def self.resize(from, to, width, height, opts={})
+    instructions = resize_instructions(from, to, width, height, opts)
     convert_and_optimize_with(instructions)
   end
 
-  def self.downsize(from, to, max_width, max_height, allow_animation=false)
-    instructions = downsize_instructions(from, to, max_width, max_height, allow_animation)
+  def self.downsize(from, to, max_width, max_height, opts={})
+    instructions = downsize_instructions(from, to, max_width, max_height, opts)
     convert_and_optimize_with(instructions)
   end
 
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 97a73223e9a..1da4af43207 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1127,6 +1127,7 @@ en:
 
     enable_emoji: "Enable emoji"
     emoji_set: "How would you like your emoji?"
+    enforce_square_emoji: "Force a square aspect ratio to all emojis."
 
     errors:
       invalid_email: "Invalid email address."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index bf05e249b4f..62dfba77e98 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -405,6 +405,8 @@ posting:
     default: 'emoji_one'
     client: true
     enum: 'EmojiSetSiteSetting'
+  enforce_square_emoji:
+    default: true
 
 email:
   email_time_window_mins: