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.
This commit is contained in:
Régis Hanol 2019-10-04 16:22:57 +02:00
parent f5d391a48a
commit 46d12c5ad3

View File

@ -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?