imgproxy / imgproxy.rb

Framework-agnostic Ruby Gem for imgproxy with support for Ruby on Rails' most popular image attachment options
https://imgproxy.net
MIT License
196 stars 28 forks source link

Feature Request: ActiveStorage Analyzer #208

Open henrikbjorn opened 1 year ago

henrikbjorn commented 1 year ago

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

marckohlbrugge commented 10 months ago

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: