mirror of
https://github.com/discourse/discourse.git
synced 2025-06-25 00:41:35 +08:00
FIX: image orientation wasn't properly working
This commit is contained in:
@ -115,6 +115,7 @@ class OptimizedImage < ActiveRecord::Base
|
|||||||
%W{
|
%W{
|
||||||
convert
|
convert
|
||||||
#{from}[0]
|
#{from}[0]
|
||||||
|
-auto-orient
|
||||||
-gravity center
|
-gravity center
|
||||||
-background transparent
|
-background transparent
|
||||||
-thumbnail #{dimensions}^
|
-thumbnail #{dimensions}^
|
||||||
@ -146,6 +147,7 @@ class OptimizedImage < ActiveRecord::Base
|
|||||||
%W{
|
%W{
|
||||||
convert
|
convert
|
||||||
#{from}[0]
|
#{from}[0]
|
||||||
|
-auto-orient
|
||||||
-gravity north
|
-gravity north
|
||||||
-background transparent
|
-background transparent
|
||||||
-thumbnail #{opts[:width]}
|
-thumbnail #{opts[:width]}
|
||||||
@ -176,6 +178,7 @@ class OptimizedImage < ActiveRecord::Base
|
|||||||
%W{
|
%W{
|
||||||
convert
|
convert
|
||||||
#{from}[0]
|
#{from}[0]
|
||||||
|
-auto-orient
|
||||||
-gravity center
|
-gravity center
|
||||||
-background transparent
|
-background transparent
|
||||||
-resize #{dimensions}
|
-resize #{dimensions}
|
||||||
|
@ -44,7 +44,6 @@ class UploadCreator
|
|||||||
|
|
||||||
return @upload if is_still_too_big?
|
return @upload if is_still_too_big?
|
||||||
|
|
||||||
fix_orientation! if should_fix_orientation?
|
|
||||||
crop! if should_crop?
|
crop! if should_crop?
|
||||||
optimize! if should_optimize?
|
optimize! if should_optimize?
|
||||||
end
|
end
|
||||||
@ -136,6 +135,7 @@ class UploadCreator
|
|||||||
OptimizedImage.ensure_safe_paths!(@file.path, jpeg_tempfile.path)
|
OptimizedImage.ensure_safe_paths!(@file.path, jpeg_tempfile.path)
|
||||||
Discourse::Utils.execute_command(
|
Discourse::Utils.execute_command(
|
||||||
'convert', @file.path,
|
'convert', @file.path,
|
||||||
|
'-auto-orient',
|
||||||
'-background', 'white',
|
'-background', 'white',
|
||||||
'-flatten',
|
'-flatten',
|
||||||
'-quality', SiteSetting.png_to_jpg_quality.to_s,
|
'-quality', SiteSetting.png_to_jpg_quality.to_s,
|
||||||
@ -144,10 +144,10 @@ class UploadCreator
|
|||||||
|
|
||||||
# keep the JPEG if it's at least 15% smaller
|
# keep the JPEG if it's at least 15% smaller
|
||||||
if File.size(jpeg_tempfile.path) < filesize * 0.85
|
if File.size(jpeg_tempfile.path) < filesize * 0.85
|
||||||
@image_info = FastImage.new(jpeg_tempfile)
|
|
||||||
@file = jpeg_tempfile
|
@file = jpeg_tempfile
|
||||||
@filename = (File.basename(@filename, ".*").presence || I18n.t("image").presence || "image") + ".jpg"
|
@filename = (File.basename(@filename, ".*").presence || I18n.t("image").presence || "image") + ".jpg"
|
||||||
@opts[:content_type] = "image/jpeg"
|
@opts[:content_type] = "image/jpeg"
|
||||||
|
extract_image_info!
|
||||||
else
|
else
|
||||||
jpeg_tempfile.close! rescue nil
|
jpeg_tempfile.close! rescue nil
|
||||||
end
|
end
|
||||||
@ -208,17 +208,8 @@ class UploadCreator
|
|||||||
when "custom_emoji"
|
when "custom_emoji"
|
||||||
OptimizedImage.downsize(@file.path, @file.path, "100x100\\>", filename: @filename, allow_animation: allow_animation)
|
OptimizedImage.downsize(@file.path, @file.path, "100x100\\>", filename: @filename, allow_animation: allow_animation)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def should_fix_orientation?
|
extract_image_info!
|
||||||
# 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)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def should_optimize?
|
def should_optimize?
|
||||||
@ -234,6 +225,7 @@ class UploadCreator
|
|||||||
def optimize!
|
def optimize!
|
||||||
OptimizedImage.ensure_safe_paths!(@file.path)
|
OptimizedImage.ensure_safe_paths!(@file.path)
|
||||||
ImageOptim.new.optimize_image!(@file.path)
|
ImageOptim.new.optimize_image!(@file.path)
|
||||||
|
extract_image_info!
|
||||||
rescue ImageOptim::Worker::TimeoutExceeded
|
rescue ImageOptim::Worker::TimeoutExceeded
|
||||||
Rails.logger.warn("ImageOptim timed out while optimizing #{@filename}")
|
Rails.logger.warn("ImageOptim timed out while optimizing #{@filename}")
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user