carrierwave png thumbnails from svg upload

喜夏-厌秋 提交于 2019-12-11 17:42:44

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/51544360/carrierwave-png-thumbnails-from-svg-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!