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?

Vasilisa
  • 4,604
  • 3
  • 20
  • 25
Steven Aguilar
  • 3,107
  • 5
  • 39
  • 89

2 Answers2

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.

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • This worked for me. Any idea what `.variable?` means. Seems like a weird choice kinda unrelated to what it actually does – stevec Dec 19 '20 at 10:00
  • @stevec 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](https://github.com/rails/rails/blob/914caca2d31bd753f47f9168f2a375921d9e91cc/activestorage/app/models/active_storage/blob/representable.rb#L42). – Juan Luis Orozco Villalobos Dec 21 '20 at 12:42
  • Thanks Juan! Today I encountered a seemingly regular `.png` that couldn't be transformed for some reason. I will read the github code. Thanks! – stevec Dec 21 '20 at 12:43
  • 1
    @stevec 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](https://github.com/rails/rails/blob/45d1efab5163f030a1a8555d44ab11b4cd437d13/activestorage/lib/active_storage/engine.rb#L31) Good luck! – Juan Luis Orozco Villalobos Dec 21 '20 at 12:49
  • 1
    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. – james00794 May 26 '21 at 14:05
0

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

Ralf Claassens
  • 189
  • 1
  • 8