Skip to content

Instantly share code, notes, and snippets.

@cgallagher
Created May 3, 2012 11:40
Show Gist options
  • Save cgallagher/2585140 to your computer and use it in GitHub Desktop.
Save cgallagher/2585140 to your computer and use it in GitHub Desktop.
Basic insert text on image via imagemagick
class Davos
#require 'RMagick'
include Magick
def self.for(user, message)
bottle_composition = create_composition(user, message)
bottle_file = Tempfile.new("composite-for-#{user.fbid}.jpg")
bottle_composition.write(bottle_file.path)
BPLogger.debug(bottle_file.path)
#File.delete(bottle_file.path) if File.exists?(bottle_file.path)
return bottle_file.path
end
private
def self.create_composition(user, message)
image_base_src = File.join(Rails.root, "app/assets/images/bottle.jpg")
bottle_image = Magick::ImageList.new(image_base_src)
bi = Davos.insert_message(bottle_image, 0, 0, message)
return bi
end
def self.insert_message(current_image, x_pos, y_pos, message)
bm = Draw.new
current_image.annotate(bm, 300, 200, 100, 20, message){
bm.gravity = Magick::NorthWestGravity
bm.pointsize = 20
bm.fill = "#000000"
bm.stroke = 'transparent'
bm.font_weight = Magick::BoldWeight
}
return current_image
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment