Handling SVG images with Refile and Imgix

… and check why 5600+ Rails engineers read also this

Handling SVG images with Refile and Imgix

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)

You might also like