diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index d1251784ffd..ad5b551a051 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -67,10 +67,14 @@ class UploadsController < ApplicationController # allow users to upload large images that will be automatically reduced to allowed size if SiteSetting.max_image_size_kb > 0 && FileHelper.is_image?(filename) && File.size(tempfile.path) > 0 + attempt = 2 allow_animation = type == "avatar" ? SiteSetting.allow_animated_avatars : SiteSetting.allow_animated_thumbnails - attempt = 5 while attempt > 0 && File.size(tempfile.path) > SiteSetting.max_image_size_kb.kilobytes - OptimizedImage.downsize(tempfile.path, tempfile.path, "80%", filename: filename, allow_animation: allow_animation) + image_info = FastImage.new(tempfile.path) rescue nil + w, h = *(image_info.try(:size) || [0, 0]) + break if w == 0 || h == 0 + dimensions = "#{(w * 0.8).floor}x#{(h * 0.8).floor}" + OptimizedImage.downsize(tempfile.path, tempfile.path, dimensions, filename: filename, allow_animation: allow_animation) attempt -= 1 end end