Image ingest for Go web apps — sniff, decode, downscale, re-encode, thumbnail, store.
Designed to be dropped into the same apps as go-ai, go-api, go-draw, go-wiki and go-login: one upload path, one naming rule, one place that decides what is safe to keep.
It solves the problem every app with an upload button hits on the same afternoon — a phone posts 12 MB of JPEG carrying the owner's GPS coordinates in its EXIF, and the app stores it verbatim.
- Re-encoding, not pass-through — strips EXIF (which carries location), guarantees the stored bytes really are the image that was decoded rather than a polyglot with a payload after the marker, and leaves one predictable format to serve.
- Sniffed, not declared — the Content-Type on a multipart part is client-controlled, so it is not evidence. A
.jpgthat is really a PHP script is refused. - Downscaled sharply — CatmullRom at the factors phone photos actually hit, which is what keeps small print on a photographed receipt or lab printout legible.
- Thumbnails — optional, written beside the image under a name derived from it, so an app remembers one path rather than two.
- Two naming rules, or your own — content-addressed (identical uploads collapse onto one file) or dated (
{owner}/{yyyy}/{mm}/…), and aNameris just a function. - Path-safe by construction — a stored name comes back out of a database, so a name that tries to climb out of the root is rejected rather than quietly rewritten into something inside it.
- Keeps what it cannot parse, if you ask —
KeepUnsupportedstores a PDF byte-for-byte alongside the photos, off by default.
One dependency: golang.org/x/image, for the WebP decoder and the scaler.
go get github.com/anchoo2kewl/go-photo
store, err := photo.NewStore("/data/photos", photo.Options{
MaxEdge: 1600,
ThumbEdge: 320,
Quality: 82,
MaxBytes: 25 << 20,
})
file, header, err := r.FormFile("file")
saved, err := store.Save(file, header.Filename, photo.Dated(userID, "progress"))
if err != nil {
// errors.Is(err, photo.ErrUnsupportedType) — not an image we accept
// errors.Is(err, photo.ErrTooLarge) — over MaxBytes
}
// Persist these; they are what the app needs to serve and delete the image.
saved.RelPath // "42/2026/09/progress-1757...-9f3c1a20.jpg"
saved.ThumbPath // "42/2026/09/progress-1757...-9f3c1a20-thumb.jpg"
saved.ContentType // "image/jpeg"
saved.Width, saved.Height
saved.Bytes() // stored size
saved.OriginalBytes
saved.Saved() // 0.86 — "86% smaller", for the upload confirmationServing and deleting go through the store, so the containment check is not something a handler can forget:
f, err := store.Open(rec.StoredName)
...
err = store.RemoveWithThumb(rec.StoredName)Process is the whole pipeline with no filesystem. Use it when the bytes have
somewhere else to go — an object store, or a vision model that decides whether
the upload is worth keeping at all:
img, err := photo.Process(raw, header.Filename, photo.Options{MaxEdge: 1600})
if err != nil {
return err
}
// A 1600px JPEG is what a vision model wants anyway: readable, and a fraction
// of the tokens of the original.
resp, err := chain.Complete(ctx, ai.Request{
Messages: []ai.Message{ai.UserImage("Read this water test.", img.ContentType, img.Data)},
})
saved, err := store.SaveImage(img, photo.ContentAddressed("attachments"))| Field | Default | Meaning |
|---|---|---|
MaxEdge |
1600 | Longest side of the stored image. Never upscales. Negative disables downscaling |
ThumbEdge |
0 | Longest side of the thumbnail. Zero produces none |
Quality |
82 | JPEG quality of the stored image |
ThumbQuality |
75 | JPEG quality of the thumbnail |
MaxBytes |
0 | Reject an input larger than this. Zero means no limit |
ForceRecompress |
false | Keep the re-encode even when it is larger, so every stored image is one this package produced |
KeepUnsupported |
false | Store an undecodable file (a PDF) byte-for-byte rather than rejecting it |
The zero value is usable.
A Namer turns a processed image into a path relative to the store root. It is
called after processing, so the digest and the content type are available:
photo.ContentAddressed("attachments")
// attachments/9f3c…8bd.jpg — identical uploads collapse onto one file
photo.Dated("42", "receipt")
// 42/2026/09/receipt-1757943210000000000-9f3c1a20.jpg
func(img *photo.Image) string { // or your own
return path.Join("pools", poolID, img.SHA256+img.Extension())
}Content-addressing is usually what an app wants, and is why an app using it must count the rows referencing a name before deleting the file behind it — the same bytes may back several records.
An already-optimised image, or noise, can encode larger than it arrived. If
the image did not need downscaling, the original bytes are kept and
Recompressed is false — storing the bigger of the two to satisfy a rule about
re-encoding would be the wrong trade, and the reported dimensions describe what
is actually stored.
An image that was downscaled keeps its re-encode regardless: the point of the resize is to stop serving a 4000px original, and a format that happened to compress the big version better does not change that.
Set ForceRecompress to drop the fallback entirely. Do it where the guarantee
matters more than the bytes — a re-encode is what strips EXIF, so an app that
promises uploaded photos carry no location data cannot afford to keep a phone's
original JPEG because it happened to be smaller.
- EXIF orientation. Rotation metadata is stripped along with everything
else, so an image relying on it is stored as its pixels were laid out. Almost
every camera and browser upload path now writes upright pixels; if yours does
not, rotate before calling
Process. - Animation. An animated GIF or WebP is stored as its first frame.
- HEIC. Go has no decoder. With
KeepUnsupportedit is stored untouched; without it, refused. - Serving.
Openhands back an*os.File; caching, ranges andContent-Dispositionare the app's policy.
MIT