Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable Image Preprocessor #1878

Merged
merged 2 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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