本文介绍了SVG上传的carrierwave png缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在轨道上使用红宝石.我希望通过SVG文件的载波上传来制作.png缩略图.

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

我在获取载波以将文件转换为png的语法上遇到麻烦.

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

这是关闭的,缩略图的内容是png数据,但文件扩展名是.svg

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

推荐答案

经过大量研究,有一种方法可以更改文件后缀.困难的部分是使载波仅更改缩略图的后缀.如果您不小心,它将更改所有文件后缀,包括原始的上载文件.

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.

这是起作用的

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

这篇关于SVG上传的carrierwave png缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 05:38