From e821410d84f2016e79864a06a4e7013c1b167c13 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Thu, 29 Dec 2016 17:09:01 -0600 Subject: [PATCH] optimize screenshots and thumbnails, slight thumbnail quality reduction --- Gemfile | 2 ++ Gemfile.lock | 16 ++++++++++++++++ environment.rb | 2 ++ workers/screenshot_worker.rb | 7 ++++++- workers/thumbnail_worker.rb | 7 +++++-- 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 5b738ec7..14b23e16 100644 --- a/Gemfile +++ b/Gemfile @@ -43,6 +43,8 @@ gem 'acme-client' gem 'http' gem 'htmlentities' gem 'rinku' +gem 'image_optim' +gem 'image_optim_pack' platform :mri, :rbx do gem 'magic' # sudo apt-get install file, For OSX: brew install libmagic diff --git a/Gemfile.lock b/Gemfile.lock index e5d828f1..69308e6a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -65,6 +65,7 @@ GEM unf (>= 0.0.5, < 1.0.0) drydock (0.6.9) erubis (2.7.0) + exifr (1.2.5) fabrication (2.15.0) faker (1.6.3) i18n (~> 0.5) @@ -75,6 +76,7 @@ GEM ffi (>= 1.0.0) rake filesize (0.1.1) + fspath (3.0.1) gandi (2.1.3) hashie geoip (1.6.1) @@ -95,6 +97,17 @@ GEM http-form_data (1.0.1) http_parser.rb (0.6.0) i18n (0.7.0) + image_optim (0.24.1) + exifr (~> 1.2, >= 1.2.2) + fspath (~> 3.0) + image_size (~> 1.5) + in_threads (~> 1.3) + progress (~> 3.0, >= 3.0.1) + image_optim_pack (0.3.0.20161206) + fspath (>= 2.1, < 4) + image_optim (~> 0.19) + image_size (1.5.0) + in_threads (1.3.1) io-extra (1.2.8) jimson-temp (0.9.5) blankslate (>= 3.1.2) @@ -141,6 +154,7 @@ GEM cliver (~> 0.3.1) websocket-driver (>= 0.2.0) posix-spawn (0.3.11) + progress (3.2.2) pry (0.10.3) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -277,6 +291,8 @@ DEPENDENCIES hoe (= 3.14.2) htmlentities http + image_optim + image_optim_pack io-extra jdbc-postgres jruby-openssl diff --git a/environment.rb b/environment.rb index 43e2c36f..43344bfd 100644 --- a/environment.rb +++ b/environment.rb @@ -152,3 +152,5 @@ end gandi_opts = {} gandi_opts[:env] = :test # unless ENV['RACK_ENV'] == 'production' $gandi = Gandi::Session.new $config['gandi_api_key'], gandi_opts + +$image_optim = ImageOptim.new pngout: false, svgo: false diff --git a/workers/screenshot_worker.rb b/workers/screenshot_worker.rb index da94aacc..72274bb2 100644 --- a/workers/screenshot_worker.rb +++ b/workers/screenshot_worker.rb @@ -62,10 +62,15 @@ class ScreenshotWorker else new_img = img.scale width, height end - new_img.write(File.join(user_screenshots_path, "#{path}.#{res}.jpg")) { + + full_screenshot_path = File.join(user_screenshots_path, "#{path}.#{res}.jpg") + + new_img.write(full_screenshot_path) { self.quality = 90 } + new_img.destroy! + $image_optim.optimize_image! full_screenshot_path end img.destroy! diff --git a/workers/thumbnail_worker.rb b/workers/thumbnail_worker.rb index ad6a3e06..385fdb4e 100644 --- a/workers/thumbnail_worker.rb +++ b/workers/thumbnail_worker.rb @@ -26,9 +26,12 @@ class ThumbnailWorker save_ext = format.match(Site::LOSSY_IMAGE_REGEX) ? 'jpg' : 'png' - resimg.write(File.join(user_thumbnails_path, "#{path}.#{res}.#{save_ext}")) { - self.quality = 90 + full_thumbnail_path = File.join(user_thumbnails_path, "#{path}.#{res}.#{save_ext}") + + resimg.write(full_thumbnail_path) { + self.quality = 75 } + $image_optim.optimize_image! full_thumbnail_path end end end