From 46d12c5ad3e8116bdc8e1662e55a4e63ed32dfb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Fri, 4 Oct 2019 16:22:57 +0200 Subject: [PATCH] FIX: properly downsize image on upload Overwriting the same file with 'convert' is not always working as expected. Adding a temporary file as the destination of the downsize makes this operation much more reliable. Also switched to using (the more aggressive) 50% resize instead of halving the number of pixels. --- lib/upload_creator.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb index 54abb69b151..632d7a7da07 100644 --- a/lib/upload_creator.rb +++ b/lib/upload_creator.rb @@ -197,6 +197,7 @@ class UploadCreator keep_jpeg &&= (filesize - new_size) > MIN_CONVERT_TO_JPEG_BYTES_SAVED if keep_jpeg + @file.close @file = jpeg_tempfile extract_image_info! else @@ -227,17 +228,20 @@ class UploadCreator def downsize! 3.times do original_size = filesize - downsized_pixels = [pixels, max_image_pixels].min / 2 + down_tempfile = Tempfile.new(["down", ".#{@image_info.type}"]) OptimizedImage.downsize( @file.path, - @file.path, - "#{downsized_pixels}@", + down_tempfile.path, + "50%", filename: @filename, allow_animation: allow_animation, raise_on_error: true ) + @file.close + @file = down_tempfile + extract_image_info! return if filesize >= original_size || pixels == 0 || !should_downsize?