FEATURE: new 'strip image metadata' site setting

This commit is contained in:
Régis Hanol 2017-07-25 11:48:39 +02:00
parent 933711a771
commit c7c93e7159
8 changed files with 39 additions and 28 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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}")