11

I would like to use a single file-field for multiple formats. It was my understanding that Paperclip was smart enough to only scale images and leave other formats alone, but this doesn't seem to work for flv's (which returns imagemagick/identify-errors). Is there any way to help Paperclip a bit and explicitly setup specific formats to scale?

UPDATE: Apparently, these errors are prevented with :whiny=>false (thanks fl00r), which works fine for regular uploads. However, what I'm trying to do here is uploading the file by FTP, and later on create a new record by code with a File.new([:path]) in the attachment-parameter. This works like a charm for images, but the :whiny=>false-trick won't do it anymore. Does anyone has any tips on this?

fl00r
  • 82,987
  • 33
  • 217
  • 237
Jpunt
  • 229
  • 2
  • 8

2 Answers2

13

set :whiny option to false:

has_attached_file :my_attach, :whiny => false ...

it won't help peparclip to process images only, but it won't throw errors if processing failed

UPD

Processing for images only:

has_attached_file :file, 
  :styles => lambda{ |a| ["image/jpeg", "image/png"].include?( a.content_type ) ? { :small => "90x90#" } : {}  }

where you can add as more as you like content types into ["image/jpeg", "image/png"] array

fl00r
  • 82,987
  • 33
  • 217
  • 237
  • somehow the array-include-approach kept throwing "NoMethodError (undefined method `each' for false:FalseClass)" errors, so I had to do it the dirty-way. But at least it works, thanks again. – Jpunt Mar 17 '11 at 11:24
  • looks like this problem not about styling – fl00r Mar 17 '11 at 11:29
  • That's what I thought.. Works for now, but i'll keep looking for what this error is. – Jpunt Mar 18 '11 at 09:32
3

You can also use paperclip's callback for post-processing of images, and instruct paperclip to only process images. If the before_post_process callback returns false, the processing stops.

    before_post_process :process_only_images

    def process_only_images
     %w(image/jpeg, image/png,image/gif,image/pjpeg, image/x-png).include?(attachment_content_type)
    end

Check paperclip's documentation for more details at https://github.com/thoughtbot/paperclip#events

mmattke
  • 711
  • 6
  • 6
  • I like this approach but the code isn't quite correct: %w(image/jpeg image/png,image/gif image/pjpeg image/x-png).include?(attachment_content_type) – Dan Hixon Jan 23 '14 at 15:57
  • 2
    you have to be careful, this is not how you declare an array using %w. use this instead `%w(image/jpeg image/png image/gif image/pjpeg image/x-png).include?(resource_content_type)` – fenec Sep 04 '15 at 14:58