diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb index 20e886f987b172..ad3c025fa00c49 100644 --- a/app/models/optimized_image.rb +++ b/app/models/optimized_image.rb @@ -115,6 +115,7 @@ def self.resize_instructions(from, to, dimensions, opts={}) %W{ convert #{from}[0] + -auto-orient -gravity center -background transparent -thumbnail #{dimensions}^ @@ -146,6 +147,7 @@ def self.crop_instructions(from, to, dimensions, opts={}) %W{ convert #{from}[0] + -auto-orient -gravity north -background transparent -thumbnail #{opts[:width]} @@ -176,6 +178,7 @@ def self.downsize_instructions(from, to, dimensions, opts={}) %W{ convert #{from}[0] + -auto-orient -gravity center -background transparent -resize #{dimensions} diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb index a00b13f08fc2e2..ee387bd857c038 100644 --- a/lib/upload_creator.rb +++ b/lib/upload_creator.rb @@ -44,7 +44,6 @@ def create_for(user_id) return @upload if is_still_too_big? - fix_orientation! if should_fix_orientation? crop! if should_crop? optimize! if should_optimize? end @@ -136,6 +135,7 @@ def convert_to_jpeg! OptimizedImage.ensure_safe_paths!(@file.path, jpeg_tempfile.path) Discourse::Utils.execute_command( 'convert', @file.path, + '-auto-orient', '-background', 'white', '-flatten', '-quality', SiteSetting.png_to_jpg_quality.to_s, @@ -144,10 +144,10 @@ def convert_to_jpeg! # keep the JPEG if it's at least 15% smaller if File.size(jpeg_tempfile.path) < filesize * 0.85 - @image_info = FastImage.new(jpeg_tempfile) @file = jpeg_tempfile @filename = (File.basename(@filename, ".*").presence || I18n.t("image").presence || "image") + ".jpg" @opts[:content_type] = "image/jpeg" + extract_image_info! else jpeg_tempfile.close! rescue nil end @@ -208,17 +208,8 @@ def crop! when "custom_emoji" OptimizedImage.downsize(@file.path, @file.path, "100x100\\>", filename: @filename, allow_animation: allow_animation) end - end - def should_fix_orientation? - # orientation is between 1 and 8, 1 being the default - # cf. http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/ - @image_info.orientation.to_i > 1 - end - - def fix_orientation! - OptimizedImage.ensure_safe_paths!(@file.path) - Discourse::Utils.execute_command('convert', @file.path, '-auto-orient', @file.path) + extract_image_info! end def should_optimize? @@ -234,6 +225,7 @@ def should_optimize? def optimize! OptimizedImage.ensure_safe_paths!(@file.path) ImageOptim.new.optimize_image!(@file.path) + extract_image_info! rescue ImageOptim::Worker::TimeoutExceeded Rails.logger.warn("ImageOptim timed out while optimizing #{@filename}") end