I am switching from Paperclip to Shrine because of Paperclip deprecation.
In my model Profilepic.rb
file I used to retrieve dimensions of an image variant as follow:
before_create :save_ratio
def save_ratio
geo = Paperclip::Geometry.from_file(image.queued_for_write[:original])
self.ratio = geo.width / geo.height
end
Basically I am saving the vertical ratio of an image.
It was working well with Paperclip : I grabbed the temporary image queued_for_write
and checked dimensions with Paperclip::Geometry
before saving the value.
In Shrine I have added the following to the uploader :
plugin :add_metadata
plugin :store_dimensions
It works great as I have all information available and images are uploaded to S3 properly.
Yet my new method for saving the image ratio no longer works :
after_create :save_ratio
def save_ratio
self.ratio = self.image[:original].width.to_i / self.image[:original].height.to_i
end
I get
error undefined method `[]' for ProfilepicUploader::UploadedFile:0x00007f69a685c750>
Whereas, in console, after image has been created :
Profilepic.first.image[:original].width.to_i
does return the correct value.
EDIT
My uploader :
require "image_processing/mini_magick"
class ProfilepicUploader < Shrine
include ImageProcessing::MiniMagick
plugin :processing
plugin :validation_helpers # to validate image data
plugin :versions
plugin :add_metadata
plugin :store_dimensions
Attacher.validate do
validate_max_size 5.megabyte
validate_mime_type_inclusion ['image/jpg', 'image/jpeg', 'image/png']
end
process(:store) do |io, context|
versions = { original: io } # retain original
io.download do |original|
pipeline = ImageProcessing::MiniMagick.source(original)
versions[:editable] = pipeline.resize_to_fit!(700, 700)
versions[:thumbnail] = pipeline.resize_to_fill!(400, 400)
versions[:small] = pipeline.resize_to_fill!(200, 200)
end
versions # return the hash of processed files
end
end
my model :
class Profilepic < ApplicationRecord
require "image_processing/mini_magick"
belongs_to :professionnel
before_create :set_hashid
include ProfilepicUploader::Attachment.new(:image) # adds an `image` virtual attribute
include ProfilepicFinalUploader::Attachment.new(:final_image) # adds an `image` virtual attribute
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_create :save_ratio_image
private
def save_ratio
self.ratio = self.image[:original].width.to_i / self.image[:original].height.to_i
end
end