FIX: don't try to optimize large PNGs (takes too much time)

This commit is contained in:
Régis Hanol
2016-02-22 12:57:24 +01:00
parent 5f747a74a1
commit 4d981cec53
3 changed files with 35 additions and 20 deletions

View File

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