mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: don't try to optimize large PNGs (takes too much time)
This commit is contained in:
@@ -73,8 +73,8 @@ class Upload < ActiveRecord::Base
|
||||
w = svg["width"].to_i
|
||||
h = svg["height"].to_i
|
||||
else
|
||||
# fix orientation first (but not for GIFs)
|
||||
fix_image_orientation(file.path) unless filename =~ /\.GIF$/i
|
||||
# fix orientation first
|
||||
fix_image_orientation(file.path) if should_optimize?(file.path)
|
||||
# retrieve image info
|
||||
image_info = FastImage.new(file) rescue nil
|
||||
w, h = *(image_info.try(:size) || [0, 0])
|
||||
@@ -107,8 +107,8 @@ class Upload < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
# optimize image (but not for GIFs)
|
||||
if filename !~ /\.GIF$/i
|
||||
# optimize image (except GIFs and large PNGs)
|
||||
if should_optimize?(file.path)
|
||||
ImageOptim.new.optimize_image!(file.path) rescue nil
|
||||
# update the file size
|
||||
filesize = File.size(file.path)
|
||||
@@ -163,6 +163,18 @@ class Upload < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
LARGE_PNG_SIZE ||= 3.megabytes
|
||||
|
||||
def self.should_optimize?(path)
|
||||
# don't optimize GIFs
|
||||
return false if path =~ /\.gif$/i
|
||||
return true if path !~ /\.png$/i
|
||||
image_info = FastImage.new(path) rescue nil
|
||||
w, h = *(image_info.try(:size) || [0, 0])
|
||||
# don't optimize large PNGs
|
||||
w > 0 && h > 0 && w * h < LARGE_PNG_SIZE
|
||||
end
|
||||
|
||||
def self.is_dimensionless_image?(filename, width, height)
|
||||
FileHelper.is_image?(filename) && (width.blank? || width == 0 || height.blank? || height == 0)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user