来自 svg 上传的载波 png 缩略图

carrierwave png thumbnails from svg upload

在 rails 上使用 ruby。我想要 SVG 文件的载波上传来制作 .png 缩略图。

我在获取 carrierwave 以将文件转换为 png 的语法方面遇到问题。

这很接近,缩略图的内容是png数据,但文件扩展名为.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

经过大量研究,有一种方法可以更改文件后缀。困难的部分是让 carrierwave 只改变缩略图的后缀。如果您不小心,它会更改所有文件后缀,包括您的原始上传文件。

这是有效的方法

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