2

Steps to reproduce

Create a model with an Active Storage (S3) image attachment (a .png in my case). Using the default active_storage_analysis

Expected behavior

Since all I did was call the analyze method (which I assume is what's being called the first time around when an attachment is made), I expect it to originally have the width/height attributes.

Actual behavior

The Blob metadata is missing its width/height attributes:

#<ActiveStorage::Blob:0x00007fb934129130
 id: "d6d6922a-646f-446f-8656-b1301ca5ba59",
 key: "dm46dnva9crgu9k1r9y4bwmhz3oi",
 filename: "Cheddah_icon-60@3x.png",
 content_type: "image/png",
 metadata: {"identified"=>true, "analyzed"=>true},
 byte_size: 3559,
 checksum: "x+EHSQeB6l3Kvb+8SCMzxg==",
 created_at: Mon, 03 Jun 2019 14:56:13 UTC +00:00>

I then manually analyze the Blob by invoking the analyze method on it. It now has the width/height:

#<ActiveStorage::Blob:0x00007fb934129130
 id: "d6d6922a-646f-446f-8656-b1301ca5ba59",
 key: "dm46dnva9crgu9k1r9y4bwmhz3oi",
 filename: "Cheddah_icon-60@3x.png",
 content_type: "image/png",
 metadata: {"identified"=>true, "analyzed"=>true, "width"=>180, "height"=>180},
 byte_size: 3559,
 checksum: "x+EHSQeB6l3Kvb+8SCMzxg==",
 created_at: Mon, 03 Jun 2019 14:56:13 UTC +00:00>

System configuration

Rails version: 6.0.0.rc1

Ruby version: 2.6.2p47 (2019-03-13 revision 67232)

Extra info

  • I'm using Sidekiq v5.2.7
  • (not sure if relevant) my Variant Processor is VIPS:
image_processing (1.9.0)
      mini_magick (>= 4.9.3, < 5)
      ruby-vips (>= 2.0.13, < 3)

I'm aware that I can just create background jobs to do this every time the model is saved, but according to all the documentation I've been able to find, I shouldn't need to do this manually right?

Thank you for your help

m10i
  • 41
  • 1
  • 8

2 Answers2

1

In Rails 6.0.3.6, in the model, near the top

after_commit :analyze_metadata_now

def analyze_metadata_now
  image.analyze if image.attached?
end

Works for me. I also put in a check on the show page to remind me of the problem. The id is for bold and red.

<% if @doc.image.analyzed? %>
  <!-- had a message here while debugging -->
<% else %>
  <p id="error_explanation"><strong>Edit and resave because doc.image.metadata not created.</strong></p> 
<% end %>
Greg
  • 2,359
  • 5
  • 22
  • 35
0

Reading your question this is expected behaviour. By default the image won't be analysed until the first request for it happens. AnalyzeJob happens asynchronously. So the background job you mention might not be a bad idea.

As for using the dimensions as attributes when they aren't always available, this answer worked for me. This is specific to Active Storage attachment variants but should still apply:

module ApplicationHelper
  def image_tag_with_dimensions(variant, opts = {})
    if variant.processed?
      metadata = variant.processed.send(:record).image.blob.metadata
      if metadata['width']
        opts[:width] = metadata['width']
        opts[:height] = metadata['height']
      end
    end
    image_tag variant, opts
  end
end

# usage: 
= image_tag_with_dimensions model.logo_image.variant(resize_to_fit: [300, 200])
Alec Rust
  • 10,532
  • 12
  • 48
  • 63