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?