File tree 2 files changed +26
-8
lines changed
2 files changed +26
-8
lines changed Original file line number Diff line number Diff line change @@ -97,7 +97,11 @@ def self.preprocessor_class=(klass)
97
97
end
98
98
99
99
# Use ActiveStorage image processing
100
- has_one_attached :image_file , service : :alchemy_cms
100
+ has_one_attached :image_file , service : :alchemy_cms do |attachable |
101
+ # Only works in Rails 7.1
102
+ preprocessor_class . new ( attachable ) . call
103
+ Preprocessor . generate_thumbs! ( attachable )
104
+ end
101
105
102
106
validates_presence_of :image_file
103
107
validate :image_file_type_allowed , :image_file_not_too_big ,
Original file line number Diff line number Diff line change 3
3
module Alchemy
4
4
class Picture < BaseRecord
5
5
class Preprocessor
6
- def initialize ( image_file )
7
- @image_file = image_file
6
+ def initialize ( attachable )
7
+ @attachable = attachable
8
8
end
9
9
10
10
# Preprocess images after upload
@@ -15,14 +15,28 @@ def initialize(image_file)
15
15
#
16
16
def call
17
17
max_image_size = Alchemy ::Config . get ( :preprocess_image_resize )
18
- image_file . thumb! ( max_image_size ) if max_image_size . present?
19
- # Auto orient the image so EXIF orientation data is taken into account
20
- image_file . auto_orient!
18
+ if max_image_size . present?
19
+ self . class . process_thumb ( attachable , size : max_image_size )
20
+ end
21
21
end
22
22
23
- private
23
+ attr_reader :attachable
24
24
25
- attr_reader :image_file
25
+ class << self
26
+ def generate_thumbs! ( attachable )
27
+ Alchemy ::Picture ::THUMBNAIL_SIZES . values . each do |size |
28
+ process_thumb ( attachable , size : size , flatten : true )
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def process_thumb ( attachable , options = { } )
35
+ attachable . variant :thumb ,
36
+ **Alchemy ::DragonflyToImageProcessing . call ( options ) ,
37
+ preprocessed : true
38
+ end
39
+ end
26
40
end
27
41
end
28
42
end
You can’t perform that action at this time.
0 commit comments