Commit 0cf62de7c3d8df89c743b22a2809952d8b49a3cf
1 parent
2b1ea36b
Exists in
master
Use posix-spawn to instead of Kernel#exec to get better performance
Showing
4 changed files
with
23 additions
and
5 deletions
Show diff stats
letter_avatar.gemspec
... | ... | @@ -20,6 +20,8 @@ Gem::Specification.new do |spec| |
20 | 20 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) |
21 | 21 | spec.require_paths = ["lib"] |
22 | 22 | |
23 | + spec.add_dependency 'posix-spawn', '>= 0.3.0' | |
24 | + | |
23 | 25 | spec.add_development_dependency "bundler", "~> 1.3" |
24 | 26 | spec.add_development_dependency "rake" |
25 | 27 | end | ... | ... |
lib/letter_avatar/avatar.rb
... | ... | @@ -12,6 +12,8 @@ module LetterAvatar |
12 | 12 | |
13 | 13 | FILL_COLOR = 'rgba(255, 255, 255, 0.65)'.freeze |
14 | 14 | |
15 | + FONT_FILENAME = File.join(File.expand_path("../../", File.dirname(__FILE__)), "Roboto-Medium") | |
16 | + | |
15 | 17 | class << self |
16 | 18 | |
17 | 19 | class Identity |
... | ... | @@ -64,21 +66,30 @@ module LetterAvatar |
64 | 66 | color = identity.color |
65 | 67 | letter = identity.letter |
66 | 68 | |
69 | + | |
67 | 70 | filename = fullsize_path(identity) |
68 | 71 | |
69 | - instructions = %W{ | |
72 | + commands = %W( | |
73 | + convert | |
70 | 74 | -size 240x240 |
71 | 75 | xc:#{to_rgb(color)} |
72 | 76 | -pointsize 140 |
73 | - -font Roboto-Medium | |
77 | + -font #{FONT_FILENAME} | |
74 | 78 | -weight 500 |
75 | 79 | -fill '#{FILL_COLOR}' |
76 | 80 | -gravity Center |
77 | 81 | -annotate -0+10 '#{letter}' |
78 | 82 | '#{filename}' |
79 | - } | |
83 | + ) | |
80 | 84 | |
81 | - `convert #{instructions.join(" ")}` | |
85 | + command = commands.join(' ') | |
86 | + | |
87 | + pid, stdin, stdout, stderr = POSIX::Spawn.popen4(command) | |
88 | + Process.waitpid(pid) | |
89 | + err = stderr.read | |
90 | + if err != nil && err.length > 0 | |
91 | + raise "letter_avatar error: #{err.strip}" | |
92 | + end | |
82 | 93 | |
83 | 94 | filename |
84 | 95 | end | ... | ... |
lib/letter_avatar/version.rb