Usage | image-rust | Spatie      

 SPATIE  

image-rust
==========

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Image-rust](https://spatie.be/docs/image-rust/v1)  Usage

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/image-rust/v1) 

  Usage    
- [ Introduction ](https://spatie.be/docs/image-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/image-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/image-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/image-rust/v1/usage)
- [ Encoding and optimization ](https://spatie.be/docs/image-rust/v1/encoding-and-optimization)
- [ Testing ](https://spatie.be/docs/image-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/image-rust/v1/changelog)

 Usage
=====

###  On this page 

1. [ Fit modes ](#content-fit-modes)
2. [ Anchored and focal cropping ](#content-anchored-and-focal-cropping)
3. [ Watermarks and inserted images ](#content-watermarks-and-inserted-images)
4. [ Canvas and borders ](#content-canvas-and-borders)
5. [ Color effects ](#content-color-effects)
6. [ Combining with other crates ](#content-combining-with-other-crates)

Fit modes
-----------------------------------------------------------------------------------

`Image` consumes itself as you chain operations. Each manipulation runs immediately, in call order. Fallible methods return `Result<Image, Error>`, so keep the returned builder and use `?` to propagate errors.

```
use spatie_image::{Fit, Image};
use image::{DynamicImage, Rgba};

let thumbnail = Image::from(DynamicImage::new_rgb8(800, 400))
    .background(Rgba([240, 240, 240, 255]))
    .fit(Fit::Fill, 300, 300)?
    .into_inner();
```

- `Max` preserves aspect ratio within the bounds and never upscales.
- `Contain` preserves aspect ratio within the bounds and allows upscaling.
- `Fill` centers a `Max` result on a canvas of the requested size.
- `FillMax` centers a `Contain` result on a canvas of the requested size. It does upscale.
- `Crop` scales to cover the bounds and crops the excess from the center.
- `Stretch` uses the exact dimensions without preserving aspect ratio.

Aspect-derived dimensions round to the nearest pixel, with a minimum of one. Resizing uses Lanczos3. `ImageExt::fit_with_filter` accepts another `image::imageops::FilterType`.

Anchored and focal cropping
-----------------------------------------------------------------------------------------------------------------------------------------

`Position` has nine anchors, from `TopLeft` to `BottomRight`. `crop_at` on `DynamicImage`, or `crop` on the builder, crops without resizing and caps oversized crop dimensions to the source.

Both focal operations use normalized coordinates between 0 and 1. `(0, 0)` is the top left, `(0.5, 0.5)` is the center, and `(1, 1)` is the bottom right. The crop moves inward at an edge to stay inside the source. Coordinates outside the range, including NaN and infinity, return an error.

```
use spatie_image::{ImageExt, Position};

let corner = source.crop_at(100, 80, Position::BottomRight)?;
let detail = source.focal_crop(200, 100, 0.8, 0.3)?;
let portrait = source.focal_crop_and_resize(100, 150, 0.8, 0.3)?;
```

`focal_crop` keeps source pixels at their original size. `focal_crop_and_resize` first selects the largest source rectangle with the target aspect ratio, then resizes to the target. It may upscale.

Watermarks and inserted images
--------------------------------------------------------------------------------------------------------------------------------------------------

Insert any `DynamicImage`. Resize it separately when needed. Padding is in pixels, measured inward from the anchored edge; the centered axis ignores padding. Opacity multiplies the overlay's existing alpha. Oversized or out-of-bounds content is clipped.

```
use spatie_image::{ImageExt, Overlay, Position};

let marked = source.insert(&logo, Overlay {
    position: Position::BottomRight,
    padding_x: 16,
    padding_y: 24,
    opacity: 0.65,
})?;
```

Canvas and borders
--------------------------------------------------------------------------------------------------------------

`resize_canvas` changes the canvas without scaling content. Its anchor determines which edges remain fixed when enlarging or cropping. The builder uses its configured background; extension methods take an explicit RGBA color.

Borders have three modes. `Expand` adds pixels outside the image. `Overlay` paints over the existing edges. `Shrink` resizes the content into the original dimensions minus twice the border width on each axis, then adds the border inside the original dimensions. It returns an error when no content area remains.

```
use image::Rgba;
use spatie_image::{BorderType, ImageExt, Position};

let framed = source.border(8, Rgba([20, 20, 20, 255]), BorderType::Expand)?;
let canvas = framed.resize_canvas(200, 200, Position::Center, Rgba([0, 0, 0, 0]))?;
```

Color effects
-----------------------------------------------------------------------------------------------

- `sepia()` applies the standard sepia matrix.
- `colorize(red, green, blue)` adds signed channel offsets between −255 and 255, clamping at 0 and 255.
- `gamma(value)` computes output = input^(1 / value) on normalized RGB channels. Values above one brighten; values below one darken. Gamma must be finite and positive.
- `pixelate(block_size)` replaces each block with the mean of its RGBA channels, including partial edge blocks. The block size must be nonzero.

Sepia, colorize and gamma preserve alpha. These operations, compositing, canvas resizing, borders and PNG/AVIF encoding use 8-bit RGBA. Cropping and resizing preserve the source's pixel type. Operations work on encoded channel values rather than in linear light; no ICC conversion is performed.

```
use spatie_image::Image;

let processed = Image::from(pixels)
    .sepia()
    .colorize(10, 0, -10)?
    .gamma(1.2)?
    .pixelate(4)?
    .into_inner();
```

Combining with other crates
-----------------------------------------------------------------------------------------------------------------------------------------

The public `image` re-export is the same crate used by the builder. `ImageExt` is implemented directly on `DynamicImage`. Use `Image::from(pixels)` and `into_inner()` to transfer ownership without copying, or `inner()` and `inner_mut()` to borrow the pixels. Native operations such as `blur`, `unsharpen`, `rotate90`, `fliph`, `grayscale` and `brighten` remain available on `DynamicImage`. Use its image buffers directly with `imageproc` or `fast_image_resize`.

```
use spatie_image::{Image, Fit};

let mut image = Image::from(pixels).fit(Fit::Max, 50, 50)?;
*image.inner_mut() = image.inner().blur(1.5).rotate90();
let pixels = image.into_inner();
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
