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"])
OptimizedImage.ensure_safe_paths!(@file.path, jpeg_tempfile.path)
Discourse::Utils.execute_command(
'convert', @file.path,
'-auto-orient',
'-background', 'white',
'-interlace', 'none',
'-flatten',
'-quality', SiteSetting.png_to_jpg_quality.to_s,
jpeg_tempfile.path
)
begin
execute_convert(@file, jpeg_tempfile)
rescue
# retry with debugging enabled
execute_convert(@file, jpeg_tempfile, true)
end
# keep the JPEG if it's at least 15% smaller
if File.size(jpeg_tempfile.path) < filesize * 0.85
@ -155,6 +153,19 @@ class UploadCreator
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?
max_image_size > 0 && filesize >= max_image_size
end