feat: add image optimizer module

- Add Optimizer module with lossless WebP conversion
- Generate responsive variants at [400, 800, 1200] widths
- Only create sizes <= source dimensions (no upscaling)
- Support AVIF, WebP, and JPEG output formats
- Add disk cache at priv/static/image_cache/
- Add comprehensive test suite (12 tests)
- Add image fixtures helper for testing

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-21 22:16:21 +00:00
parent cefec1aabd
commit 2b5b749a69
4 changed files with 335 additions and 0 deletions

BIN
test/fixtures/sample_1200x800.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View File

@@ -0,0 +1,121 @@
defmodule SimpleshopTheme.Images.OptimizerTest do
use SimpleshopTheme.DataCase, async: false
alias SimpleshopTheme.Images.Optimizer
import SimpleshopTheme.ImageFixtures
setup do
cleanup_cache()
on_exit(&cleanup_cache/0)
:ok
end
describe "applicable_widths/1" do
test "returns all widths for large source" do
assert [400, 800, 1200] = Optimizer.applicable_widths(1500)
end
test "excludes widths larger than source" do
assert [400, 800] = Optimizer.applicable_widths(900)
assert [400] = Optimizer.applicable_widths(500)
end
test "returns source width when smaller than minimum" do
assert [300] = Optimizer.applicable_widths(300)
assert [50] = Optimizer.applicable_widths(50)
end
end
describe "to_lossless_webp/1" do
test "converts image and returns dimensions" do
{:ok, webp, width, height} = Optimizer.to_lossless_webp(sample_jpeg())
assert is_binary(webp)
assert width == 1200
assert height == 800
# WebP magic bytes: RIFF....WEBP
assert <<"RIFF", _size::binary-size(4), "WEBP", _::binary>> = webp
end
test "returns error for invalid data" do
assert {:error, _} = Optimizer.to_lossless_webp("not an image")
end
end
describe "process_for_image/1" do
test "generates all variants for 1200px image" do
image = image_fixture(%{source_width: 1200, source_height: 800})
assert {:ok, [400, 800, 1200]} = Optimizer.process_for_image(image.id)
for w <- [400, 800, 1200], fmt <- [:avif, :webp, :jpg] do
assert File.exists?(cache_path(image.id, w, fmt)),
"Missing #{w}.#{fmt}"
end
assert File.exists?(cache_path(image.id, "thumb", :jpg))
end
test "generates only applicable widths for smaller image" do
# Create fixture with smaller source width
{:ok, webp, _w, _h} = Optimizer.to_lossless_webp(sample_jpeg())
image =
%SimpleshopTheme.Media.Image{}
|> SimpleshopTheme.Media.Image.changeset(%{
image_type: "product",
filename: "small.jpg",
content_type: "image/webp",
file_size: byte_size(webp),
data: webp,
source_width: 600,
source_height: 400,
variants_status: "pending",
is_svg: false
})
|> Repo.insert!()
assert {:ok, [400]} = Optimizer.process_for_image(image.id)
assert File.exists?(cache_path(image.id, 400, :avif))
refute File.exists?(cache_path(image.id, 800, :avif))
end
test "skips SVG images" do
image = svg_fixture()
assert {:ok, :svg_skipped} = Optimizer.process_for_image(image.id)
end
test "returns error for missing image" do
assert {:error, :not_found} = Optimizer.process_for_image(Ecto.UUID.generate())
end
test "is idempotent - skips existing files" do
image = image_fixture(%{source_width: 1200, source_height: 800})
{:ok, _} = Optimizer.process_for_image(image.id)
path = cache_path(image.id, 400, :avif)
{:ok, %{mtime: mtime1}} = File.stat(path)
Process.sleep(1100)
{:ok, _} = Optimizer.process_for_image(image.id)
{:ok, %{mtime: mtime2}} = File.stat(path)
assert mtime1 == mtime2, "File was regenerated"
end
end
describe "disk_variants_exist?/2" do
test "returns true when all variants exist" do
image = image_fixture(%{source_width: 1200, source_height: 800})
{:ok, _} = Optimizer.process_for_image(image.id)
assert Optimizer.disk_variants_exist?(image.id, 1200)
end
test "returns false when variants missing" do
image = image_fixture(%{source_width: 1200, source_height: 800})
refute Optimizer.disk_variants_exist?(image.id, 1200)
end
end
end

View File

@@ -0,0 +1,73 @@
defmodule SimpleshopTheme.ImageFixtures do
@moduledoc """
Shared fixtures for image-related tests.
"""
alias SimpleshopTheme.Repo
alias SimpleshopTheme.Media.Image
@sample_jpeg File.read!("test/fixtures/sample_1200x800.jpg")
def sample_jpeg, do: @sample_jpeg
def image_fixture(attrs \\ %{}) do
{:ok, webp, w, h} = SimpleshopTheme.Images.Optimizer.to_lossless_webp(@sample_jpeg)
defaults = %{
image_type: "product",
filename: "test.jpg",
content_type: "image/webp",
file_size: byte_size(webp),
data: webp,
source_width: w,
source_height: h,
variants_status: "pending",
is_svg: false
}
%Image{}
|> Image.changeset(Map.merge(defaults, attrs))
|> Repo.insert!()
end
def svg_fixture do
svg = ~s(<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect/></svg>)
%Image{}
|> Image.changeset(%{
image_type: "logo",
filename: "logo.svg",
content_type: "image/svg+xml",
file_size: byte_size(svg),
data: svg,
is_svg: true,
svg_content: svg,
variants_status: "pending"
})
|> Repo.insert!()
end
def cache_path(id, width, format) do
ext =
case format do
:jpg -> "jpg"
:webp -> "webp"
:avif -> "avif"
"thumb" -> "jpg"
_ -> to_string(format)
end
filename =
case width do
"thumb" -> "#{id}-thumb.#{ext}"
_ -> "#{id}-#{width}.#{ext}"
end
Path.join(SimpleshopTheme.Images.Optimizer.cache_dir(), filename)
end
def cleanup_cache do
cache_dir = SimpleshopTheme.Images.Optimizer.cache_dir()
if File.exists?(cache_dir), do: File.rm_rf!(cache_dir)
end
end