2024-02-24 19:52:57 -05:00
|
|
|
defmodule SloaneSH.Assets.Image do
|
|
|
|
alias SloaneSH.Asset
|
|
|
|
alias SloaneSH.OutputDirs
|
|
|
|
|
|
|
|
@behaviour Asset
|
|
|
|
|
|
|
|
@impl Asset
|
|
|
|
def extensions(_cfg), do: ~w[.jpg .jpeg .png .webp .gif]
|
|
|
|
|
|
|
|
@impl Asset
|
2024-02-24 21:11:19 -05:00
|
|
|
def attrs(_cfg, _path, data) do
|
|
|
|
{:ok, image} = Image.from_binary(data)
|
|
|
|
|
|
|
|
aspect = Image.aspect(image)
|
|
|
|
{width, height, _} = Image.shape(image)
|
|
|
|
|
|
|
|
{:ok, %{aspect: aspect, width: width, height: height}, image}
|
2024-02-24 19:52:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl Asset
|
|
|
|
def render(cfg, _ctx, path, data, _attrs) do
|
2024-02-24 21:11:19 -05:00
|
|
|
formats = ~w[.webp .png .jpg]
|
|
|
|
|
|
|
|
outputs =
|
|
|
|
for format <- formats do
|
|
|
|
format_path = OutputDirs.replace_ext(path, format)
|
|
|
|
output_path = OutputDirs.image(cfg, format_path)
|
|
|
|
|
|
|
|
{:ok, converted} =
|
|
|
|
Image.write(data, :memory,
|
|
|
|
suffix: format,
|
|
|
|
strip_metadata: true,
|
|
|
|
minimize_file_size: true,
|
|
|
|
quality: 80
|
|
|
|
)
|
|
|
|
|
|
|
|
{output_path, converted}
|
|
|
|
end
|
|
|
|
|
|
|
|
{:ok, outputs}
|
2024-02-24 19:52:57 -05:00
|
|
|
end
|
|
|
|
end
|