Skip to content

feat(libvips): add my-media-libvips, a media_image plugin with libvips - #19

Merged
rosylilly merged 1 commit into
mainfrom
feat/media-libvips
Nov 26, 2026
Merged

rosylilly merged 1 commit into
mainfrom
feat/media-libvips

Conversation

@rosylilly

Copy link
Copy Markdown
Member

Summary

A new plugin, my-media-libvips, that implements MediaService.Process (media_image) with libvips. It makes the same outputs as my-media-imagemagick (formats, sizes, metadata removal, blurhash, the same bytes for the same input).

  • cgo, one worker process per image. The binary starts itself again with MY_MEDIA_LIBVIPS_WORKER=1 for each call. A decoder that crashes takes down that call only, a cancelled call kills its worker, and the memory of an image goes back to the system when the worker exits.
  • Only the loaders of the accepted formats (JPEG, PNG, GIF, WebP, HEIF) are unblocked. Debian's libvips also links PDF, SVG, TIFF, ImageMagick, and others, which stay blocked.
  • The limits are checked before a pixel is decoded. libvips does not expose the stored frame sizes of an animation, so frames.go reads them from the blocks of a GIF and the chunks of a WebP.
  • Animations are cropped and resized frame by frame, so avatars crop every frame alike and the kernel never reaches into the next frame. Resizing is Lanczos3 with premultiplied alpha.
  • AVIF and HEIC are declared only when this libvips decodes the embedded samples at start.

Differences from my-media-imagemagick

  • An APNG is not declared: libvips reads its default image, as the server reads one when APNG is not declared. To animate it, the APNG compositing of my-media-imagemagick/apng.go could move to internal/media.
  • No sharpening after a large reduction.
  • -concurrency defaults to 2 (the server sends at most two image calls at once), and each worker gets NumCPU / concurrency threads.

CI

go vet type-checks the cgo, so the lint job installs libvips-dev. The workflow change makes this PR test every plugin.

Test plan

  • go test -race ./... in the test stage of the image: 14 tests, none skipped (photo, wide gamut, transparent avatar, opaque PNG, emoji, GIF and WebP animations, APNG, AVIF and HEIC, HEIC rotated once, refusals, cancellation)
  • ./scripts/lint.sh in a container with libvips-dev
  • Contract: go run -C tools ./cmd/contract -plugin my-media-libvips with my v0.1.0-rc.12: 24 passed
  • A 4032×3024 JPEG on 2 CPUs: 0.29 s per image, 134 MB at peak, against 3.32 s and 188 MB for my-media-imagemagick

🤖 Generated with Claude Code

It makes the same outputs as my-media-imagemagick, about ten times faster on a
12-megapixel photo. The plugin links libvips (cgo) and processes each image in
a worker, a process of its own binary, so that a decoder that crashes takes
down one call, a cancelled call kills its worker, and the memory of an image
goes back with it. Only the loaders of the accepted formats are left
unblocked, and the limits are checked before a pixel is decoded, the frames of
a GIF and a WebP read from their blocks and chunks.

An APNG is not declared: libvips reads its default image, as the server reads
one when the plugin does not declare APNG.

go vet type-checks the cgo, so the lint job installs libvips-dev.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rosylilly
rosylilly merged commit cb94568 into main Nov 26, 2026
12 checks passed
@rosylilly
rosylilly deleted the feat/media-libvips branch November 26, 2026 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant