Commit 414d3d3d9a2262db7201e4104515f62dd7ccff6b
1 parent
1d1f5e51
Exists in
master
one, standard LetterAvatar.execute method for system calls
Showing
2 changed files
with
44 additions
and
40 deletions
Show diff stats
lib/letter_avatar.rb
... | ... | @@ -6,29 +6,30 @@ require "letter_avatar/avatar_helper" |
6 | 6 | module LetterAvatar |
7 | 7 | extend LetterAvatar::Configuration |
8 | 8 | |
9 | + class ExecutionError < StandardError; end | |
10 | + | |
9 | 11 | def self.setup(&block) |
10 | 12 | yield(self) |
11 | 13 | end |
12 | 14 | |
13 | 15 | def self.resize(from, to, width, height) |
14 | - # NOTE: ORDER is important! | |
15 | - instructions = %W{ | |
16 | - #{from} | |
17 | - -background transparent | |
18 | - -gravity center | |
19 | - -thumbnail #{width}x#{height}^ | |
20 | - -extent #{width}x#{height} | |
21 | - -interpolate bicubic | |
22 | - -unsharp 2x0.5+0.7+0 | |
23 | - -quality 98 | |
24 | - #{to} | |
25 | - }.join(" ") | |
26 | - | |
27 | - `convert #{instructions}` | |
28 | - | |
29 | - if $?.exitstatus == 0 | |
16 | + begin | |
17 | + execute( | |
18 | + # NOTE: ORDER is important! | |
19 | + %W{ | |
20 | + #{from} | |
21 | + -background transparent | |
22 | + -gravity center | |
23 | + -thumbnail #{width}x#{height}^ | |
24 | + -extent #{width}x#{height} | |
25 | + -interpolate bicubic | |
26 | + -unsharp 2x0.5+0.7+0 | |
27 | + -quality 98 | |
28 | + #{to} | |
29 | + }.join(" ") | |
30 | + ) | |
30 | 31 | true |
31 | - else | |
32 | + rescue => e | |
32 | 33 | false |
33 | 34 | end |
34 | 35 | end |
... | ... | @@ -37,4 +38,16 @@ module LetterAvatar |
37 | 38 | Avatar.generate(username, size) |
38 | 39 | end |
39 | 40 | |
41 | + def self.execute(cmd) | |
42 | + cmd = cmd.join(' ') if cmd.is_a?(Array) | |
43 | + pid, stdin, stdout, stderr = POSIX::Spawn.popen4(cmd) | |
44 | + Process.waitpid(pid) | |
45 | + err = stderr.read | |
46 | + if err != nil && err.length > 0 | |
47 | + raise ExecutionError.new("letter_avatar execution error (when calling '#{cmd}'): '#{err.strip}'") | |
48 | + else | |
49 | + true | |
50 | + end | |
51 | + end | |
52 | + | |
40 | 53 | end | ... | ... |
lib/letter_avatar/avatar.rb
... | ... | @@ -64,31 +64,22 @@ module LetterAvatar |
64 | 64 | end |
65 | 65 | |
66 | 66 | def generate_fullsize(identity) |
67 | - color = identity.color | |
68 | - letter = identity.letter | |
69 | - | |
70 | - | |
71 | 67 | filename = fullsize_path(identity) |
72 | 68 | |
73 | - command = %W( | |
74 | - convert | |
75 | - -size #{FULLSIZE}x#{FULLSIZE} | |
76 | - xc:#{to_rgb(color)} | |
77 | - -pointsize 140 | |
78 | - -font #{FONT_FILENAME} | |
79 | - -weight #{LetterAvatar.weight} | |
80 | - -fill '#{FILL_COLOR}' | |
81 | - -gravity Center | |
82 | - -annotate #{LetterAvatar.annotate_position} '#{letter}' | |
83 | - '#{filename}' | |
84 | - ).join(' ') | |
85 | - | |
86 | - pid, stdin, stdout, stderr = POSIX::Spawn.popen4(command) | |
87 | - Process.waitpid(pid) | |
88 | - err = stderr.read | |
89 | - if err != nil && err.length > 0 | |
90 | - raise "letter_avatar error: #{err.strip}" | |
91 | - end | |
69 | + LetterAvatar.execute( | |
70 | + %W( | |
71 | + convert | |
72 | + -size #{FULLSIZE}x#{FULLSIZE} | |
73 | + xc:#{to_rgb(identity.color)} | |
74 | + -pointsize 140 | |
75 | + -font #{FONT_FILENAME} | |
76 | + -weight #{LetterAvatar.weight} | |
77 | + -fill '#{FILL_COLOR}' | |
78 | + -gravity Center | |
79 | + -annotate #{LetterAvatar.annotate_position} '#{identity.letter}' | |
80 | + '#{filename}' | |
81 | + ).join(' ') | |
82 | + ) | |
92 | 83 | |
93 | 84 | filename |
94 | 85 | end | ... | ... |