Skip to content

Commit

Permalink
Switch to vips image processor (much faster). BUT:
Browse files Browse the repository at this point in the history
1. Breaks tests.
2. All variants will need to be recreated, leaving unused records in the DB and S3 - migration process is needed.
  • Loading branch information
khustochka committed May 3, 2024
1 parent dd0fd39 commit a420dfc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/models/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
16 changes: 11 additions & 5 deletions app/models/image_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 8 additions & 3 deletions lib/quails/exif_date_image_analyzer.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit a420dfc

Please sign in to comment.