Hey @kip,
After admiring your work on Image library for a long time, I finally got a chance to use it in my project.
I have a mix task, that compiles the images from assets/images/*
directory and puts them into priv/images/*
directory.
It creates different variations for input images.
defmodule Mix.Tasks.Images.Compile do
@moduledoc "Compile all images of projects, inspired by Benjamin Milde's elixir blog."
@shortdoc "Compiles images"
use Mix.Task
@assets_folder "assets/images/"
@image_sizes [300, 720, 960, 1200, 2000]
@impl Mix.Task
def run(_args) do
src_paths =
(@assets_folder <> "**/*.{png,webp,jpeg,jpg}")
|> Path.wildcard()
|> dbg()
for src_path <- src_paths, size <- @image_sizes do
file_name = src_path |> Path.rootname() |> Path.basename()
ext = src_path |> Path.extname()
path = src_path |> Path.split() |> Enum.drop(1) |> Path.join()
dest_dir = "priv/static" |> Path.join(path) |> Path.dirname()
dest_path = "#{dest_dir}/#{file_name}@#{size}#{ext}"
{:ok, thumb} = Image.thumbnail(src_path, size)
File.mkdir_p!(dest_dir)
Image.write(thumb, dest_path,
quality: 70,
strip_metadata: true,
minimize_file_size: true,
compression: 6
)
end
end
end
This produces following output: (For PNGs)
However, when I run the same code on WebP, it doesn’t work! Filename, may have changed, but the sizes remain the same.
What is your thoughts on tools like imgproxy
?
After resize was done, I was thinking pre-compiling led to unnecessary resizing. For instance for images that are smaller than 500px, was being stretched to 1200px needlessly.
I will have to segregated folders and selectively resize them to different sizes, instead of resizing at runtime like imageproxy
does.