Expand description
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.
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");use Utopia\Image\Image;
$image = new Image(file_get_contents('/usr/src/code/packages/image/tests/resources/resize/100x100.jpg'));
$image->crop(64, 64)->setBorderRadius(8);
echo substr($image->output('png', 90), 1, 3), "\n"; // PNGAll the work is CPU-bound: in async code run it on the blocking pool
(utopia_async::spawn_blocking), never on a runtime thread.
The codecs are pure Rust by default. Next to ImageMagick, which the PHP
library wraps, that means: WebP is written lossless (no pure-Rust lossy VP8
encoder exists), AVIF is written but not read, and HEIC is neither read
nor written. Two features add those through the C libraries ImageMagick
itself uses: libwebp writes lossy WebP at the requested quality, and
libheif reads AVIF and HEIC, writes AVIF as ImageMagick does, and writes
HEIC where the system’s libheif has an HEVC encoder (Appwrite’s PHP image
has none, so HEIC output fails there in PHP too). The pure-Rust AVIF
encoder is the default ravif feature; a process that forks, such as a
PHP extension under Swoole, leaves it out and writes AVIF through libheif. Pixels match ImageMagick’s up to resampling and encoder
differences; sizes, frames, delays and formats match exactly.
PHP → Rust:
PHP (Utopia\Image) | Rust (utopia_image) |
|---|---|
Canvas: new Canvas($data, $limits), new Canvas(new Blank(...)) | Canvas: Canvas::decode, Canvas::blank |
$canvas->width, ->height, ->orientation, ->format | Canvas::width, Canvas::height, Canvas::orientation, Canvas::format |
apply(Transform) | Canvas::apply with Transform |
draw(Path|Text) | Canvas::draw with Drawing |
encode(Format, ?int $quality) | Canvas::encode with OutputFormat |
Limits | Limits |
Gravity, Point | Gravity, FocalPoint |
Font::has($character) | has_glyph |
colour strings (ImagickPixel) | Color |
Exception\ImageException and its UnsupportedFormatException, CorruptImageException and LimitExceededException; InvalidArgumentException, DivisionByZeroError | Error |
Structs§
- Canvas
- An image, and what has been asked of it so far.
- Color
- An RGBA colour with 8 bits per channel (alpha 255 is opaque).
- Focal
Point - A focal point: the spot a crop keeps in view, as fractions of the width
and height (
0.0..=1.0). - Image
- A decoded image (every frame of an animation), transformed in place and
encoded on
outputorsave. - Limits
- What one image may cost: its largest width and height in pixels, and how many bytes of decoded pixels it may hold at once.
- Transform
- One preview: a crop, then opacity, a background, a border, rounded corners and a rotation, in that order. A part left at its default is skipped.
Enums§
- Drawing
- Something drawn onto a canvas.
- Error
- Everything
Imagecan fail with, one variant per PHP exception class. The messages are the library’s own and say what the caller can act on; what a codec reported is kept forDebugonly. - Format
- A raster format
Image::newaccepts, identified from the file signature, so only decoders for these formats ever see the data. - Gravity
- Which part of the image a crop keeps.
- Output
Format - An output format.
Functions§
- has_
glyph - Whether
font(TrueType or OpenType data) draws something forcharacter: a missing glyph, or one with no outline, draws nothing.
Type Aliases§
- Result
- The result of every fallible operation of this crate.