Handling SVG images with Refile and Imgix

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

My colleague Tomek today was responsible for changing a bit how we handle file uploads in a project so that it can support SVG logos.

For handling uploads this Rails app uses Refile library. And for serving images there is Imgix which helps you save bandwith and apply transformations (using Imgix servers instead of yours).

The normal approach didn’t work because it did not recognize SVGs as images.

attachment :logo, type: :image 

So instead we had to list supported content types manually.

attachment :logo,    content_type: %w(image/jpeg image/png image/gif image/svg+xml) 

There is also a bit of logic involved in building proper URL for the browser.

= link_to image_tag(imgix_url("/shop/#{shop.logo_id}",   { auto: "compress,format",w: 300,h: 300,fit: "crop" }),   filename: shop.logo_filename) 
def imgix_url(path, **options)   options[:lossless] = true if options[:lossless].nil?   host = options.delete(:host) || S3_IMGIX_PRODUCTION_HOST)   Imgix::Client.new(host: host).path(path).to_url(options) end 

Leave a Reply

Your email address will not be published. Required fields are marked *