utopia_image/lib.rs
1//! Image previews: decode a JPEG, PNG, GIF or WebP (animations included),
2//! crop it by gravity or focal point, add a border, round its corners, change
3//! its opacity, rotate it, flatten it onto a background, and encode it as
4//! JPEG, PNG, GIF, WebP or AVIF.
5//!
6//! ```
7//! use utopia_image::{Gravity, Image, OutputFormat};
8//!
9//! let jpeg = std::fs::read("../../../packages/image/tests/resources/resize/100x100.jpg")?;
10//! let mut image = Image::new(&jpeg)?;
11//! image.crop(64, 64, Gravity::Center, None)?.set_border_radius(8)?;
12//! let png = image.output(OutputFormat::Png, 90)?;
13//! assert_eq!(&png[1..4], b"PNG");
14//! # Ok::<(), Box<dyn std::error::Error>>(())
15//! ```
16//!
17//! ```php
18//! use Utopia\Image\Image;
19//!
20//! $image = new Image(file_get_contents('/usr/src/code/packages/image/tests/resources/resize/100x100.jpg'));
21//! $image->crop(64, 64)->setBorderRadius(8);
22//! echo substr($image->output('png', 90), 1, 3), "\n"; // PNG
23//! ```
24//!
25//! All the work is CPU-bound: in async code run it on the blocking pool
26//! (`utopia_async::spawn_blocking`), never on a runtime thread.
27//!
28//! The codecs are pure Rust by default. Next to ImageMagick, which the PHP
29//! library wraps, that means: WebP is written lossless (no pure-Rust lossy VP8
30//! encoder exists), AVIF is written but not read, and HEIC is neither read
31//! nor written. Two features add those through the C libraries ImageMagick
32//! itself uses: `libwebp` writes lossy WebP at the requested quality, and
33//! `libheif` reads AVIF and HEIC, writes AVIF as ImageMagick does, and writes
34//! HEIC where the system's libheif has an HEVC encoder (Appwrite's PHP image
35//! has none, so HEIC output fails there in PHP too). The pure-Rust AVIF
36//! encoder is the default `ravif` feature; a process that forks, such as a
37//! PHP extension under Swoole, leaves it out and writes AVIF through libheif. Pixels match ImageMagick's up to resampling and encoder
38//! differences; sizes, frames, delays and formats match exactly.
39//!
40//! PHP → Rust:
41//!
42//! | PHP (`Utopia\Image`) | Rust (`utopia_image`) |
43//! |---|---|
44//! | `Canvas`: `new Canvas($data, $limits)`, `new Canvas(new Blank(...))` | [`Canvas`]: [`Canvas::decode`], [`Canvas::blank`] |
45//! | `$canvas->width`, `->height`, `->orientation`, `->format` | [`Canvas::width`], [`Canvas::height`], [`Canvas::orientation`], [`Canvas::format`] |
46//! | `apply(Transform)` | [`Canvas::apply`] with [`Transform`] |
47//! | `draw(Path\|Text)` | [`Canvas::draw`] with [`Drawing`] |
48//! | `encode(Format, ?int $quality)` | [`Canvas::encode`] with [`OutputFormat`] |
49//! | `Limits` | [`Limits`] |
50//! | `Gravity`, `Point` | [`Gravity`], [`FocalPoint`] |
51//! | `Font::has($character)` | [`has_glyph`] |
52//! | colour strings (`ImagickPixel`) | [`Color`] |
53//! | `Exception\ImageException` and its `UnsupportedFormatException`, `CorruptImageException` and `LimitExceededException`; `InvalidArgumentException`, `DivisionByZeroError` | [`Error`] |
54
55#![warn(missing_docs)]
56
57mod canvas;
58mod color;
59mod decode;
60mod draw;
61mod encode;
62mod error;
63mod format;
64mod frame;
65#[cfg(feature = "libheif")]
66mod heif;
67mod image;
68mod jpeg_source;
69mod options;
70mod transform;
71
72pub use crate::canvas::{Canvas, Drawing, Transform};
73pub use crate::color::Color;
74pub use crate::draw::has_glyph;
75pub use crate::error::{Error, Result};
76pub use crate::format::Format;
77pub use crate::image::Image;
78pub use crate::options::{FocalPoint, Gravity, Limits, OutputFormat};
79
80/// The getting started guide (`guide.md`), compiled and run with the doctests.
81#[cfg(doctest)]
82#[doc = include_str!("../guide.md")]
83pub struct GettingStarted;