Skip to content

Instantly share code, notes, and snippets.

@tedgrubb
Last active May 4, 2017 17:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tedgrubb/db81f606ae4974325769 to your computer and use it in GitHub Desktop.
Save tedgrubb/db81f606ae4974325769 to your computer and use it in GitHub Desktop.
Paperclip Processor that finds the background color and replaces it with a transparent background. Also simulates anti-aliasing using '-fuzz' to get rid of pixelated artifacts.
# lib/paperclip_processors/transparency.rb
module Paperclip
class Transparency < Thumbnail
# Find the background and replace with transparency.
# -fuzz 20% simulates antialiasing
CONVERT_OPTIONS = [
'-alpha', 'set',
'-fill', 'white',
'-draw', "'color 0,0 replace'",
'-fuzz', '20%',
'-transparent', 'white'
]
# Append the convert options to existing options
# defined via style and convert_options
def transformation_command
super + CONVERT_OPTIONS
end
end
end
# Usage
# has_attached_file :logo,
# styles: { small: ['150x50', :png] },
# processors: [:transparency]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment