tfantina
tfantina

Reputation: 805

Rails image resize before_save

I'd like to resize an image before it gets saved. Currently, this is done in the controller:

shrink = MiniMagick::Image.new(params[:user][:photo].tempfile.path)
shrink.resize "500x500"

That works but it doesn't feel very skinny. I'd rather have this in the model, which I assume would look something like this:

    before_save :resize_image

    def resize_image
      shrink = MiniMagick::Image.new(self.photo)
      shrink.resize "500x500"
    end

This; however, yealds the error:

`magick mogrify -resize 500x500 #<ActiveStorage::Attached::One:0x00007ffc866418d0>` failed with error:
mogrify: unable to open image '#<ActiveStorage::Attached::One:0x00007ffc866418d0>': No such file or directory @ error/blob.c/OpenBlob/3496.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/562.
 excluded from capture: Not configured to send/capture in environment 'development'

I've tried variations such as self.photo.path but so far I have been unable to find a method works for the active storage object. What am I missing? Am I correct in assuming that something like this belongs in the model in the first place rather than the controller?

Upvotes: 1

Views: 1336

Answers (0)

Related Questions