Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/stable' into pkgr
Browse files Browse the repository at this point in the history
  • Loading branch information
crohr committed Jun 23, 2017
2 parents 14efd80 + 04bc75b commit 198bfa5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
3 changes: 3 additions & 0 deletions app/models/optimized_image.rb
Expand Up @@ -115,6 +115,7 @@ def self.resize_instructions(from, to, dimensions, opts={})
%W{
convert
#{from}[0]
-auto-orient
-gravity center
-background transparent
-thumbnail #{dimensions}^
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -176,6 +178,7 @@ def self.downsize_instructions(from, to, dimensions, opts={})
%W{
convert
#{from}[0]
-auto-orient
-gravity center
-background transparent
-resize #{dimensions}
Expand Down
16 changes: 4 additions & 12 deletions lib/upload_creator.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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?
Expand All @@ -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
Expand Down

0 comments on commit 198bfa5

Please sign in to comment.