【发布时间】:2021-11-17 13:08:14
【问题描述】:
我正在使用 Magick 将 PDF 页面转换为图像,并使用 Carrierwave 进行文件存储管理。
这是我正在使用的方法,它有效:
def convert_to_images
user_cv_images.destroy_all
Magick::ImageList.new(self.file.file.file) do
self.quality = 100
self.density = 144
self.colorspace = Magick::RGBColorspace
self.interlace = Magick::NoInterlace
end.each_with_index do |image, index|
tmp_file_path = "tmp/page-#{index}.png"
image.resize_to_fit!(1600, 10000)
image.write(tmp_file_path)
user_cv_images.create(image: File.open(Rails.root.join(tmp_file_path), position: index + 1))
File.delete(Rails.root.join(tmp_file_path))
end
end
困扰我的是,我将这些图像中的每一个都保存到 tmp/ 文件夹,以便将其传递给 Carrierwave 上传器。我很确定有更好的方法可以做到这一点,但我找不到它。
图像是Magick::Image 类的对象。
【问题讨论】:
-
我猜你可以use IO和image.to_blob
标签: ruby-on-rails-5 carrierwave rmagick