- 主要作用:用于给网站用户默认图像,当用户未上传图像的时候,给用户产生默认的图像.
- 相关方法使用和源码解析
方法1: setup(&_block),这个方法主要设置生成图片需要的一些值.
LetterAvatar.setup do |config|
config.fill_color = 'rgba(255, 255, 255, 1)' # default is 'rgba(255, 255, 255, 0.65)'
config.cache_base_path = 'public/system/lets' # default is 'public/system'
config.colors_palette = :iwanthue # default is :google
config.weight = 500 # default is 300
config.annotate_position = '-0+10' # default is -0+5
end
源码:
module LetterAvatar
extend LetterAvatar::Configuration
def self.setup(&_block)
yield(self)
end
end
setup方法需要传入一个代码块,同时代码块需要传递一个参数,此处传递的self代表LetterAvatar.
因此代码块内部的设值操作其实是调用了LetterAvatar的类方法.
这些类方法并未在代码中定义而是通过
extend LetterAvatar::Configuration
获取到的.详细可以看LetterAvatar::Configuration定义
module LetterAvatar
module Configuration
def cache_base_path
@cache_base_path
end
def cache_base_path=(v)
@cache_base_path = v
end
def fill_color
@fill_color || Avatar::FILL_COLOR
end
def fill_color=(v)
@fill_color = v
end
def colors_palette
@colors_palette ||= :google
end
def colors_palette=(v)
@colors_palette = v if v.in?(Colors::PALETTES)
end
def weight
@weight ||= 300
end
def weight=(v)
@weight = v
end
def annotate_position
@annotate_position ||= '-0+5'
end
def annotate_position=(v)
@annotate_position = v
end
end
end
方法2:generate(username, size),用于生成图片.主要使用到了imagemagick.根据username首字母和size(默认会生成一张240*240图片)生成图片
module LetterAvatar
extend LetterAvatar::Configuration
def self.generate(username, size)
Avatar.generate(username, size)
end
end
因此方法的实现代码在Avatar中:
module LetterAvatar
class Avatar
class << self
class Identity
attr_accessor :color, :letter
def self.from_username(username)
identity = new
identity.color = LetterAvatar::Colors.for(username)
identity.letter = username[0].upcase
identity
end
end
def generate(username, size, opts = nil)
identity = Identity.from_username(username)
cache = true
cache = false if opts && opts[:cache] == false
size = FULLSIZE if size > FULLSIZE
filename = cached_path(identity, size)
return filename if cache && File.exist?(filename)
fullsize = fullsize_path(identity)
generate_fullsize(identity) if !cache || !File.exist?(fullsize)
LetterAvatar.resize(fullsize, filename, size, size)
filename
end
end
end
end
Identify可以认为是一个局部类,此处主要功能是获取生成图片的两个重要元素,color,letter.
获取颜色的代码如下:
module LetterAvatar
module Colors
PALETTES = [:google, :iwanthue]
GOOGLE_COLORS = [
[226, 95, 81], # A
[242, 96, 145], # B
[187, 101, 202], # C
[149, 114, 207], # D
[120, 132, 205], # E
[91, 149, 249], # F
[72, 194, 249], # G
[69, 208, 226], # H
[72, 182, 172], # I
[82, 188, 137], # J
[155, 206, 95], # K
[212, 227, 74], # L
[254, 218, 16], # M
[247, 192, 0], # N
[255, 168, 0], # O
[255, 138, 96], # P
[194, 194, 194], # Q
[143, 164, 175], # R
[162, 136, 126], # S
[163, 163, 163], # T
[175, 181, 226], # U
[179, 155, 221], # V
[194, 194, 194], # W
[124, 222, 235], # X
[188, 170, 164], # Y
[173, 214, 125] # Z
].freeze
def self.for(username)
public_send("with_#{LetterAvatar.colors_palette}", username)
end
def self.with_google(username)
char = username[0].upcase
if /[A-Z]/ =~ char
# 65 is 'A' ord
idx = char.ord - 65
google[idx]
elsif /[\d]/ =~ char
google[char.to_i]
else
google[Digest::MD5.hexdigest(username)[0...15].to_i(16) % google.length]
end
end
# Colors form Google Inbox
# https://inbox.google.com
def self.google
GOOGLE_COLORS
end
end
end
LetterAvatar::Color通过一个动态方法public_send调用with_google获取首字母对于的数组GOOGLE_COLORS中的颜色值
接着生成图片的路径,因为会默认生成一个240*240的图片,因此调用了fullsize_path方法.
def cached_path(identity, size)
dir = "#{cache_path}/#{identity.letter}/#{identity.color.join('_')}"
FileUtils.mkdir_p(dir)
"#{dir}/#{size}.png"
end
def fullsize_path(identity)
cached_path(identity, FULLSIZE)
end
generate_fullsize(identity) 是真正用于产生图片的方法.主要就是使用imagemagick强大的图片裁剪功能.
def generate_fullsize(identity)
filename = fullsize_path(identity)
LetterAvatar.execute(
%W(
convert
-size #{FULLSIZE}x#{FULLSIZE}
xc:#{to_rgb(identity.color)}
-pointsize 140
-font #{FONT_FILENAME}
-weight #{LetterAvatar.weight}
-fill '#{LetterAvatar.fill_color}'
-gravity Center
-annotate #{LetterAvatar.annotate_position} '#{identity.letter}'
'#{filename}'
).join(' ')
)
filename
end
最后调用此方法对图片进行裁剪,生成用户需要的大小的图片.
LetterAvatar.resize(fullsize, filename, size, size)