Open henrikbjorn opened 1 year 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:
http
to fetch the info endpoint, but you can use any HTTP client.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)
It would be pretty cool to have a
imgproxy
active storage analyzer. This would make it possible to completely skip the imagemagick / vips