I use Paperclip for attachments in my Rails applications and I find that most of the time I end up using the following tweaks.

Convert Options

When you define attachments in your model, one of the available parameters is “convert_options” which is basically a string of parameters that is passed to image magick when thumbnails are created. I use the following value for the parameter:
has_attached_file :image,
  ...
  :convert_options => { :all => '-strip -colorspace RGB -resample 72'}
  ...

The list of supported parameters can be found here Below is the description of those that I use.

  • “-strip” tells IM to strip the image of any profiles or comments, which you probably don’t need and which helps to reduce the size of the image.
  • “-colorspace RGB” obviously sets the image colorspace to RGB. It’s more a specific of projects that I work on, where users sometimes upload CMYK images that are BTW not supported by some browsers (like, you know, IE).
  • “-resample 72” resamples the image to 72dpi resolution. Again, applications that I work on sometimes get 300dpi images, that need to be converted.

I use these for all attached images, so it can be DRYed by setting it as a default parameter in say config/initializers/paperclip.rb:

Paperclip::Attachment.default_options[:convert_options] = { :all => '-strip -colorspace RGB -resample 72'}

Delete Attachments

Another thing that I need for pretty much any attachment is an ability to delete it (if the attachment is optional). I think a good way to do it is to display the attachment on the “edit” page along with a checkbox that if checked removes the attachment. It’s totally doable, you just need to set the attachment to nil if the box is checked by (I guess) adding some logic to your controller, however it would be nice to have this feature out of the box. And that’s what I’ve added to my fork of paperclip

Basically it creates a virtual attribute for every attachment in the model that is named like “image_delete” if you have an “image” attachment. You just need to make sure that it is included in attr_accessible and add a checkbox to your form:

<% if form.object.image? -%>
  <%= image_tag form.object.image.url %>
  <%= form.check_box :image_delete, :label => 'Delete Image' %>
<% end -%>

Rename attachments

Paperclip provides a neat way to customize paths to your files using interpolations. It works great if you use things like “:id” that never change. However if you’re really crazy about the URLs of your images and want to have something more meaningful there (e.g. image title & author’s name like I do in one of my projects) and you update values that interpolations depend on, you need to rename your files as well. Paperclip won’t do it and hence another feature that is present in my fork

It checks whether it needs to rename files on every update and does it if necessary.

Content type and image dimensions validations

It’s a well-known fact that Firefox on Windows does not send correct mime type headers with attachments, that’s why Paperclip can not correctly validate such uploads. There’s a mimetype-fu plugin that can figure out the file’s mime type when it is already in the file system and my fork uses it if it’s installed.

Besides it also provides a way to validate image dimensions that is missing out of the box:

validates_attachment_dimensions :image, :minimum => 300, :maximum => 900

It will check if the image’s smallest side is bigger than the minimum parameter and the largest side is smaller than the maximum.

This seems to be it. Paperclip is a great gem and I like it even more with these tiny tweaks.