widthxheight support
This commit is contained in:
@@ -16,9 +16,14 @@ defmodule LabelmakerWeb.LabelController do
|
||||
|
||||
filename = filename <> ".png"
|
||||
filepath = Path.join(@label_dir, filename)
|
||||
options = Map.put(options, :filepath, filepath)
|
||||
|
||||
unless File.exists?(filepath) do
|
||||
generate_image(options, filepath)
|
||||
basic_settings(options)
|
||||
|> size_settings(options)
|
||||
|> outline_settings(options)
|
||||
|> final_settings(options)
|
||||
|> generate_image()
|
||||
end
|
||||
|
||||
conn
|
||||
@@ -26,36 +31,61 @@ defmodule LabelmakerWeb.LabelController do
|
||||
|> send_file(200, filepath)
|
||||
end
|
||||
|
||||
defp generate_image(options, filepath) do
|
||||
File.mkdir_p!(@label_dir)
|
||||
|
||||
args = [
|
||||
defp basic_settings(options) do
|
||||
[
|
||||
"-background",
|
||||
"none",
|
||||
"-fill",
|
||||
options.color,
|
||||
"-pointsize",
|
||||
options.size,
|
||||
"-font",
|
||||
options.font,
|
||||
"label:#{String.slice(options.label, 0, Constants.max_label_length())}",
|
||||
"-set",
|
||||
"comment",
|
||||
inspect(Jason.encode!(Map.drop(options, [:link]))),
|
||||
filepath
|
||||
options.font
|
||||
]
|
||||
end
|
||||
|
||||
args =
|
||||
if options.outline != "none" do
|
||||
[
|
||||
"-stroke",
|
||||
options.outline,
|
||||
"-strokewidth",
|
||||
"1"
|
||||
] ++ args
|
||||
else
|
||||
args
|
||||
end
|
||||
defp size_settings(args, %{height: 0, width: 0} = options) do
|
||||
args ++
|
||||
[
|
||||
"-pointsize",
|
||||
options.size,
|
||||
"label:#{String.slice(options.label, 0, Constants.max_label_length())}"
|
||||
]
|
||||
end
|
||||
|
||||
defp size_settings(args, %{gravity: gravity, height: height, width: width} = options) do
|
||||
args ++
|
||||
[
|
||||
"-gravity",
|
||||
gravity,
|
||||
"-size",
|
||||
"#{width}x#{height}",
|
||||
"caption:#{String.slice(options.label, 0, Constants.max_label_length())}"
|
||||
]
|
||||
end
|
||||
|
||||
defp outline_settings(args, %{outline: "none"}), do: args
|
||||
|
||||
defp outline_settings(args, %{outline: color}) do
|
||||
args ++
|
||||
[
|
||||
"-stroke",
|
||||
color,
|
||||
"-strokewidth",
|
||||
"1"
|
||||
]
|
||||
end
|
||||
|
||||
defp final_settings(args, options) do
|
||||
args ++
|
||||
[
|
||||
"-set",
|
||||
"comment",
|
||||
inspect(Jason.encode!(Map.drop(options, [:filepath, :link]))),
|
||||
options.filepath
|
||||
]
|
||||
end
|
||||
|
||||
defp generate_image(args) do
|
||||
File.mkdir_p!(@label_dir)
|
||||
|
||||
{_, 0} = System.cmd("magick", args)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user