avatarize 0.1.0
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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +50 -0
- data/app/assets/javascripts/avatarize.js +47 -0
- data/lib/avatarize.rb +12 -0
- data/lib/avatarize/engine.rb +9 -0
- data/lib/avatarize/helper.rb +24 -0
- data/lib/avatarize/version.rb +3 -0
- metadata +91 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 67526e89034102b5bad76ccb4e79f41e7d2b42aaee671e12b2ebe83c06b64a4d
|
|
4
|
+
data.tar.gz: a76e75fc26301c9de10cac2f8e63bd46a1b16970d178acdb5959e59fac331521
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d0e722b5433f4ccd3849f582e2719c9e714a311aa95208376bd22552968d99750c78e1f4e051f2e7a64afbee9c3a90cc733d150f86336b13a082a049c70646f7
|
|
7
|
+
data.tar.gz: 3a3cb2da0544ccc1b68b0cd224177748158f23a963aa1ba3653c4f01498e198d70988e90cda61debb36889fca4b61ac44a413f43cc1e9641b451ebf6c35daf43
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Roger Bagué
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Avatarize
|
|
2
|
+
|
|
3
|
+
Gem to create letter avatars from users name (or any string).
|
|
4
|
+
It uses HTML canvas to generate the image (base64).
|
|
5
|
+
|
|
6
|
+
Based on [Lee Crossley's](https://github.com/leecrossley) [gist](https://gist.github.com/leecrossley/6027780)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Add this line to your application's `Gemfile`:
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
gem 'avatarize'
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Add the following to `application.js`:
|
|
17
|
+
```js
|
|
18
|
+
//= require avatarize
|
|
19
|
+
|
|
20
|
+
window.addEventListener("turbolinks:load", Avatarizer.processAll);
|
|
21
|
+
// Or this if not uing turbolinks
|
|
22
|
+
window.addEventListener("load", Avatarizer.processAll);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
To create an avatar:
|
|
28
|
+
```erb
|
|
29
|
+
<%= avatar_tag 'Avatarize' %>
|
|
30
|
+
|
|
31
|
+
# Custom size
|
|
32
|
+
<%= avatar_tag 'Avatarize', 200 %>
|
|
33
|
+
|
|
34
|
+
# Other html attributes
|
|
35
|
+
<%= avatar_tag 'Avatarize', nil, class: 'rounded-circle', title: 'Avatar' %>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Contributing
|
|
39
|
+
|
|
40
|
+
Bug reports and pull requests are welcome.
|
|
41
|
+
|
|
42
|
+
1. Fork the project https://github.com/rbague/avatarize/fork
|
|
43
|
+
2. Run `bundle` and `bundle exec rake`
|
|
44
|
+
3. Make your feature or bug fix
|
|
45
|
+
4. Add tests for it. This is important so that it does not break in a future version.
|
|
46
|
+
5. Submit a new Pull Request
|
|
47
|
+
|
|
48
|
+
## License
|
|
49
|
+
|
|
50
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Avatarizer.js
|
|
3
|
+
*
|
|
4
|
+
* Roger Bagué Martí
|
|
5
|
+
* Create a letter avatar based on the initial
|
|
6
|
+
* MIT License
|
|
7
|
+
*/
|
|
8
|
+
(function (global, factory) {
|
|
9
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
10
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
11
|
+
(global.Avatarizer = factory());
|
|
12
|
+
}(this, (function () { 'use strict';
|
|
13
|
+
const COLOURS = ["#1abc9c", "#2ecc71", "#3498db", "#9b59b6", "#34495e", "#16a085", "#27ae60", "#2980b9", "#8e44ad", "#2c3e50", "#f1c40f", "#e67e22", "#e74c3c", "#ecf0f1", "#95a5a6", "#f39c12", "#d35400", "#c0392b", "#bdc3c7", "#7f8c8d"];
|
|
14
|
+
|
|
15
|
+
const Avatarizer = {
|
|
16
|
+
process: function (source, size) {
|
|
17
|
+
size = size || 100;
|
|
18
|
+
if (window.devicePixelRatio) { size = size * window.devicePixelRatio; }
|
|
19
|
+
|
|
20
|
+
var content = String(source || "").toUpperCase().charAt(0);
|
|
21
|
+
var colourIndex = (content.charCodeAt(0) - 64) % COLOURS.length;
|
|
22
|
+
|
|
23
|
+
let canvas = document.createElement("canvas");
|
|
24
|
+
canvas.width = size;
|
|
25
|
+
canvas.height = size;
|
|
26
|
+
|
|
27
|
+
let context = canvas.getContext("2d");
|
|
28
|
+
context.fillStyle = COLOURS[colourIndex - 1];
|
|
29
|
+
context.fillRect(0, 0, canvas.width, canvas.height);
|
|
30
|
+
context.font = Math.round(canvas.width / 1.333) + "px Arial";
|
|
31
|
+
context.textAlign = "center";
|
|
32
|
+
context.fillStyle = "#FFFFFF";
|
|
33
|
+
context.fillText(content, size / 2, Math.round(canvas.width / 1.333));
|
|
34
|
+
|
|
35
|
+
return canvas.toDataURL();
|
|
36
|
+
},
|
|
37
|
+
processAll: function () {
|
|
38
|
+
document.querySelectorAll("img[data-avatar-content]").forEach(function(element) {
|
|
39
|
+
let source = element.getAttribute("data-avatar-content");
|
|
40
|
+
element.src = Avatarizer.process(source, element.getAttribute("data-avatar-size"));
|
|
41
|
+
element.setAttribute("alt", source);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
return Avatarizer;
|
|
47
|
+
})));
|
data/lib/avatarize.rb
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'avatarize/version'
|
|
4
|
+
require 'avatarize/engine' if defined?(Rails)
|
|
5
|
+
|
|
6
|
+
module Avatarize # :nodoc:
|
|
7
|
+
class << self
|
|
8
|
+
attr_accessor :defaults
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
self.defaults = { data: { avatar_content: '?', avatar_size: 100 } }
|
|
12
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module Avatarize
|
|
2
|
+
# Module Helper provides a method to generate the avatar in views
|
|
3
|
+
module Helper
|
|
4
|
+
def avatar_tag(content, size = nil, html_options = {})
|
|
5
|
+
avatar = avatarize_deep_copy(Avatarize.defaults)
|
|
6
|
+
avatar[:data][:avatar_content] = content unless content.nil? || content.empty?
|
|
7
|
+
avatar[:data][:avatar_size] = size unless size.nil? || size <= 0
|
|
8
|
+
|
|
9
|
+
tag(:img, html_options.merge(avatar))
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
private
|
|
13
|
+
|
|
14
|
+
def avatarize_deep_copy(source)
|
|
15
|
+
source.each_with_object(source.dup) do |(key, value), hash|
|
|
16
|
+
hash[key] = if value.is_a?(Hash)
|
|
17
|
+
avatarize_deep_copy(value)
|
|
18
|
+
else
|
|
19
|
+
value
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: avatarize
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Roger Bagué
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-03-12 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
description:
|
|
56
|
+
email: rogerbague@gmail.com
|
|
57
|
+
executables: []
|
|
58
|
+
extensions: []
|
|
59
|
+
extra_rdoc_files: []
|
|
60
|
+
files:
|
|
61
|
+
- LICENSE
|
|
62
|
+
- README.md
|
|
63
|
+
- app/assets/javascripts/avatarize.js
|
|
64
|
+
- lib/avatarize.rb
|
|
65
|
+
- lib/avatarize/engine.rb
|
|
66
|
+
- lib/avatarize/helper.rb
|
|
67
|
+
- lib/avatarize/version.rb
|
|
68
|
+
homepage: https://github.com/rbague/avatarize
|
|
69
|
+
licenses:
|
|
70
|
+
- MIT
|
|
71
|
+
metadata: {}
|
|
72
|
+
post_install_message:
|
|
73
|
+
rdoc_options: []
|
|
74
|
+
require_paths:
|
|
75
|
+
- lib
|
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
|
+
requirements:
|
|
78
|
+
- - ">="
|
|
79
|
+
- !ruby/object:Gem::Version
|
|
80
|
+
version: '0'
|
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
|
+
requirements:
|
|
83
|
+
- - ">="
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: '0'
|
|
86
|
+
requirements: []
|
|
87
|
+
rubygems_version: 3.0.3
|
|
88
|
+
signing_key:
|
|
89
|
+
specification_version: 4
|
|
90
|
+
summary: Create nice avatars from your user's initial
|
|
91
|
+
test_files: []
|