我正在拔头发,几天来一直在寻找解决方案。我正在尝试获取上传的 pdf 文档并创建一个可在所有浏览器上查看的缩略图。我可以让它在 Safari/iOS 或 Firefox/IE7+/Chrome 上运行,但不是一个可以在任何地方运行的版本。我尝试将色彩空间设置为 rgb(无效果),转换为 png 而不是 jpg(无效果)并设置:set_content_type(无效果)。我在本地机器和生产(Heroku)上得到了相同的结果。
pdfdoc_uploader.rb
include ImageManipulators
include CarrierWave::RMagick
include CarrierWave::MimeTypes
version :thumb do
process :convert => 'jpg' #<---This works in Safari and iOS
process :resize_to_fit => [200, 200]
process :paper_shape
process :strip
process :convert => 'jpg' #<---Move it here and it works everywhere else
def full_filename (for_file = model.logo.file)
super.chomp(File.extname(super)) + '.jpg'
end
end
image_manipulators.rb
module ImageManipulators
# creates an image with a 3x4 aspect ratio
def paper_shape
manipulate! do |img|
if img.rows*4 != img.columns*3
width=img.columns
height=img.columns/3*4
img.crop!(0,0,width,height,true)
else
img
end
end
end
def set_content_type(*args)
self.file.instance_variable_set(:@content_type, "image/jpeg")
end
# Autoorients image according to exif
def auto_orient
manipulate! {|img| img.auto_orient! || img }
end
# Crops the biggest possible square from the image
def biggest_square
manipulate! do |img|
if img.rows != img.columns
max = img.rows > img.columns ? img.columns : img.rows
img.crop!(0,0,max,max,true)
else
img
end
end
end
def paper_shape
manipulate! do |img|
if img.rows*4 != img.columns*3
width=img.columns
height=img.columns/3*4
img.crop!(0,0,width,height,true)
else
img
end
end
end
# Create rounded corners for the image
def rounded_corners
radius = 10
manipulate! do |img|
#create a masq of same size
masq = Magick::Image.new(img.columns, img.rows)
d = Magick::Draw.new
d.roundrectangle(0, 0, img.columns - 1, img.rows - 1, radius, radius)
d.draw(masq)
img.composite(masq, 0, 0, Magick::LightenCompositeOp)
end
end
# Rotates the image based on the EXIF Orientation
def fix_exif_rotation
manipulate! do |img|
img.auto_orient!
img = yield(img) if block_given?
img
end
end
# Strips out all embedded information from the image
def strip
manipulate! do |img|
img.strip!
img = yield(img) if block_given?
img
end
end
def colorspace(cs)
manipulate! do |img|
case cs
when 'rgb'
img.colorspace = Magick::RGBColorspace
when 'cmyk'
img.colorspace = Magick::CMYKColorspace
end
img = yield(img) if block_given?
img
end
end
end