Image
Rust 2,136 lines (1,524 code)split
| Part | Code | Comments | Blank | Total | Files |
|---|---|---|---|---|---|
| Rust source | 1,524 | 489 | 123 | 2,136 | 9 |
| Rust tests | 49 | 14 | 11 | 74 | 2 |
Comments include doc comments. Rust tests are tests/ and #[cfg(test)] modules.
Install#
The crate lives in the Appwrite repository; depend on it from Git.
[dependencies]
utopia-image = { git = "https://github.com/appwrite/appwrite" }Getting started#
Common tasks with Image, each a complete program you can copy and run.
utopia-image turns an uploaded image into a preview: decode it, crop it, decorate it, and encode it in the format the client asked for. Everything runs in memory on the calling thread; in async code, run it on the blocking pool.
Make a thumbnail
Decode the blob, crop it to the target size (the centre is kept, scaled to cover), and encode it.
use utopia_image::{Gravity, Image, OutputFormat};
let blob = std::fs::read("../../../packages/image/tests/resources/disk-a/kitten-1.jpg")?;
let mut image = Image::new(&blob)?;
image.crop(400, 400, Gravity::Center, None)?;
let preview = image.output(OutputFormat::Jpeg, 80)?;
assert_eq!(&preview[..2], &[0xFF, 0xD8]);Keep a point of interest in view
A gravity keeps an edge or a corner; a focal point (fractions of the width and height) keeps a spot as centred as the edges allow. A request that carries the coordinates separately can check them with FocalPoint::from_parts.
use utopia_image::{FocalPoint, Gravity, Image, OutputFormat};
let blob = std::fs::read("../../../packages/image/tests/resources/disk-a/kitten-1.jpg")?;
let mut image = Image::new(&blob)?;
let focal = FocalPoint::from_parts(Some(0.3), Some(0.2))?;
image.crop(300, 100, Gravity::Center, focal)?;
let webp = image.output(OutputFormat::Webp, 75)?;
assert_eq!(&webp[8..12], b"WEBP");Decorate it
Borders, rounded corners, opacity, rotation and a background colour chain like the PHP setters. Colours are ImageMagick colour strings.
use utopia_image::{Image, OutputFormat};
let blob = std::fs::read("../../../packages/image/tests/resources/resize/100x100.png")?;
let mut image = Image::new(&blob)?;
image.set_opacity(0.5).set_rotation(90)?.set_background("#ffffff")?;
let png = image.output(OutputFormat::Png, 100)?;
assert_eq!(&png[1..4], b"PNG");Save it to disk
save creates the directory when it is missing; a known file extension decides the format.
use utopia_image::{Image, OutputFormat};
let blob = std::fs::read("../../../packages/image/tests/resources/resize/100x100.png")?;
let dir = std::env::temp_dir().join("utopia-image-guide");
Image::new(&blob)?.save(dir.join("preview.gif"), OutputFormat::Gif, 75)?;
assert!(dir.join("preview.gif").exists());Reference#
Image previews: decode a JPEG, PNG, GIF or WebP (animations included), crop it by gravity or focal point, add a border, round its corners, change its opacity, rotate it, flatten it onto a background, and encode it as JPEG, PNG, GIF, WebP or AVIF.
All the work is CPU-bound: in async code run it on the blocking pool
(tokio::task::spawn_blocking), never on a runtime thread.
use utopia_image::{Gravity, Image, OutputFormat};
let jpeg = std::fs::read("../../../packages/image/tests/resources/resize/100x100.jpg")?;
let mut image = Image::new(&jpeg)?;
image.crop(64, 64, Gravity::Center, None)?.set_border_radius(8)?;
let png = image.output(OutputFormat::Png, 90)?;
assert_eq!(&png[1..4], b"PNG");Modules#
Each module groups one concept of the library. Internal ones hold items the crate re-exports at its root.
API#
What utopia_image exports at its root.