Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pr at https://github.com/zhangsoledad/alchemic_avatar/pull/4/files #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,14 @@ AlchemicAvatar.generate "ksz2k", 200
iex> Pinyin.from_string("龡龢龣龤龥癩")
"chui he jue xie yu la"
```


# Creating letter avatar from user's name
config :alchemic_avatar,
global_path?: true,
cache_base_path: System.user_home() <> "/media/users",
colors_palette: :iwanthue,
weight: 500,
annotate_position: "-0+10",
app_name: :project_name

71 changes: 47 additions & 24 deletions lib/alchemic_avatar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule AlchemicAvatar do
def generate(username, size, opts \\ []) do
cache = Keyword.get(opts, :cache, true)
identity = identity(username)
a_size = [size, @fullsize] |> Enum.min
a_size = [size, @fullsize] |> Enum.min()
do_generate(identity, a_size, cache)
end

Expand Down Expand Up @@ -52,12 +52,17 @@ defmodule AlchemicAvatar do
end

defp cache_path do
"#{AlchemicAvatar.Config.cache_base_path}/alchemic_avatar"
"#{AlchemicAvatar.Config.cache_base_path()}/alchemic_avatar"
end

defp dir_path(identity) do
path = "#{cache_path()}/#{identity.letter}/#{identity.color |> Enum.join("_")}"
:code.priv_dir(AlchemicAvatar.Config.app_name) |> Path.join(path)

if AlchemicAvatar.Config.global_path?() do
path
else
:code.priv_dir(AlchemicAvatar.Config.app_name()) |> Path.join(path)
end
end

defp filename(identity, size) do
Expand All @@ -66,48 +71,66 @@ defmodule AlchemicAvatar do

defp mk_path(path) do
unless File.exists?(path) do
File.mkdir_p path
File.mkdir_p(path)
end

:ok
end

defp convert_file(identity, size) do
mk_path dir_path(identity)
mk_path(dir_path(identity))
filename = filename(identity, size)
System.cmd "convert", [
"-size", "#{size}x#{size}",

System.cmd("convert", [
"-size",
"#{size}x#{size}",
"xc:#{to_rgb(identity.color)}",
"-pointsize", "140",
"-font", "#{@font_filename}",
"-weight", "#{AlchemicAvatar.Config.weight}",
"-fill", "#{@fill_color}",
"-gravity", "Center",
"-annotate", "#{AlchemicAvatar.Config.annotate_position}", "#{identity.letter}",
"-pointsize",
"140",
"-font",
"#{@font_filename}",
"-weight",
"#{AlchemicAvatar.Config.weight()}",
"-fill",
"#{@fill_color}",
"-gravity",
"Center",
"-annotate",
"#{AlchemicAvatar.Config.annotate_position()}",
"#{identity.letter}",
"#{filename}"
]
])

filename
end

defp resize(from, to, width, height) do
System.cmd "convert", [
System.cmd("convert", [
"#{from}",
"-background", "transparent",
"-gravity", "center",
"-thumbnail", "#{width}x#{height}^",
"-extent", "#{width}x#{height}",
"-interpolate", "catrom",
"-unsharp", "2x0.5+0.7+0",
"-quality", "98",
"-background",
"transparent",
"-gravity",
"center",
"-thumbnail",
"#{width}x#{height}^",
"-extent",
"#{width}x#{height}",
"-interpolate",
"catrom",
"-unsharp",
"2x0.5+0.7+0",
"-quality",
"98",
"#{to}"
]
])
end

defp convert_fullsize(identity) do
convert_file(identity, @fullsize)
end

defp to_rgb(color) do
[r, g, b ] = color
[r, g, b] = color
"rgb(#{r},#{g},#{b})"
end
end
20 changes: 15 additions & 5 deletions lib/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule AlchemicAvatar.Config do
| colors_palette | atom | :google or :iwanthue |
| weight | int | 300 |
| annotate_position | binary | "-0+5" |
| global_path? | boolean | false |
| app_name | atom | N/A |
"""

Expand All @@ -27,15 +28,17 @@ defmodule AlchemicAvatar.Config do
"""
def app_name do
case Keyword.fetch(@config, :app_name) do
{:ok, name} -> name
{:ok, name} ->
name

_ ->
raise ArgumentError, message: """
you need set app_name in your config
"""
raise ArgumentError,
message: """
you need set app_name in your config
"""
end
end


@doc """
colors_palette
"""
Expand All @@ -56,4 +59,11 @@ defmodule AlchemicAvatar.Config do
def annotate_position do
Keyword.get(@config, :annotate_position, "-0+5")
end

@doc """
global_path?
"""
def global_path? do
Keyword.get(@config, :global_path?, false)
end
end