Skip to content

Commit

Permalink
Refactoring & Usage of templates
Browse files Browse the repository at this point in the history
  • Loading branch information
emaillenin committed Jun 8, 2014
1 parent 5f466ed commit 0aba81d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.gem
*.rbc
.idea
/.config
/coverage/
/InstalledFiles
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gem 'rmagick'

gem 'psych'
64 changes: 64 additions & 0 deletions index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'RMagick'
include Magick

require_relative 'src/template/image_mojo_template'

template = ImageMojoTemplate.new(ARGV[0])

imgl = Magick::ImageList.new

src = Magick::Image.read(template.image['path']).first

puts "#{src.columns}x#{src.rows}"

start_x = template.copyright['x_offset']
start_y = (src.rows/2) - template.copyright['height']
end_x = start_x + template.copyright['width']
end_y = (src.rows/2) + template.copyright['height']

imgl.from_blob(File.open(template.image['path']).read)

gc = Magick::Draw.new

gc.fill_opacity(template.copyright['opacity'])

gc.rectangle(start_x, start_y, end_x, end_y)

gc.font(template.copyright['opacity'])

gc.draw(imgl)

imgl.alpha(Magick::ActivateAlphaChannel)

watermark_text = Magick::Draw.new
watermark_text.annotate(imgl, start_x, start_y, end_x - 5, end_y - 10, template.copyright['text']) do
self.gravity = Magick::EastGravity
self.pointsize = 15
self.font_family = 'Arial'
self.font_weight = Magick::BoldWeight
self.stroke = 'transparent'
self.fill = 'white'
self.rotation = 270
self.kerning = 1
self.align = LeftAlign
end

caption_text = Magick::Draw.new
caption_text.annotate(imgl, src.columns, src.rows, src.columns/2, src.rows/2, template.caption['text']) do
self.gravity = Magick::CenterGravity
self.pointsize = template.caption['size']
self.font_family = template.caption['font']
self.font_weight = Magick::BoldWeight
self.stroke = 'transparent'
self.fill = template.caption['color']
self.stroke = template.caption['stroke']
self.stroke_width = 1
# self.rotation = 270
self.kerning = 1
self.interline_spacing = 2.5
self.align = CenterAlign
end

imgl.write("mojo_#{File.basename(template.image['path'])}")

exit
11 changes: 11 additions & 0 deletions src/template/image_mojo_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'psych'
class ImageMojoTemplate

attr_accessor :copyright, :caption, :image
def initialize(config_file)
template = Psych.load(File.read(config_file))
@copyright = template['copyright']
@caption = template['caption']
@image = template['image']
end
end
19 changes: 19 additions & 0 deletions template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
image:
path: /Users/leninraj/Downloads/161895.3.jpg

copyright:
text: WWW.DUGGOUT.COM
height: 100
width: 25
x_offset: 50
opacity: 0.6
font: Roboto

caption:
text: Chris Gayle is playing his \n100th test match today!
font: Roboto
size: 45
color: white
stroke: Transparent
offset_x: 0
offset_y: 100

0 comments on commit 0aba81d

Please sign in to comment.