From c7c93e7159751141721d5214ebf354fd25bc0705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 25 Jul 2017 11:48:39 +0200 Subject: [PATCH] FEATURE: new 'strip image metadata' site setting --- .image_optim.yml | 12 ------------ app/models/optimized_image.rb | 11 +++++++---- app/models/upload.rb | 4 +--- config/locales/server.en.yml | 2 ++ config/site_settings.yml | 1 + lib/file_helper.rb | 30 ++++++++++++++++++++++++------ lib/tasks/images.rake | 5 +++-- lib/upload_creator.rb | 2 +- 8 files changed, 39 insertions(+), 28 deletions(-) delete mode 100644 .image_optim.yml diff --git a/.image_optim.yml b/.image_optim.yml deleted file mode 100644 index 746b85dc3b4..00000000000 --- a/.image_optim.yml +++ /dev/null @@ -1,12 +0,0 @@ -skip_missing_workers: true -allow_lossy: false -# PNG -advpng: false -optipng: - level: 2 -pngcrush: false -pngout: false -pngquant: false -# JPG -jpegrecompress: false -timeout: 15 diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb index 52c79698ecb..e665a8f89d3 100644 --- a/app/models/optimized_image.rb +++ b/app/models/optimized_image.rb @@ -107,6 +107,9 @@ class OptimizedImage < ActiveRecord::Base end end + def self.thumbnail_or_resize + SiteSetting.strip_image_metadata ? "thumbnail" : "resize" + end def self.resize_instructions(from, to, dimensions, opts={}) ensure_safe_paths!(from, to) @@ -118,7 +121,7 @@ class OptimizedImage < ActiveRecord::Base -auto-orient -gravity center -background transparent - -thumbnail #{dimensions}^ + -#{thumbnail_or_resize} #{dimensions}^ -extent #{dimensions} -interpolate bicubic -unsharp 2x0.5+0.7+0 @@ -151,7 +154,7 @@ class OptimizedImage < ActiveRecord::Base -auto-orient -gravity north -background transparent - -thumbnail #{opts[:width]} + -#{thumbnail_or_resize} #{opts[:width]} -crop #{dimensions}+0+0 -unsharp 2x0.5+0.7+0 -interlace none @@ -223,7 +226,7 @@ class OptimizedImage < ActiveRecord::Base return false end - ImageOptim.new.optimize_image!(to) + FileHelper.optimize_image!(to) true rescue Rails.logger.error("Could not optimize image: #{to}") @@ -268,7 +271,7 @@ class OptimizedImage < ActiveRecord::Base optimized_image.sha1 = Upload.generate_digest(path) end # optimize if image - ImageOptim.new.optimize_image!(path) + FileHelper.optimize_image!(path) # store to new location & update the filesize File.open(path) do |f| optimized_image.url = Discourse.store.store_optimized_image(f, optimized_image) diff --git a/app/models/upload.rb b/app/models/upload.rb index d40ac15836e..5e272846402 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -109,9 +109,7 @@ class Upload < ActiveRecord::Base upload.sha1 = Upload.generate_digest(path) end # optimize if image - if FileHelper.is_image?(File.basename(path)) - ImageOptim.new.optimize_image!(path) - end + FileHelper.optimize_image!(path) if FileHelper.is_image?(File.basename(path)) # store to new location & update the filesize File.open(path) do |f| upload.url = Discourse.store.store_upload(f, upload) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index e9bc0335f72..d9960a815ad 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1206,6 +1206,8 @@ en: allow_staff_to_upload_any_file_in_pm: "Allow staff members to upload any files in PM." + strip_image_metadata: "Strip image metadata." + enable_flash_video_onebox: "Enable embedding of swf and flv (Adobe Flash) links in oneboxes. WARNING: may introduce security risks." default_invitee_trust_level: "Default trust level (0-4) for invited users." diff --git a/config/site_settings.yml b/config/site_settings.yml index c270144629e..53ed6d41ea1 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -811,6 +811,7 @@ files: allow_staff_to_upload_any_file_in_pm: default: true client: true + strip_image_metadata: true trust: default_trust_level: diff --git a/lib/file_helper.rb b/lib/file_helper.rb index f3ae79d4a5d..be727230d04 100644 --- a/lib/file_helper.rb +++ b/lib/file_helper.rb @@ -48,14 +48,32 @@ class FileHelper downloaded&.close end + def self.optimize_image!(filename) + ImageOptim.new( + # GLOBAL + timeout: 15, + skip_missing_workers: true, + # PNG + optipng: { level: 2, strip: SiteSetting.strip_image_metadata }, + advpng: false, + pngcrush: false, + pngout: false, + pngquant: false, + # JPG + jpegoptim: { strip: SiteSetting.strip_image_metadata ? "all" : "none" }, + jpegtran: false, + jpegrecompress: false, + ).optimize_image!(filename) + end + private - def self.images - @@images ||= Set.new %w{jpg jpeg png gif tif tiff bmp svg webp ico} - end + def self.images + @@images ||= Set.new %w{jpg jpeg png gif tif tiff bmp svg webp ico} + end - def self.images_regexp - @@images_regexp ||= /\.(#{images.to_a.join("|")})$/i - end + def self.images_regexp + @@images_regexp ||= /\.(#{images.to_a.join("|")})$/i + end end diff --git a/lib/tasks/images.rake b/lib/tasks/images.rake index 79e19bbe18f..c754841f05d 100644 --- a/lib/tasks/images.rake +++ b/lib/tasks/images.rake @@ -1,8 +1,9 @@ +require_dependency "file_helper" + task "images:compress" => :environment do - io = ImageOptim.new images = Dir.glob("#{Rails.root}/app/**/*.png") image_sizes = Hash[*images.map{|i| [i,File.size(i)]}.to_a.flatten] - io.optimize_images!(images) do |name, optimized| + FileHelper.optimize_images!(images) do |name, optimized| if optimized new_size = File.size(name) puts "#{name} => from: #{image_sizes[name.to_s]} to: #{new_size}" diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb index 656d74f90ef..67efcd32704 100644 --- a/lib/upload_creator.rb +++ b/lib/upload_creator.rb @@ -237,7 +237,7 @@ class UploadCreator def optimize! OptimizedImage.ensure_safe_paths!(@file.path) - ImageOptim.new.optimize_image!(@file.path) + FileHelper.optimize_image!(@file.path) extract_image_info! rescue ImageOptim::Worker::TimeoutExceeded Rails.logger.warn("ImageOptim timed out while optimizing #{@filename}")