1

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 %>
Philip7899
  • 4,599
  • 4
  • 55
  • 114

1 Answers1

5

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
Alex Falke
  • 2,178
  • 2
  • 14
  • 11
  • That worked! I did not even need to include Mimetypes. How did that work though? None of the files actually start with the string 'image' – Philip7899 Dec 16 '13 at 14:39
  • There's a actually one thing that still doesn't work with this. When i try to loop through only pictures in the portfolio show page, it still includes pdf's and includes a question mark image for the pdf. How do I make it only show the pdf? I'll add my loop through code into the content. – Philip7899 Dec 16 '13 at 15:37
  • For the first comment, it doesn't look for 'string' in the image filename, but in the file content_type. PDF files won't have 'image' in the content type. – Alex Falke Dec 16 '13 at 17:41
  • If this response worked, could you marked it as solved? :) As for the second comment, it is another issue. You could ask again if the file is an image looking at the content_type. I don't remember how to do it exactly now, you could always google it. If I can I'll look it up later. – Alex Falke Dec 16 '13 at 17:49
  • I would use your second version. You can use the response for [this question](http://stackoverflow.com/questions/2224379/ruby-on-rails-how-do-you-check-if-a-file-is-an-image) to check if the file is an image. – Alex Falke Dec 16 '13 at 19:05