1

我正在拔头发,几天来一直在寻找解决方案。我正在尝试获取上传的 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
4

2 回答 2

2

2.5 年后我也遇到了同样的问题,而且我的头发也比以前少了。

当 Carrierwave 使用 RMagick 转换文件时,它调用manipulate!并传递一个:option => :format. manipulate!然后,如果该方法看到选项格式,则执行此操作(您将在lib/carrierwave/processing/rmagick.rb中找到):

if options[:format] || @format
    frames.write("#{options[:format] || @format}:#{current_path}", &write_block)
  move_to = current_path.chomp(File.extname(current_path)) + ".#{options[:format] || @format}"
    file.move_to(move_to, permissions, directory_permissions)
else
  frames.write(current_path, &write_block)
end

我认为,问题在于它没有明确设置文件内容,它只是更改了扩展名。

事实证明,一些浏览器(除了 Safari 之外)很乐意根据文件扩展名解释图像,而 Safari 显然使用或检查文件头。

所以,我的解决方案不是仅仅依靠 Carrierwave/RMagick 魔法来转换,而是在自定义进程中显式设置内容标题,看起来像这样,我从另一个堆栈问题中直接提取:

  version :png_thumb, :if => :pdf_document? do
    process :cover
    process :convert => :png
    process :resize_to_fit => [200, 300]
    process :set_content_type_png
    def full_filename (for_file = model.document.file)
      super.chomp(File.extname(super)) + '.png'
    end
  end

  def set_content_type_png(*args)
    Rails.logger.debug "#{file.content_type}"
    self.file.instance_variable_set(:@content_type, "image/png")
  end

你会注意到调试器会在你运行时打印 application/pdf,确认该:convert方法还没有工作(还没有!)。

于 2015-07-25T11:08:51.037 回答
0

我最终放弃了,只创建了两个版本并使用 useragent 来确定要显示哪个版本。虽然不是一个有吸引力的选择,但它正在发挥作用。我希望有人提出更好的解决方案。

version :thumb_safari do #special version for safari and ios
  process :resize_to_fit => [200,200]
  process :convert => 'jpg'
  process :paper_shape
  def full_filename (for_file = model.logo.file)
     super.chomp(File.extname(super)) + '.jpg'
  end     
end

version :thumb do #all browsers except safari
  process :resize_to_fit => [200,200]
  process :convert => 'jpg' #must convert to jpg before running paper shape
  process :paper_shape
  process :convert => 'jpg' #after running paper_shape it will default to original file type
  def full_filename (for_file = model.logo.file)
    super.chomp(File.extname(super)) + '.jpg'
  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

在控制器/视图中,我使用了 useragent gem 并这样做了:

文档控制器.rb

def index
  @user_agent=UserAgent.parse(request.user_agent)
  @search = Document.search(params[:q])
end

index.html.rb

<% if @user_agent.browser.downcase == 'safari' %>
<%= link_to(image_tag(doc.pdfdoc_url(:thumb_safari).to_s, :class=>"dropshadow", :size => "150x225"), doc.pdfdoc_url)%>
<% else %>
<%= link_to(image_tag(doc.pdfdoc_url(:thumb).to_s, :class=>"dropshadow", :size => "150x225"), doc.pdfdoc_url)%>
<% end %>

毫无疑问,有一种更好的方法可以做到这一点,但目前正在奏效。

于 2012-11-11T23:19:59.623 回答