Retry PNG to JPG conversion with debug enabled on failure

This commit is contained in:
Gerhard Schlager 2017-12-19 11:31:18 +01:00
parent 9472821331
commit b47b378cb6

View File

@ -134,15 +134,13 @@ class UploadCreator
jpeg_tempfile = Tempfile.new(["image", ".jpg"]) jpeg_tempfile = Tempfile.new(["image", ".jpg"])
OptimizedImage.ensure_safe_paths!(@file.path, jpeg_tempfile.path) OptimizedImage.ensure_safe_paths!(@file.path, jpeg_tempfile.path)
Discourse::Utils.execute_command(
'convert', @file.path, begin
'-auto-orient', execute_convert(@file, jpeg_tempfile)
'-background', 'white', rescue
'-interlace', 'none', # retry with debugging enabled
'-flatten', execute_convert(@file, jpeg_tempfile, true)
'-quality', SiteSetting.png_to_jpg_quality.to_s, end
jpeg_tempfile.path
)
# 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
@ -155,6 +153,19 @@ class UploadCreator
end end
end end
def execute_convert(input_file, output_file, debug = false)
command = ['convert', input_file.path,
'-auto-orient',
'-background', 'white',
'-interlace', 'none',
'-flatten',
'-quality', SiteSetting.png_to_jpg_quality.to_s]
command << '-debug' << 'all' if debug
command << output_file.path
Discourse::Utils.execute_command(*command, failure_message: "failed to convert png to jpg")
end
def should_downsize? def should_downsize?
max_image_size > 0 && filesize >= max_image_size max_image_size > 0 && filesize >= max_image_size
end end