avatarly 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +8 -0
- data/LICENSE +20 -0
- data/README.md +51 -0
- data/assets/fonts/Roboto.ttf +0 -0
- data/lib/avatarly.rb +87 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e0865e02ffaedda438ffddf4dcd45b8e1c463e2
|
4
|
+
data.tar.gz: 016b46b1f9b97cc0b7508772e76c6ce1dc8e786f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c1d616124e73312b383e2d5e9b429eb02acffe318fd47a08aa26894b9446454c3042681d7754a729bdef54714844cd38c2090ce5945175f1f5eeb548075e504d
|
7
|
+
data.tar.gz: 04733517a10cadd5375e729628f0c6924fc33f55bab079ef6321ba9132d7cfa1e2301d404e3558fb494b5077650c9583d2fb0de06833fab67f9b5351c225a7ad
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 Łukasz Odziewa
|
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,51 @@
|
|
1
|
+
![](http://f.cl.ly/items/0M3L273P2r2l0A3L052M/avatars.png)
|
2
|
+
|
3
|
+
# avatarly
|
4
|
+
|
5
|
+
avatarly is a simple gem for creating gmail-like user avatars based on user email or any other string passed
|
6
|
+
|
7
|
+
inspired and influenced by https://github.com/johnnyhalife/avatar-generator.rb
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Avatarly requires ImageMagick to be installed.
|
12
|
+
|
13
|
+
### Gems
|
14
|
+
|
15
|
+
The gems are hosted at Rubygems.org. Make sure you're using the latest version of rubygems:
|
16
|
+
|
17
|
+
$ gem update --system
|
18
|
+
|
19
|
+
Then you can install the gem as follows:
|
20
|
+
|
21
|
+
$ gem install avatarly
|
22
|
+
|
23
|
+
### Bundler
|
24
|
+
|
25
|
+
Add to your Gemfile:
|
26
|
+
|
27
|
+
gem "avatarly"
|
28
|
+
|
29
|
+
and then type:
|
30
|
+
|
31
|
+
bundle install
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
To generate image please do:
|
36
|
+
|
37
|
+
Avatarly.generate_avatar(text, opts={})
|
38
|
+
|
39
|
+
the only required parameter is <tt>text</tt>. Other options that you can pass are:
|
40
|
+
|
41
|
+
* <tt>background_color</tt> (#AABBCC)
|
42
|
+
* <tt>font_color</tt> (#AABBCC)
|
43
|
+
* <tt>size</tt> (default: 32)
|
44
|
+
* <tt>font</tt> (path to font - e.g. "#{Rails.root}/your_font.ttf")
|
45
|
+
* <tt>font_size</tt> (default: size / 2)
|
46
|
+
|
47
|
+
As a result you will get an image blob - rest is up to you, do whatever you want with it.
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
MIT
|
Binary file
|
data/lib/avatarly.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'rvg/rvg'
|
2
|
+
require 'rfc822'
|
3
|
+
|
4
|
+
class Avatarly
|
5
|
+
BACKGROUND_COLORS = [
|
6
|
+
"#ff4040", "#7f2020", "#cc5c33", "#734939", "#bf9c8f", "#995200",
|
7
|
+
"#4c2900", "#f2a200", "#ffd580", "#332b1a", "#4c3d00", "#ffee00",
|
8
|
+
"#b0b386", "#64664d", "#6c8020", "#c3d96c", "#143300", "#19bf00",
|
9
|
+
"#53a669", "#bfffd9", "#40ffbf", "#1a332e", "#00b3a7", "#165955",
|
10
|
+
"#00b8e6", "#69818c", "#005ce6", "#6086bf", "#000e66", "#202440",
|
11
|
+
"#393973", "#4700b3", "#2b0d33", "#aa86b3", "#ee00ff", "#bf60b9",
|
12
|
+
"#4d3949", "#ff00aa", "#7f0044", "#f20061", "#330007", "#d96c7b"
|
13
|
+
].freeze
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def generate_avatar(text, opts={})
|
17
|
+
generate_image(initials(text.to_s).upcase, parse_options(opts)).to_blob
|
18
|
+
end
|
19
|
+
|
20
|
+
def root
|
21
|
+
File.expand_path '../..', __FILE__
|
22
|
+
end
|
23
|
+
|
24
|
+
def lib
|
25
|
+
File.join root, 'lib'
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def fonts
|
31
|
+
File.join root, 'assets/fonts'
|
32
|
+
end
|
33
|
+
|
34
|
+
def generate_image(text, opts)
|
35
|
+
image = Magick::RVG.new(opts[:size], opts[:size]).viewbox(0, 0, opts[:size], opts[:size]) do |canvas|
|
36
|
+
canvas.background_fill = opts[:background_color]
|
37
|
+
end.draw
|
38
|
+
image.format = 'png'
|
39
|
+
draw_text(image, text, opts)
|
40
|
+
image
|
41
|
+
end
|
42
|
+
|
43
|
+
def draw_text(canvas, text, opts)
|
44
|
+
Magick::Draw.new do
|
45
|
+
self.pointsize = opts[:font_size]
|
46
|
+
self.font = opts[:font]
|
47
|
+
self.fill = opts[:font_color]
|
48
|
+
self.gravity = Magick::CenterGravity
|
49
|
+
end.annotate(canvas, 0, 0, 0, 0, text)
|
50
|
+
end
|
51
|
+
|
52
|
+
def initials(text)
|
53
|
+
if text.dup.is_email? # duplicate used due to the fact that is_email? method changes encoding to binary
|
54
|
+
initials_for_separator(text.split("@").first, ".")
|
55
|
+
elsif text.include?(" ")
|
56
|
+
initials_for_separator(text, " ")
|
57
|
+
else
|
58
|
+
initials_for_separator(text, ".")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def initials_for_separator(text, separator)
|
63
|
+
if text.include?(separator)
|
64
|
+
text = text.split(separator)
|
65
|
+
text[0][0] + text[1][0]
|
66
|
+
else
|
67
|
+
text[0][0]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def default_options
|
72
|
+
{ background_color: BACKGROUND_COLORS.sample,
|
73
|
+
font_color: '#FFFFFF',
|
74
|
+
size: 32,
|
75
|
+
font: "#{fonts}/Roboto.ttf" }
|
76
|
+
end
|
77
|
+
|
78
|
+
def parse_options(opts)
|
79
|
+
opts = default_options.merge(opts)
|
80
|
+
opts[:size] = opts[:size].to_i
|
81
|
+
opts[:font] = default_options[:font] unless Pathname(opts[:font]).exist?
|
82
|
+
opts[:font_size] ||= opts[:size] / 2
|
83
|
+
opts[:font_size] = opts[:font_size].to_i
|
84
|
+
opts
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: avatarly
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lukasz Odziewa
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rmagick
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rfc822
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Avatarly is a simple gem for Ruby that creates gmail-like avatars with
|
42
|
+
initials, inspired by avatar-generator by johnnyhalife. See homepage for more information.
|
43
|
+
email: lukasz.odziewa@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- README.md
|
49
|
+
- LICENSE
|
50
|
+
- Gemfile
|
51
|
+
- lib/avatarly.rb
|
52
|
+
- assets/fonts/Roboto.ttf
|
53
|
+
homepage: https://github.com/lucek/avatarly
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata: {}
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 2.1.11
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Simple gem for creating gmail-like user avatars with initials.
|
77
|
+
test_files: []
|