Skip to content

Instantly share code, notes, and snippets.

@tonycoco
Created June 11, 2012 15:04
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save tonycoco/2910540 to your computer and use it in GitHub Desktop.
Save tonycoco/2910540 to your computer and use it in GitHub Desktop.
Useful image filters (Instagram/Hipster/Cool) for CarrierWave using MiniMagick (ImageMagick)
module CarrierWave
module MiniMagick
def toaster_filter
manipulate! do |img|
img.modulate '150,80,100'
img.gamma 1.1
img.contrast
img.contrast
img.contrast
img.contrast
img = yield(img) if block_given?
img
end
end
def lomo_filter
manipulate! do |img|
img.channel 'R'
img.level '22%'
img.channel 'G'
img.level '22%'
img = yield(img) if block_given?
img
end
end
def kelvin_filter
manipulate! do |img|
cols, rows = img[:dimensions]
img.combine_options do |cmd|
cmd.auto_gamma
cmd.modulate '120,50,100'
end
new_img = img.clone
new_img.combine_options do |cmd|
cmd.fill 'rgba(255,153,0,0.5)'
cmd.draw "rectangle 0,0 #{cols},#{rows}"
end
img = img.composite new_img do |cmd|
cmd.compose 'multiply'
end
img = yield(img) if block_given?
img
end
end
def colortone_filter(color = '#222b6d', level = 100, type = 0)
manipulate! do |img|
color_img = img.clone
color_img.combine_options do |cmd|
cmd.fill color
cmd.colorize '63%'
end
img = img.composite color_img do |cmd|
cmd.compose 'blend'
cmd.define "compose:args=#{level},#{100-level}"
end
img = yield(img) if block_given?
img
end
end
def gotham_filter
manipulate! do |img|
img.modulate '120,10,100'
img.fill '#222b6d'
img.colorize 20
img.gamma 0.5
img.contrast
img = yield(img) if block_given?
img
end
end
def strip
manipulate! do |img|
img.strip
img = yield(img) if block_given?
img
end
end
def quality(percentage)
manipulate! do |img|
img.quality(percentage)
img = yield(img) if block_given?
img
end
end
def vignette(path_to_file)
manipulate! do |img|
cols, rows = img[:dimensions]
vignette_img = ::MiniMagick::Image.open(path_to_file)
vignette_img.size "#{cols}x#{rows}"
img = img.composite(vignette_img) do |cmd|
cmd.gravity 'center'
cmd.compose 'multiply'
end
img = yield(img) if block_given?
img
end
end
def pad_it_up(width, height, background=:transparent, gravity='Center')
manipulate! do |img|
cols, rows = img[:dimensions]
if width > cols || height > rows
img.combine_options do |cmd|
if background == :transparent
cmd.background "rgba(255, 255, 255, 0.0)"
else
cmd.background background
end
cmd.gravity gravity
if width > cols && height > rows
cmd.extent "#{width}x#{height}"
elsif width > cols
cmd.extent "#{width}x#{rows}"
else
cmd.extent "#{cols}x#{height}"
end
end
end
img = yield(img) if block_given?
img
end
end
end
end
@tonycoco
Copy link
Author

The vignette needs to be a PNG transparent overlay.

@emilesilvis
Copy link

Hi, just discovered this. Do you perhaps have an example of the overlay for the vignette filter, and what are some sample values for width and height in the pad_it_up filter (just so I can see some effect)?

@tonycoco
Copy link
Author

Here's a sample vignette...

https://dl.dropboxusercontent.com/u/27290468/github/vignette.png

You can use any integer for the width/height for the pad_it_up filter. Basically, if your image is less than the dimensions you pass in, it will pad the image up to the those dimensions.

@emilesilvis
Copy link

Thanks for this! I've tried using this and a similar vignette mask, but it doesn't seem to have any effect whatsoever. Did something perhaps change in the underlying code?

Also can't get the pad_it_up filter to work...my image is 500px X 500px, so I pass 1000 in for both width and height, to no effect.

The rest of them work beautifully though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment