mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: new 'strip image metadata' site setting
This commit is contained in:
parent
933711a771
commit
c7c93e7159
@ -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
|
|
@ -107,6 +107,9 @@ class OptimizedImage < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.thumbnail_or_resize
|
||||||
|
SiteSetting.strip_image_metadata ? "thumbnail" : "resize"
|
||||||
|
end
|
||||||
|
|
||||||
def self.resize_instructions(from, to, dimensions, opts={})
|
def self.resize_instructions(from, to, dimensions, opts={})
|
||||||
ensure_safe_paths!(from, to)
|
ensure_safe_paths!(from, to)
|
||||||
@ -118,7 +121,7 @@ class OptimizedImage < ActiveRecord::Base
|
|||||||
-auto-orient
|
-auto-orient
|
||||||
-gravity center
|
-gravity center
|
||||||
-background transparent
|
-background transparent
|
||||||
-thumbnail #{dimensions}^
|
-#{thumbnail_or_resize} #{dimensions}^
|
||||||
-extent #{dimensions}
|
-extent #{dimensions}
|
||||||
-interpolate bicubic
|
-interpolate bicubic
|
||||||
-unsharp 2x0.5+0.7+0
|
-unsharp 2x0.5+0.7+0
|
||||||
@ -151,7 +154,7 @@ class OptimizedImage < ActiveRecord::Base
|
|||||||
-auto-orient
|
-auto-orient
|
||||||
-gravity north
|
-gravity north
|
||||||
-background transparent
|
-background transparent
|
||||||
-thumbnail #{opts[:width]}
|
-#{thumbnail_or_resize} #{opts[:width]}
|
||||||
-crop #{dimensions}+0+0
|
-crop #{dimensions}+0+0
|
||||||
-unsharp 2x0.5+0.7+0
|
-unsharp 2x0.5+0.7+0
|
||||||
-interlace none
|
-interlace none
|
||||||
@ -223,7 +226,7 @@ class OptimizedImage < ActiveRecord::Base
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
ImageOptim.new.optimize_image!(to)
|
FileHelper.optimize_image!(to)
|
||||||
true
|
true
|
||||||
rescue
|
rescue
|
||||||
Rails.logger.error("Could not optimize image: #{to}")
|
Rails.logger.error("Could not optimize image: #{to}")
|
||||||
@ -268,7 +271,7 @@ class OptimizedImage < ActiveRecord::Base
|
|||||||
optimized_image.sha1 = Upload.generate_digest(path)
|
optimized_image.sha1 = Upload.generate_digest(path)
|
||||||
end
|
end
|
||||||
# optimize if image
|
# optimize if image
|
||||||
ImageOptim.new.optimize_image!(path)
|
FileHelper.optimize_image!(path)
|
||||||
# store to new location & update the filesize
|
# store to new location & update the filesize
|
||||||
File.open(path) do |f|
|
File.open(path) do |f|
|
||||||
optimized_image.url = Discourse.store.store_optimized_image(f, optimized_image)
|
optimized_image.url = Discourse.store.store_optimized_image(f, optimized_image)
|
||||||
|
@ -109,9 +109,7 @@ class Upload < ActiveRecord::Base
|
|||||||
upload.sha1 = Upload.generate_digest(path)
|
upload.sha1 = Upload.generate_digest(path)
|
||||||
end
|
end
|
||||||
# optimize if image
|
# optimize if image
|
||||||
if FileHelper.is_image?(File.basename(path))
|
FileHelper.optimize_image!(path) if FileHelper.is_image?(File.basename(path))
|
||||||
ImageOptim.new.optimize_image!(path)
|
|
||||||
end
|
|
||||||
# store to new location & update the filesize
|
# store to new location & update the filesize
|
||||||
File.open(path) do |f|
|
File.open(path) do |f|
|
||||||
upload.url = Discourse.store.store_upload(f, upload)
|
upload.url = Discourse.store.store_upload(f, upload)
|
||||||
|
@ -1206,6 +1206,8 @@ en:
|
|||||||
|
|
||||||
allow_staff_to_upload_any_file_in_pm: "Allow staff members to upload any files in PM."
|
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."
|
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."
|
default_invitee_trust_level: "Default trust level (0-4) for invited users."
|
||||||
|
@ -811,6 +811,7 @@ files:
|
|||||||
allow_staff_to_upload_any_file_in_pm:
|
allow_staff_to_upload_any_file_in_pm:
|
||||||
default: true
|
default: true
|
||||||
client: true
|
client: true
|
||||||
|
strip_image_metadata: true
|
||||||
|
|
||||||
trust:
|
trust:
|
||||||
default_trust_level:
|
default_trust_level:
|
||||||
|
@ -48,14 +48,32 @@ class FileHelper
|
|||||||
downloaded&.close
|
downloaded&.close
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def self.images
|
def self.images
|
||||||
@@images ||= Set.new %w{jpg jpeg png gif tif tiff bmp svg webp ico}
|
@@images ||= Set.new %w{jpg jpeg png gif tif tiff bmp svg webp ico}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.images_regexp
|
def self.images_regexp
|
||||||
@@images_regexp ||= /\.(#{images.to_a.join("|")})$/i
|
@@images_regexp ||= /\.(#{images.to_a.join("|")})$/i
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
|
require_dependency "file_helper"
|
||||||
|
|
||||||
task "images:compress" => :environment do
|
task "images:compress" => :environment do
|
||||||
io = ImageOptim.new
|
|
||||||
images = Dir.glob("#{Rails.root}/app/**/*.png")
|
images = Dir.glob("#{Rails.root}/app/**/*.png")
|
||||||
image_sizes = Hash[*images.map{|i| [i,File.size(i)]}.to_a.flatten]
|
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
|
if optimized
|
||||||
new_size = File.size(name)
|
new_size = File.size(name)
|
||||||
puts "#{name} => from: #{image_sizes[name.to_s]} to: #{new_size}"
|
puts "#{name} => from: #{image_sizes[name.to_s]} to: #{new_size}"
|
||||||
|
@ -237,7 +237,7 @@ class UploadCreator
|
|||||||
|
|
||||||
def optimize!
|
def optimize!
|
||||||
OptimizedImage.ensure_safe_paths!(@file.path)
|
OptimizedImage.ensure_safe_paths!(@file.path)
|
||||||
ImageOptim.new.optimize_image!(@file.path)
|
FileHelper.optimize_image!(@file.path)
|
||||||
extract_image_info!
|
extract_image_info!
|
||||||
rescue ImageOptim::Worker::TimeoutExceeded
|
rescue ImageOptim::Worker::TimeoutExceeded
|
||||||
Rails.logger.warn("ImageOptim timed out while optimizing #{@filename}")
|
Rails.logger.warn("ImageOptim timed out while optimizing #{@filename}")
|
||||||
|
Loading…
Reference in New Issue
Block a user