Ruby on rails 4 Rails 4 CarrierWave(主)和MiniMagick:更改文件格式
我使用的是Rails 4.2、CarrierWave主分支和MiniMagick 4.3.6(也使用CarrierWave::MiniMagick模块) 根据技术规范,可以在上传/处理阶段更改文件格式 然而,我认为代码并不像文档所描述的那样工作。我使用AmazonS3作为我的存储库,使用Ruby on rails 4 Rails 4 CarrierWave(主)和MiniMagick:更改文件格式,ruby-on-rails-4,carrierwave,imagemagick-convert,minimagick,Ruby On Rails 4,Carrierwave,Imagemagick Convert,Minimagick,我使用的是Rails 4.2、CarrierWave主分支和MiniMagick 4.3.6(也使用CarrierWave::MiniMagick模块) 根据技术规范,可以在上传/处理阶段更改文件格式 然而,我认为代码并不像文档所描述的那样工作。我使用AmazonS3作为我的存储库,使用FogAWSgem,上传到S3的文件保留其原始格式 我的最终目标是上传一个PDF文档,并在S3中保存时将其处理为png文件 当我上传图像文件时,一切正常,它们作为图像正确地进入S3。但是,PDF文件仍然存储在S3
FogAWS
gem,上传到S3的文件保留其原始格式
我的最终目标是上传一个PDF文档,并在S3中保存时将其处理为png文件
当我上传图像文件时,一切正常,它们作为图像正确地进入S3。但是,PDF文件仍然存储在S3中,其内容类型为“application/PDF”,浏览器尝试将其呈现为PDF而不是图像(如果有用,我的上载将在图像滑块中使用)
有人知道为什么转换过程不能正常工作吗
模型(模型/证明文件.rb):
类证明
上传者(上传者/推荐上传者.rb):
类认证上传器[8501100]
def将文件转换为png(宽度、高度)
操纵do | img|
img.format(“png”)do|c|
c、 修剪
c、 调整“#{width}x#{height}>”的大小
c、 调整“#{width}x#{height}大小。您应该在此处添加您的解决方案或经验
class Testimonial < ActiveRecord::Base
mount_uploader :image, TestimonialUploader
end
class TestimonialUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :fog
process :convert_file_to_png => [850, 1100]
def convert_file_to_png(width, height)
manipulate! do |img|
img.format("png") do |c|
c.trim
c.resize "#{width}x#{height}>"
c.resize "#{width}x#{height}<"
end
img
end
end
def extension_white_list
%w(jpg jpeg png pdf bmp tif tiff)
end
def store_dir
"testimonials/#{model.uuid}"
end
def filename
super.chomp(File.extname(super)) + ".png" if original_filename.present?
end
end
class TestimonialUploader < CarrierWave::Uploader::Base
# 2015-12-23: note that ImageMagick does not actually seem to work
include CarrierWave::RMagick
# Scopes
#----------------------------------------------------------------------
# NOOP
# Macros
#----------------------------------------------------------------------
storage :fog
process convert_file_to_png: [850, 1100]
process :set_content_type_to_png
# Associations
#----------------------------------------------------------------------
# NOOP
# Validations
#----------------------------------------------------------------------
# NOOP
# Methods
#----------------------------------------------------------------------
def convert_file_to_png(width, height)
manipulate!(format: "png", read: { density: 400 }) do |img, index, options|
options = { quality: 100 }
img.resize_to_fit!(width, height)
end
end
# Required to actually force Amazon S3 to treat it like an image
def set_content_type_to_png
self.file.instance_variable_set(:@content_type, "image/png")
end
def extension_white_list
%w(jpg jpeg png pdf bmp tif tiff)
end
def store_dir
"testimonials/#{model.uuid}"
end
def filename
super.chomp(File.extname(super)) + ".png" if original_filename.present?
end
end