0

I'm using Active Storage and displaying images in the view like so:

image_tag current_user.user_primary_image.variant(resize_to_limit: [100, 100])

But when I try to resize this particular .png file:

ActiveStorage::InvariableError

I can stop the error by using .variable? to display/resize the image conditionally. But there's two problems, i) the image won't display (I want it to), and ii) I don't know why it's happening, so I can't predict when it will happen to other images - afaik, that .png should be .variable? true.

So, just to stop errors, I have

<% if current_user.user_primary_image.variable? %>
  <%= image_tag current_user.user_primary_image.variant(resize_to_limit: [100, 100]) if current_user.user_primary_image.attached? %>
<% end %>

Since png is one of these file types that should be able to be rezied/displayed, why doesn't the .png above resize/display, and how can I get it it to work?

dss
  • 395
  • 3
  • 11
  • 1
    It's not you -- it's the file. It's not really a png file -- it's a RIFF file. https://en.wikipedia.org/wiki/Resource_Interchange_File_Format.. Another reason to never trust file extensions. – dbugger Dec 22 '20 at 01:20
  • @dbugger thanks heaps for investigating. Do you know what I could do about this? Does Active Storage have a way of easily checking file types on upload (noting that the extension doesn't really help)? – dss Dec 22 '20 at 01:22
  • Not that I am aware of. – dbugger Dec 22 '20 at 01:22
  • You may be able to use a mime sniffing check on upload in Ruby (e.g., MimeMagic) or before upload in JS depending on how your code is structured. You could sniff it after upload and treat the file as invalid later before display. See also https://stackoverflow.com/questions/4600679/detect-mime-type-of-uploaded-file-in-ruby – rdnewman Dec 22 '20 at 15:22

0 Answers0