Skip to content

Commit

Permalink
Merge pull request #1878 from tvdeyen/preprocess-images
Browse files Browse the repository at this point in the history
Configurable Image Preprocessor
  • Loading branch information
tvdeyen authored Jun 15, 2020
2 parents 1e7c025 + a7479da commit 3072ac9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
19 changes: 16 additions & 3 deletions app/models/alchemy/picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions app/models/alchemy/picture/preprocessor.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3072ac9

Please sign in to comment.