Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: ActiveStorage Analyzer #208

Open
henrikbjorn opened this issue Dec 9, 2022 · 1 comment
Open

Feature Request: ActiveStorage Analyzer #208

henrikbjorn opened this issue Dec 9, 2022 · 1 comment

Comments

@henrikbjorn
Copy link

It would be pretty cool to have a imgproxy active storage analyzer. This would make it possible to completely skip the imagemagick / vips

@marckohlbrugge
Copy link
Contributor

Here's what I use:

# app/analyzers/customer_image_analyzer.rb
class CustomImageAnalyzer < ActiveStorage::Analyzer::ImageAnalyzer
  def metadata
    metadata_from_imgproxy
  rescue HTTP::Error
    super
  end

  private

  def metadata_from_imgproxy
    url = blob.imgproxy_info_url(blurhash: "4:3", format: false, exif: false, iptc: false, xmp: false, video_meta: false, average: true)
    data = HTTP.get(url).parse

    {
      width: data["width"],
      height: data["height"],
      average_color: "##{data["average"]["R"].to_s(16)}#{data["average"]["G"].to_s(16)}#{data["average"]["B"].to_s(16)}",
      blurhash: data["blurhash"]
    }
  end
end
# config/initializers/active_storage_analyzers.rb
require Rails.root.join("app/analyzers/custom_image_analyzer.rb")

Rails.application.config.active_storage.analyzers.prepend CustomImageAnalyzer

Important to note:

  • I use http to fetch the info endpoint, but you can use any HTTP client
  • The current implementation of .imgproxy_info_url does NOT yet support specifying options like I do above. You can use my fork (might disappear in the future) or wait for @DarthSim's implementation (strongly recommended)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants