From a420dfc19d360bb0d962e64508be47b8df28ecf0 Mon Sep 17 00:00:00 2001 From: Vitalii Khustochka Date: Wed, 1 May 2024 23:06:23 -0500 Subject: [PATCH] Switch to vips image processor (much faster). BUT: 1. Breaks tests. 2. All variants will need to be recreated, leaving unused records in the DB and S3 - migration process is needed. --- app/models/image.rb | 2 +- app/models/image_representer.rb | 16 +++++++++++----- config/application.rb | 2 +- lib/quails/exif_date_image_analyzer.rb | 11 ++++++++--- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/models/image.rb b/app/models/image.rb index f701c302d..2e673ffcc 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -119,7 +119,7 @@ def stored_image_to_asset_item end def stored_image_thumbnail_variant - stored_image.variant(resize: "900x>") + stored_image.variant(ImageRepresenter.resize_and_save_space([900, nil])) end # FIXME: another duplication... diff --git a/app/models/image_representer.rb b/app/models/image_representer.rb index 96fd6d86a..6d2c571f3 100644 --- a/app/models/image_representer.rb +++ b/app/models/image_representer.rb @@ -65,16 +65,22 @@ def storage_srcset end def variant(width) - image.stored_image.variant(resize_and_save_space("#{width}x>")) + image.stored_image.variant(ImageRepresenter.resize_and_save_space([width, nil])) end - private - # TODO: Make adjustments for images already saved with worse quality? - def resize_and_save_space(resizing) - { resize: resizing, quality: "85%", strip: true, interlace: "Plane" } + def self.resize_and_save_space(dimensions) + { + resize_to_limit: dimensions, + saver: { + quality: 85, strip: true, optimize_coding: true, + trellis_quant: true, quant_table: 3, keep: nil, + }, + } end + private + def relevant_assets_cache key = image.on_flickr? ? :externals : :locals image.assets_cache.public_send(key) diff --git a/config/application.rb b/config/application.rb index 2ca584413..584ab81c8 100644 --- a/config/application.rb +++ b/config/application.rb @@ -63,7 +63,7 @@ class Application < Rails::Application config.i18n.default_locale = :uk config.i18n.available_locales = [:uk, :en, :ru] - config.active_storage.variant_processor = :mini_magick + config.active_storage.variant_processor = :vips config.before_configuration do require "quails" diff --git a/lib/quails/exif_date_image_analyzer.rb b/lib/quails/exif_date_image_analyzer.rb index 9904dfac4..f1cf78f3c 100644 --- a/lib/quails/exif_date_image_analyzer.rb +++ b/lib/quails/exif_date_image_analyzer.rb @@ -1,13 +1,18 @@ # frozen_string_literal: true module Quails - class ExifDateImageAnalyzer < ::ActiveStorage::Analyzer::ImageAnalyzer::ImageMagick + class ExifDateImageAnalyzer < ActiveStorage::Analyzer::ImageAnalyzer::Vips def metadata read_image do |image| mdata = {} - exif_date = image.exif["DateTimeOriginal"] + exif_date = + begin + image.get("exif-ifd2-DateTimeOriginal") + rescue ::Vips::Error + nil + end if exif_date.present? - mdata[:exif_date] = exif_date.split(":", 3).join("-") + mdata[:exif_date] = exif_date[0..18].split(":", 3).join("-") end if rotated_image?(image) mdata[:width] = image.height