Resize and re-encode an image
[![image-badge]][image] [![cat-multimedia-badge]][cat-multimedia]
Serving a small preview of a large upload means decoding the original, shrinking
it, and writing it back in a web-friendly format. image::open reads the file
and infers the format from its contents. DynamicImage::resize scales the
image to fit a bounding box while preserving its aspect ratio. Its last argument
is the resampling FilterType: Lanczos3 gives the best quality, while
Nearest, Triangle, CatmullRom, and Gaussian trade quality for
speed. DynamicImage::save selects the encoder from the output extension, so
writing to thumbnail.jpg produces a JPEG.
Detect an image format from its bytes
[![image-badge]][image] [![cat-multimedia-badge]][cat-multimedia]
An upload may arrive with a misleading extension or none at all. guess_format
reads the leading magic bytes and returns the matching ImageFormat, which in
turn gives the MIME type through ImageFormat::to_mime_type. Only the header is
examined, so a short prefix of the file is enough.
Inspect image EXIF metadata
[![kamadak-exif-badge]][kamadak-exif] [![cat-multimedia-badge]][cat-multimedia]
A photograph carries EXIF metadata describing the camera and the capture. The
[kamadak-exif][kamadak-exif] crate, imported as exif, reads it without
decoding the pixels. Reader::read_from_container parses the EXIF block from a
buffered reader. EXIF splits its fields across directories: the primary image and
an optional embedded thumbnail. Exif::get_field looks up a Tag in one of
them, chosen with In, here In::PRIMARY for the primary image.
Field::display_value renders the value.
{{#include ../links.md}}