ActiveStorage::InvariableError in Home#index
Asked Answered
L

3

7

Currently I'm trying to display .HEIC images in Rails 6. I'm using ActiveStorage ImageMagic to use variant to display jpg. Basically I'm trying to use

mogrify -format jpg myimage.heic

In the image to display jpg.

I added Rails.application.config.active_storage.variant_processor into application.rb to be able to use the variant. However it seems to break in the following line:

 <%= image_tag post.image.variant(format: 'jpg'), class: "card-home__img" %>

Why is not working?

Laurynlausanne answered 14/3, 2020 at 18:52 Comment(0)
V
11

You can only call variant on an image that returns true when you call variable? on it.

Internally, ActiveStorage checks if ActiveStorage.variable_content_types contains your image's type. The default supported values are:

  • image/png
  • image/gif
  • image/jpg
  • image/jpeg
  • image/pjpeg
  • image/tiff
  • image/bmp
  • image/vnd.adobe.photoshop
  • image/vnd.microsoft.icon
  • image/webp

So it seems that currently .HEIC images are not supported.

You can instead apply a format transformation before attaching the image to a model or storing it, it might solve your use case.

Visceral answered 23/5, 2020 at 11:57 Comment(5)
This worked for me. Any idea what .variable? means. Seems like a weird choice kinda unrelated to what it actually doesEmpoverish
@Empoverish According to a comment in the source code it just checks if ImageMagick can transform it, maybe they were thinking about image variations, hence the name. ``` ruby def variable? ActiveStorage.variable_content_types.include?(content_type) end ``` You can check the source code here.Visceral
Thanks Juan! Today I encountered a seemingly regular .png that couldn't be transformed for some reason. I will read the github code. Thanks!Empoverish
@Empoverish Sure thing. Supposedly ActiveStorage should be able to work with .png, but I've seen it behave weird from time to time. If you need to check the supported types you can find them here Good luck!Visceral
Thanks for this! I had a file with a .jpeg extension that was throwing this exception, and after investigating the image a bit more discovered that it was actually an HEIC image disguised as a jpeg. This answer helped me narrow it down.Twelvemo
D
2

Raised when ActiveStorage::Blob#variant is called on a blob that isn't variable. Use ActiveStorage::Blob#variable? to determine whether a blob is variable.

Source: https://edgeapi.rubyonrails.org/classes/ActiveStorage/InvariableError.html

Downpipe answered 8/4, 2020 at 6:7 Comment(0)
D
2

If you have compiled VIPS or ImageMagick to support HEIC images you can make the following configuration change to allow them to be automatically converted into a web format before further processing:

In application.rb:

config.active_storage.variable_content_types << 'image/heic'

Create an initialiser called active_storage.rb if you haven't got one already and add the following:

ActiveSupport.on_load :active_storage_blob do
  def default_variant_format
    if web_image?
      format || :jpeg
    else
      :jpeg
    end
  end
end

You only need to do the second step if you don't like the default web image format to be PNG. I think it's better to default to JPEG as this is better at compressing photos.

This works with Rails 6.1 and later I believe.

Dashed answered 16/5 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.