Steven Aguilar
Steven Aguilar

Reputation: 3029

Unable to display image using variant with ActiveStorage image_processing gem

Currently I'm working on a Rails 5.2 application. When I try to display a profile's avatar in a show.html.erb template using variant it doesn't work

<%= image_tag @profile.avatar.variant(resize_to_fit: [100, 100]) %>

I get the followingenter image description here

I'm unable to see the image.

I installed gem 'image_processing', '~> 1.2' in my Gemfile.

Also on application.rb I added config.active_storage.variant_processor = :vips

But still, on the backend I get a 500 error:

Started GET "/rails/active_storage/representations/xxxx/steven.jpeg" for 127.0.0.1 at 2018-09-26 16:33:21 -0400
Processing by ActiveStorage::RepresentationsController#show as JPEG
  Parameters: {"signed_blob_id"=>"xxxxxx", "variation_key"=>"xxxxxx", "filename"=>"steven"}
  ActiveStorage::Blob Load (0.3ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  ↳ /Users/stevenaguilar/.rvm/gems/ruby-2.2.2/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
  Disk Storage (0.1ms) Checked if file exists at key: variants/7rnyyMpZaqXT4RBNtzDqPFqS/477efe2eb62003af0b5b40ec71c56de636f58f942964d830feeed4057b8718a6 (no)
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.3ms)

If I edit to <%= image_tag @profile.avatar %> I'm able to see the original image.

here is the Photo model:

 class Profile < ApplicationRecord
  belongs_to :user
  has_one_attached :avatar
 end

Any suggestions?

Upvotes: 2

Views: 3789

Answers (3)

user22445746
user22445746

Reputation: 1

The image_processing gem relies on ImageMagick to do the actual image transformation. ImageMagick isn’t a Ruby gem; it’s distributed as a binary that’s specific to your operating system. You’ll need to install it separately for your operating system: For Linux use apt-get:

sudo apt-get install imagemagick

Then once you have ImageMagick installed, you’ll need to restart your Rails app.

Upvotes: 0

Yi Feng Xie
Yi Feng Xie

Reputation: 5017

The version of Rails you are using is 5.2, ActiveStorage of the version has no variant_processor option.

resize_to_fit is the option belongs to image_processing. You install the gem, but the processor is fixed to mini_magick. see: https://github.com/rails/rails/blob/v5.2.3/activestorage/app/models/active_storage/variant.rb#L117

If you want to keep the version of 5.2, you should follow options provided by mini_magick. see: https://api.rubyonrails.org/classes/ActiveStorage/Variation.html

If you prefer to use image_processing to enjoy convenient options, you should upgrade your Rails version to >= 6.

Upvotes: 4

mluiz
mluiz

Reputation: 11

try this:

<%= image_tag @profile.avatar.variant(resize: "100x100") %>

Upvotes: 1

Related Questions