diff --git a/app/models/alchemy/picture.rb b/app/models/alchemy/picture.rb index a11a4fff0f..c9d5b26d8d 100644 --- a/app/models/alchemy/picture.rb +++ b/app/models/alchemy/picture.rb @@ -50,12 +50,25 @@ class Picture < BaseRecord raise PictureInUseError, Alchemy.t(:cannot_delete_picture_notice) % { name: name } end + # Image preprocessing class + def self.preprocessor_class + @_preprocessor_class ||= Preprocessor + end + + # Set a image preprocessing class + # + # # config/initializers/alchemy.rb + # Alchemy::Picture.preprocessor_class = My::ImagePreprocessor + # + def self.preprocessor_class=(klass) + @_preprocessor_class = klass + end + # Enables Dragonfly image processing dragonfly_accessor :image_file, app: :alchemy_pictures do # Preprocess after uploading the picture - after_assign do |p| - resize = Config.get(:preprocess_image_resize) - p.thumb!(resize) if resize.present? + after_assign do |image| + self.class.preprocessor_class.new(image).call end end diff --git a/app/models/alchemy/picture/preprocessor.rb b/app/models/alchemy/picture/preprocessor.rb new file mode 100644 index 0000000000..9798ae125b --- /dev/null +++ b/app/models/alchemy/picture/preprocessor.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Alchemy + class Picture < BaseRecord + class Preprocessor + def initialize(image_file) + @image_file = image_file + end + + # Preprocess images after upload + # + # Define preprocessing options in the Alchemy::Config + # + # preprocess_image_resize [String] - Downsizing example: '1000x1000>' + # + def call + max_image_size = Alchemy::Config.get(:preprocess_image_resize) + image_file.thumb!(max_image_size) if max_image_size.present? + end + + private + + attr_reader :image_file + end + end +end