ruby-on-railssvgcarrierwave

carrierwave png thumbnails from svg upload


Using ruby on rails. I want carrierwave upload of an SVG file to make .png thumbnails.

I'm having trouble with the syntax of getting carrierwave to convert the files to png.

This is close, and the contents of the thumbnails are png data, but the filename extension is .svg

class SvgUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  storage :file

  version :thumb do
    process :convert => 'png'
    process resize_to_fit: [50, 50]
  end
  version :thumb_small do
    process :convert => 'png'
    process resize_to_fit: [15, 15]
  end

Solution

  • After lots of research, there's a way to change the file suffix. The difficult part is to get carrierwave to change only the suffix of the thumbnails. If you're not careful it will change all files suffixes including the your original upload file.

    Here's what worked

    class SvgUploader < CarrierWave::Uploader::Base
      include CarrierWave::MiniMagick
      storage :file
    
      version :thumb do
        def full_filename(for_file)
          super(for_file).chomp(File.extname(super(for_file))) + '.png'
        end
        process :convert => 'png'
        process resize_to_fit: [50, 50]
      end
    
      version :thumb_small do
        def full_filename(for_file)
          super(for_file).chomp(File.extname(super(for_file))) + '.png'
        end
        process :convert => 'png'
        process resize_to_fit: [15, 15]
      end
    end