From d67e8853250c92c48c5a89e16b0493895b3c8905 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 4 Jan 2023 00:47:34 -0500 Subject: [PATCH] pr at https://github.com/zhangsoledad/alchemic_avatar/pull/4/files --- README.md | 11 +++++++ lib/alchemic_avatar.ex | 71 ++++++++++++++++++++++++++++-------------- lib/config.ex | 20 +++++++++--- 3 files changed, 73 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 654e161..6fd4463 100644 --- a/README.md +++ b/README.md @@ -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 + \ No newline at end of file diff --git a/lib/alchemic_avatar.ex b/lib/alchemic_avatar.ex index 840f0fe..6dd372e 100644 --- a/lib/alchemic_avatar.ex +++ b/lib/alchemic_avatar.ex @@ -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 @@ -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 @@ -66,40 +71,58 @@ 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 @@ -107,7 +130,7 @@ defmodule AlchemicAvatar do end defp to_rgb(color) do - [r, g, b ] = color + [r, g, b] = color "rgb(#{r},#{g},#{b})" end end diff --git a/lib/config.ex b/lib/config.ex index 27576b1..d6d6b92 100644 --- a/lib/config.ex +++ b/lib/config.ex @@ -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 | """ @@ -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 """ @@ -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