feat: enhance image optimization with on-demand JPEG fallbacks

Improve the image optimization pipeline with better compression and
smarter variant generation:

- Change to_lossless_webp → to_optimized_webp (lossy, quality 90)
- Auto-resize uploads larger than 2000px to save storage
- Skip pre-generating JPEG variants (~50% disk savings)
- Add on-demand JPEG generation for legacy browsers (<5% of users)
- Add /images/:id/variant/:width route for dynamic serving
- Add VariantCache to supervision tree for startup validation
- Add image_cache to static paths for disk-based serving

The pipeline now stores smaller WebP sources and generates AVIF/WebP
variants upfront, with JPEG generated only when legacy browsers request it.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-25 00:33:09 +00:00
parent 252ca2268a
commit 2bc05097b9
11 changed files with 610 additions and 79 deletions

View File

@@ -0,0 +1,51 @@
defmodule SimpleshopTheme.Images.OptimizeWorkerTest do
use SimpleshopTheme.DataCase, async: false
use Oban.Testing, repo: SimpleshopTheme.Repo
alias SimpleshopTheme.Images.OptimizeWorker
import SimpleshopTheme.ImageFixtures
setup do
cleanup_cache()
on_exit(&cleanup_cache/0)
:ok
end
describe "perform/1" do
test "processes image and generates variants" do
image = image_fixture(%{source_width: 1200, source_height: 800})
assert :ok = perform_job(OptimizeWorker, %{image_id: image.id})
# Verify pre-generated variants were created (AVIF and WebP only, not JPEG)
for w <- [400, 800, 1200], fmt <- [:avif, :webp] do
assert File.exists?(cache_path(image.id, w, fmt))
end
# JPEG is generated on-demand, not pre-generated
refute File.exists?(cache_path(image.id, 400, :jpg))
end
test "cancels for missing image" do
assert {:cancel, :image_not_found} =
perform_job(OptimizeWorker, %{image_id: Ecto.UUID.generate()})
end
test "skips SVG images" do
image = svg_fixture()
assert :ok = perform_job(OptimizeWorker, %{image_id: image.id})
end
end
describe "enqueue/1" do
test "inserts and executes job (inline mode)" do
image = image_fixture(%{source_width: 1200, source_height: 800})
# In inline test mode, job executes immediately
assert {:ok, %Oban.Job{state: "completed"}} = OptimizeWorker.enqueue(image.id)
# Verify variants were created (job ran inline)
assert File.exists?(cache_path(image.id, 400, :avif))
end
end
end

View File

@@ -26,9 +26,9 @@ defmodule SimpleshopTheme.Images.OptimizerTest do
end
end
describe "to_lossless_webp/1" do
describe "to_optimized_webp/1" do
test "converts image and returns dimensions" do
{:ok, webp, width, height} = Optimizer.to_lossless_webp(sample_jpeg())
{:ok, webp, width, height} = Optimizer.to_optimized_webp(sample_jpeg())
assert is_binary(webp)
assert width == 1200
@@ -37,28 +37,47 @@ defmodule SimpleshopTheme.Images.OptimizerTest do
assert <<"RIFF", _size::binary-size(4), "WEBP", _::binary>> = webp
end
test "resizes images larger than 2000px" do
# Create a large test image by scaling up
{:ok, image} = Image.open("test/fixtures/sample_1200x800.jpg")
{:ok, large} = Image.thumbnail(image, 3000)
{:ok, large_data} = Image.write(large, :memory, suffix: ".jpg")
{:ok, webp, width, height} = Optimizer.to_optimized_webp(large_data)
assert is_binary(webp)
assert width == 2000
# Height should be proportionally scaled
assert height <= 2000
end
test "returns error for invalid data" do
assert {:error, _} = Optimizer.to_lossless_webp("not an image")
assert {:error, _} = Optimizer.to_optimized_webp("not an image")
end
end
describe "process_for_image/1" do
test "generates all variants for 1200px image" do
test "generates AVIF and WebP 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
# Only AVIF and WebP are pre-generated (JPEG is on-demand)
for w <- [400, 800, 1200], fmt <- [:avif, :webp] do
assert File.exists?(cache_path(image.id, w, fmt)),
"Missing #{w}.#{fmt}"
end
# JPEG should NOT be pre-generated
refute File.exists?(cache_path(image.id, 400, :jpg))
# Thumbnail is still pre-generated as JPEG
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())
{:ok, webp, _w, _h} = Optimizer.to_optimized_webp(sample_jpeg())
image =
%SimpleshopTheme.Media.Image{}
@@ -106,10 +125,11 @@ defmodule SimpleshopTheme.Images.OptimizerTest do
end
describe "disk_variants_exist?/2" do
test "returns true when all variants exist" do
test "returns true when all pre-generated variants exist" do
image = image_fixture(%{source_width: 1200, source_height: 800})
{:ok, _} = Optimizer.process_for_image(image.id)
# Should return true even without JPEG (only checks AVIF/WebP)
assert Optimizer.disk_variants_exist?(image.id, 1200)
end
@@ -118,4 +138,36 @@ defmodule SimpleshopTheme.Images.OptimizerTest do
refute Optimizer.disk_variants_exist?(image.id, 1200)
end
end
describe "generate_jpeg_on_demand/3" do
test "generates JPEG variant and caches to disk" do
image = image_fixture(%{source_width: 1200, source_height: 800})
# JPEG shouldn't exist yet
refute File.exists?(cache_path(image.id, 400, :jpg))
# Generate on-demand
{:ok, path} = Optimizer.generate_jpeg_on_demand(image.data, image.id, 400)
assert File.exists?(path)
assert path == cache_path(image.id, 400, :jpg)
end
test "returns cached path if JPEG already exists" do
image = image_fixture(%{source_width: 1200, source_height: 800})
# Generate first time
{:ok, path1} = Optimizer.generate_jpeg_on_demand(image.data, image.id, 800)
{:ok, %{mtime: mtime1}} = File.stat(path1)
Process.sleep(1100)
# Generate second time - should return cached
{:ok, path2} = Optimizer.generate_jpeg_on_demand(image.data, image.id, 800)
{:ok, %{mtime: mtime2}} = File.stat(path2)
assert path1 == path2
assert mtime1 == mtime2, "File was regenerated instead of cached"
end
end
end