Utopia Image
Image previews: decode, crop by gravity or focal point, borders, rounded corners, opacity, rotation and background, then encode to JPEG, PNG, GIF, WebP or AVIF.
This library is maintained by the Appwrite team.
Getting started
[]
= { = "https://github.com/appwrite/appwrite" }
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 ;
let blob = read?;
let mut image = new?;
image.crop?;
let preview = image.output?;
assert_eq!;
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 ;
let blob = read?;
let mut image = new?;
let focal = from_parts?;
image.crop?;
let webp = image.output?;
assert_eq!;
Decorate it
Borders, rounded corners, opacity, rotation and a background colour chain like the PHP setters. Colours are ImageMagick colour strings.
use ;
let blob = read?;
let mut image = new?;
image.set_opacity.set_rotation?.set_background?;
let png = image.output?;
assert_eq!;
Save it to disk
save creates the directory when it is missing; a known file extension decides the format.
use ;
let blob = read?;
let dir = temp_dir.join;
new?.save?;
assert!;
Contributing
Development happens in appwrite/appwrite, where this crate lives at crates/utopia/image. Run these commands from a checkout of it. The contributing guide covers each step.
Test
Compare with PHP
The utopia PHP extension serves this crate as Utopia\Image\Canvas and Utopia\Image\Font, which have no PHP implementation. crates/extension/tests/canvas.php and packages/image/tests run on it in the development image (crates/extension/tests/run.sh).
License
BSD-3-Clause. See LICENSE.