
11

Handling SVG images with Refile and Imgix
source link: https://blog.arkency.com/2017/06/handling-svg-images-with-refile/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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)
end
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK