memegen 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +45 -0
  3. data/bin/memegen +6 -0
  4. data/lib/meme_generator.rb +114 -0
  5. metadata +71 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # memegen
2
+
3
+ An RMagick-based two-caption meme generator.
4
+
5
+ ![Profit](https://github.com/cmdrkeene/memegen/raw/master/example.jpg)
6
+
7
+ ## Install
8
+
9
+ You'll need [ImageMagick](http://www.imagemagick.org/script/index.php) installed.
10
+ The easiest way is to use [Homebrew](https://github.com/mxcl/homebrew).
11
+
12
+ brew install imagemagick
13
+
14
+ ## Usage
15
+
16
+ To see a list of available generators:
17
+
18
+ $ memegen --list
19
+ a_dog
20
+ p_obama
21
+ ...
22
+
23
+ To generate an image:
24
+
25
+ $ memegen a_dog "Eat Crayons" "Poop Rainbows"
26
+ /tmp/meme-1304797373.jpg
27
+ $
28
+
29
+ You only have to supply one piece of text:
30
+
31
+ $ memegen a_dog "" "AWESOME"
32
+ /tmp/meme-1304797375.jpg
33
+ $
34
+
35
+ ## Adding your own generators
36
+
37
+ You can add generators to your local `~/.memegen` folder:
38
+
39
+ $ ls ~/.memegen
40
+ my_custom_generator.png
41
+
42
+ ## Contribute
43
+
44
+ Feel free to improve this and add your own generator images!
45
+
data/bin/memegen ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
4
+ require "meme_generator"
5
+
6
+ MemeGenerator.run
@@ -0,0 +1,114 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require "RMagick"
4
+
5
+ class MemeGenerator
6
+ VERSION = "0.0.2"
7
+ IMPACT_PATH = "fonts/Impact.ttf" # If you don't have OS X, fork me :)
8
+
9
+ class << self
10
+ def run(argv = ARGV)
11
+ generator, top, bottom = argv[0..2]
12
+
13
+ return list_generators if generator == "--list"
14
+ return usage unless generator && (top || bottom)
15
+
16
+ if path = generators.find { |p| p =~ generator}
17
+ generate(path, top, bottom)
18
+ exit 0
19
+ else
20
+ puts "Meme not found"
21
+ exit 1
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def usage
28
+ puts 'usage: memegen <generator> <top text> <bottom text> [--list]'
29
+ exit 1
30
+ end
31
+
32
+ def list_generators
33
+ generators.each do |path|
34
+ puts File.basename(path).gsub(/\..*/, '')
35
+ end
36
+ exit 0
37
+ end
38
+
39
+ def generate(path, top, bottom)
40
+ top = top.upcase
41
+ bottom = bottom.upcase
42
+
43
+ canvas = Magick::ImageList.new(path)
44
+ image = canvas.first
45
+
46
+ draw = Magick::Draw.new
47
+ draw.font = IMPACT_PATH if File.exists?(IMPACT_PATH)
48
+ draw.font_weight = Magick::BoldWeight
49
+
50
+ pointsize = image.columns / 5.0
51
+ stroke_width = pointsize / 30.0
52
+ x_position = image.columns / 2
53
+ y_position = image.rows * 0.15
54
+
55
+ # Draw top
56
+ unless top.empty?
57
+ scale, text = scale_text(top)
58
+ bottom_draw = draw.dup
59
+ bottom_draw.annotate(canvas, 0, 0, 0, 0, text) do
60
+ self.interline_spacing = -(pointsize / 5)
61
+ self.stroke_antialias(true)
62
+ self.stroke = "black"
63
+ self.fill = "white"
64
+ self.gravity = Magick::NorthGravity
65
+ self.stroke_width = stroke_width * scale
66
+ self.pointsize = pointsize * scale
67
+ end
68
+ end
69
+
70
+ # Draw bottom
71
+ unless bottom.empty?
72
+ scale, text = scale_text(bottom)
73
+ bottom_draw = draw.dup
74
+ bottom_draw.annotate(canvas, 0, 0, 0, 0, text) do
75
+ self.interline_spacing = -(pointsize / 5)
76
+ self.stroke_antialias(true)
77
+ self.stroke = "black"
78
+ self.fill = "white"
79
+ self.gravity = Magick::SouthGravity
80
+ self.stroke_width = stroke_width * scale
81
+ self.pointsize = pointsize * scale
82
+ end
83
+ end
84
+
85
+ output_path = "/tmp/meme-#{Time.now.to_i}.jpeg"
86
+ canvas.write(output_path)
87
+ puts output_path
88
+ exit 0
89
+ end
90
+
91
+ def word_wrap(txt, col = 80)
92
+ txt.gsub(/(.{1,#{col + 4}})(\s+|\Z)/, "\\1\n")
93
+ end
94
+
95
+ def scale_text(text)
96
+ text = text.dup
97
+ if text.length < 10
98
+ scale = 1.0
99
+ elsif text.length < 24
100
+ text = word_wrap(text, 10)
101
+ scale = 0.70
102
+ else
103
+ text = word_wrap(text, 18)
104
+ scale = 0.5
105
+ end
106
+ [scale, text.strip]
107
+ end
108
+
109
+ def generators
110
+ home_dir = File.expand_path("~")
111
+ Dir.glob(["generators/*", home_dir]).sort
112
+ end
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: memegen
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Brandon Keene
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-07 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Locally generate two-caption 'Advice Dog'-style meme images
22
+ email:
23
+ - bkeene@gmail.com
24
+ executables:
25
+ - memegen
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - bin/memegen
32
+ - lib/meme_generator.rb
33
+ - LICENSE
34
+ - README.md
35
+ homepage: http://github.com/cmdrkeene/memegen
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 3
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 23
58
+ segments:
59
+ - 1
60
+ - 3
61
+ - 6
62
+ version: 1.3.6
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.7.2
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Two-caption meme generator CLI
70
+ test_files: []
71
+