fix and improve image downsizing algorithm

This commit is contained in:
Régis Hanol 2016-06-20 12:35:07 +02:00
parent 6416ea9e09
commit 7fca6f502f

View file

@ -51,8 +51,6 @@ class UploadsController < ApplicationController
render nothing: true, status: 404 render nothing: true, status: 404
end end
DOWNSIZE_RATIO ||= 0.8
def create_upload(type, file, url) def create_upload(type, file, url)
begin begin
maximum_upload_size = [SiteSetting.max_image_size_kb, SiteSetting.max_attachment_size_kb].max.kilobytes maximum_upload_size = [SiteSetting.max_image_size_kb, SiteSetting.max_attachment_size_kb].max.kilobytes
@ -72,20 +70,20 @@ class UploadsController < ApplicationController
return { errors: I18n.t("upload.file_missing") } if tempfile.nil? return { errors: I18n.t("upload.file_missing") } if tempfile.nil?
# allow users to upload (not that) large images that will be automatically reduced to allowed size # allow users to upload large images that will be automatically reduced to allowed size
max_image_size_kb = SiteSetting.max_image_size_kb.kilobytes max_image_size_kb = SiteSetting.max_image_size_kb.kilobytes
if max_image_size_kb > 0 && FileHelper.is_image?(filename) if max_image_size_kb > 0 && FileHelper.is_image?(filename)
uploaded_size = File.size(tempfile.path) if File.size(tempfile.path) >= max_image_size_kb && Upload.should_optimize?(tempfile.path)
if 0 < uploaded_size && uploaded_size < maximum_upload_size && Upload.should_optimize?(tempfile.path)
attempt = 2 attempt = 2
allow_animation = type == "avatar" ? SiteSetting.allow_animated_avatars : SiteSetting.allow_animated_thumbnails allow_animation = type == "avatar" ? SiteSetting.allow_animated_avatars : SiteSetting.allow_animated_thumbnails
while attempt > 0 while attempt > 0
downsized_size = File.size(tempfile.path) downsized_size = File.size(tempfile.path)
break if downsized_size >= uploaded_size || downsized_size < max_image_size_kb break if downsized_size < max_image_size_kb
image_info = FastImage.new(tempfile.path) rescue nil image_info = FastImage.new(tempfile.path) rescue nil
w, h = *(image_info.try(:size) || [0, 0]) w, h = *(image_info.try(:size) || [0, 0])
break if w == 0 || h == 0 break if w == 0 || h == 0
dimensions = "#{(w * DOWNSIZE_RATIO).floor}x#{(h * DOWNSIZE_RATIO).floor}" downsize_ratio = best_downsize_ratio(downsized_size, max_image_size_kb)
dimensions = "#{(w * downsize_ratio).floor}x#{(h * downsize_ratio).floor}"
OptimizedImage.downsize(tempfile.path, tempfile.path, dimensions, filename: filename, allow_animation: allow_animation) OptimizedImage.downsize(tempfile.path, tempfile.path, dimensions, filename: filename, allow_animation: allow_animation)
attempt -= 1 attempt -= 1
end end
@ -109,4 +107,14 @@ class UploadsController < ApplicationController
end end
end end
def best_downsize_ratio(downsized_size, max_image_size)
if downsized_size / 9 > max_image_size
0.3
elsif downsized_size / 3 > max_image_size
0.6
else
0.8
end
end
end end