Skip to content

A platform for community discussion. Free, open, simple.

Retry PNG to JPG conversion with debug enabled on failure

Gerhard Schlagerb47b378cb69d
diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb
index a0cdd75a922..75f4b3cff6e 100644
--- a/lib/upload_creator.rb
+++ b/lib/upload_creator.rb
@@ -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