Handle CMYK colorspace uploads with Paperclip

Ariejan de vroomPosted by Ariejan de Vroom on 17-6-2010

When saving images in general, and JPEG in particular, a colorspace is used. The two most commonly used colorspaces are RGB and CMYK. RGB is used for screens (e.g. television, monitors, phones). Screens work by combining different light colors (red, green or blue) into one color we can actually see. CMYK is used in print often. Instead of sending out light, paper must reflect light to give it color. If you check your own printer you’ll see there are three colors: cyan, magenta and yellow. Again, these three colors can be combined to absorb colors, and thus reflect the color you want. The problem is that not all web browsers are capable of handling JPEGs that use the CMYK colorspace. Internet Explorer is the most notorious one. Internet Explorer users will see an ‘image not found’-error - or red cross - instead. If you’re using thoughtbot’s Paperclip, the solution is easy. By adding some custom ImageMagick convert options you can make sure that all generated thumbnails are in the RGB colorspace.

has_attached_file :avatar

would become:

has_attached_file :avatar 
   :convert_options => { :all => '-strip -colorspace RGB'}

Easy right? If you have a lot of CMYK images already, you don’t have to delete them, you can easily reprocess them when you made the above change:

User.find_each { |user| user.avatar.reprocess! }
Ariejan de vroom

Ariejan de Vroom

Software Engineer • CodeRetreat Facilitator • Ruby, Go and C Programmer • Electronics Apprentice

Bij Kabisa staat privacy hoog in het vaandel. Wij vinden het belangrijk dat er zorgvuldig wordt omgegaan met de data die onze bezoekers achterlaten. Zo zult u op onze website geen tracking-cookies vinden van third-parties zoals Facebook, Hotjar of Hubspot. Er worden alleen cookies geplaatst van Google en Vimeo. Deze worden gebruikt voor analyses, om zo de gebruikerservaring van onze websitebezoekers te kunnen verbeteren. Tevens zorgen deze cookies ervoor dat er relevante advertenties worden getoond. Lees meer over het gebruik van cookies in ons privacy statement.