Skip to content

Instantly share code, notes, and snippets.

@cherring
Created September 13, 2011 05:37
Show Gist options
  • Save cherring/1213197 to your computer and use it in GitHub Desktop.
Save cherring/1213197 to your computer and use it in GitHub Desktop.
Using libdevil to thumbnail and watermark
# Gem: http://rubygems.org/gems/devil
# Library: http://openil.sourceforge.net/
# Images for use with this: http://dl.dropbox.com/u/363740/images.zip
# To install on snow leopard with homebrew:
# 1. brew install devil
# 2. gem install devil
# 3. ???
# 4. Profit!
require 'rubygems'
require 'devil'
# Set the image to be centered when enlarging the canvas
Devil.set_options(:placement => Devil::CENTER)
# Set the background to be black when enlarging the canvas ([r,g,b,a])
Devil.set_options(:clear_color => [0,0,0,255])
# Memory automatically freed after blocks are done running
def thumbnail1(input, output)
# Make a square thumbnail with black padding to maintain aspect ratio
Devil.load_image(input) do |image|
# Make the image square (black borders added) with the image centered
longest_side = [image.width,image.height].max
image.enlarge_canvas(longest_side, longest_side)
# Thumbnail the image (applying a blur to keep it from looking crappy)
image.thumbnail2(200)
# Save to a new file
image.save(output)
end
end
def thumbnail2(input, output)
Devil.load_image(input) do |image|
if image.width == image.height
image.thumbnail2(200) # Just do normal thumbnailing if it's already square
elsif image.width > image.height
new_width = (image.width / image.height.to_f) * 200
image.resize2(new_width, 200)
x_offset = (new_width - 200) / 2.0
image.crop(x_offset, 0, 200, 200)
else
new_height = (image.height / image.width.to_f) * 200
image.resize2(200, new_height)
y_offset = (new_height - 200) / 2.0
image.crop(0, y_offset, 200, 200)
end
# Save to a new file
image.save(output)
end
end
def thumbnail_with_watermark(input, output, watermark)
thumbnail2(input, output)
i = Devil.load_image(output)
w = Devil.load_image(watermark)
if w.width > (i.width / 2.0) || w.height > (i.height / 2.0)
new_width = w.width
new_height = w.height
if w.width > (i.width / 2.0)
old_width = new_width
new_width = i.width / 2.0
new_height = (new_height / old_width.to_f) * new_width
end
if w.height > (i.height / 2.0)
old_height = new_height
new_height = i.height / 2.0
new_width = (new_width / old_height.to_f) * new_height
end
w.resize2(new_width, new_height)
i.blit(w, i.width - w.width - 10, i.height - w.height - 10)
else
i.blit(w, i.width - w.width - 10, i.height - w.height - 10)
end
i.save(output)
i.free
w.free
end
thumbnail1('square.jpg', 'square1.jpg')
thumbnail1('wider.jpg', 'wider1.jpg')
thumbnail1('taller.jpg', 'taller1.jpg')
thumbnail2('square.jpg', 'square2.jpg')
thumbnail2('wider.jpg', 'wider2.jpg')
thumbnail2('taller.jpg', 'taller2.jpg')
thumbnail_with_watermark('square.jpg', 'square-wide-watermarked.jpg', 'wider.jpg')
thumbnail_with_watermark('wider.jpg', 'wider-wide-watermarked.jpg', 'wider.jpg')
thumbnail_with_watermark('taller.jpg', 'taller-wide-watermarked.jpg', 'wider.jpg')
thumbnail_with_watermark('square.jpg', 'square-tall-watermarked.jpg', 'taller.jpg')
thumbnail_with_watermark('wider.jpg', 'wider-tall-watermarked.jpg', 'taller.jpg')
thumbnail_with_watermark('taller.jpg', 'taller-tall-watermarked.jpg', 'taller.jpg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment