1

我已成功使用载波上传图像文件。我希望表单能够接受图像文件和 pdf。当我尝试上传 pdf 时,它不会上传文件。它与这条线有关:

process :resize_to_fill => [166,166]

如果我把它拿出来,pdf的工作。问题是我需要那条线,因为我需要调整所有上传的图片大小。这是上传者:

class PortfoliofileUploader < CarrierWave::Uploader::Base
      include CarrierWave::MiniMagick
      def store_dir
          "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
      end
      version :picture do
           process :resize_to_fill => [166,166]
      end
      def extension_white_list
          %w(jpg jpeg gif png pdf doc docx)
      end
end

有谁知道我可以如何修复它以便图像和 pdf 可以工作?谢谢。

更新:

作品集展示页面(2 个版本):

版本 1:

<% @portfolio.portfolio_pics.collect{|picture| picture.port_pic.picture}.each do |pic| %>                           
    <li><a href="#"><%= image_tag pic %></a></li>                       
<% end %>

版本 2:

<% @portfolio.portfolio_pics.each do |pic| %>
    <li><a href="#"><%= image_tag pic.port_pic.picture %></a></li>
<% end %>
4

1 回答 1

4

Carrierwave 对此有一个解决方案,在自述文件中指出:

有条件的版本

有时,您希望限制在模型中的某些属性上或基于图片本身创建版本。

class MyUploader < CarrierWave::Uploader::Base

  version :human, :if => :is_human?
  version :monkey, :if => :is_monkey?
  version :banner, :if => :is_landscape?

protected

  def is_human? picture
    model.can_program?(:ruby)
  end

  def is_monkey? picture
    model.favorite_food == 'banana'
  end

  def is_landscape? picture
    image = MiniMagick::Image.open(picture.path)
    image[:width] > image[:height]
  end

end

例子

例如,要仅为图像创建缩略图,我采用了这个选项:

version :thumb, :if => :image? do
    process :resize_to_fit => [200, 200]
  end

protected    

    def image?(new_file)
      new_file.content_type.start_with? 'image'
    end

在这种情况下,请确保包含 MimeType:

include CarrierWave::MimeTypes
于 2013-12-15T22:24:37.607 回答