Question

I have successfully used carrier wave to upload image files. I want the form to be able to accept image files and pdf's. When I try to upload a pdf, it does not upload the file. It has to do with the line:

process :resize_to_fill => [166,166]

If I take that out, pdf's work. The problem is I need that line because I need all pictures uploaded need to be resized. Here is the uploader:

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

Does anyone know how I can fix it so images and pdf's will work? Thanks.

UPDATE:

Portfolio show page (2 versions):

version 1:

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

version 2:

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

Solution

Carrierwave has a solution to that, pointed in the Readme:

Conditional versions

Occasionally you want to restrict the creation of versions on certain properties within the model or based on the picture itself.

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

Example

For example, to create thumbs only for images I adopted this option:

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

In this case make sure you include the MimeTypes:

include CarrierWave::MimeTypes
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top