Safe output and reports | image-optimizer-rust | Spatie      

 SPATIE  

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

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Image-optimizer-rust](https://spatie.be/docs/image-optimizer-rust/v1)  Safe output and reports

 Version   v1      

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

  Safe output and reports    
- [ Introduction ](https://spatie.be/docs/image-optimizer-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/image-optimizer-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/image-optimizer-rust/v1/installation-and-setup)
- [ Which tools will do what? ](https://spatie.be/docs/image-optimizer-rust/v1/which-tools-will-do-what)
- [ Usage ](https://spatie.be/docs/image-optimizer-rust/v1/usage)
- [ Logging the optimization process ](https://spatie.be/docs/image-optimizer-rust/v1/logging-the-optimization-process)
- [ Example conversions ](https://spatie.be/docs/image-optimizer-rust/v1/example-conversions)
- [ Changelog ](https://spatie.be/docs/image-optimizer-rust/v1/changelog)
- [ Testing ](https://spatie.be/docs/image-optimizer-rust/v1/testing)
- [ Safe output and reports ](https://spatie.be/docs/image-optimizer-rust/v1/safe-output-and-reports)

 Safe output and reports
=======================

###  On this page 

1. [ Keeping the original safe ](#content-keeping-the-original-safe)
2. [ Reading optimization reports ](#content-reading-optimization-reports)

Safe output optimizes a working copy and only publishes a smaller, error-free candidate. Reports expose sizes, timing and individual optimizer outcomes.

<a id="keeping-the-original-safe"></a>

Keeping the original safe
-----------------------------------------------------------------------------------------------------------------------------------

Enable `safe_output(true)` to optimize a working copy before replacing the destination:

```
use spatie_image_optimizer::{OptimizerChainFactory, OutputStatus};

let report = OptimizerChainFactory::create()
    .safe_output(true)
    .optimize_with_report(path_to_image)?;

match report.status {
    OutputStatus::Written => println!("Saved {} bytes", report.bytes_saved()),
    OutputStatus::KeptOriginal => println!("No size reduction; kept the original"),
    OutputStatus::Discarded => eprintln!("An optimizer failed; kept the original"),
}
```

To preserve the source and use a separate destination:

```
use spatie_image_optimizer::OptimizerChainFactory;

let report = OptimizerChainFactory::create()
    .safe_output(true)
    .throws(true)
    .optimize_to_with_report(path_to_image, path_to_output)?;
println!("{:?}: {} bytes saved", report.status, report.bytes_saved());
```

The working copy lives in a private temporary directory beside the destination. Use a trusted, writable parent directory with enough space for the copy and encoder scratch files. Destination symlinks and separate-output source aliases, including hard links, are rejected.

If the candidate is not smaller or no tools match, in-place optimization keeps the original and separate-output optimization writes a copy of the source. If any optimizer fails under a continuing error policy, the entire candidate is discarded and an existing destination stays untouched. Check `OutputStatus::Discarded`; `Ok` alone does not mean a file was published. Use `throws(true)` to abort on optimizer failures.

Publication uses a same-filesystem rename, atomic where supported. Existing destinations retain their `std::fs::Permissions`; new outputs copy the source permissions. Ownership, ACLs, extended attributes and timestamps may change. Other hard links and open handles still refer to the old file. There is no `fsync` or power-loss durability guarantee. Forced termination may leave scratch files behind.

Custom optimizers receive the working path. Supply explicit configuration paths when an encoder discovers configuration relative to its input. This is not a sandbox or an image-validity check. Keep encoders updated and isolate untrusted uploads at the operating-system level.

<a id="reading-optimization-reports"></a>

Reading optimization reports
--------------------------------------------------------------------------------------------------------------------------------------------

Use `optimize_with_report` or `optimize_to_with_report` with any chain, with or without safe output:

```
use spatie_image_optimizer::{OptimizerChainFactory, OptimizerStatus};

let report = OptimizerChainFactory::create()
    .safe_output(true)
    .optimize_with_report(path_to_image)?;

println!("Input: {} bytes; candidate: {:?}; elapsed: {:?}",
    report.original_bytes, report.candidate_bytes, report.elapsed);
for optimizer in &report.optimizers {
    match &optimizer.status {
        OptimizerStatus::Succeeded => println!("{}: completed in {:?}", optimizer.name, optimizer.elapsed),
        OptimizerStatus::Skipped => println!("{}: unsupported format", optimizer.name),
        OptimizerStatus::Failed(message) => eprintln!("{}: {message}", optimizer.name),
    }
}
```

`input` and `output` identify the requested paths. `original_bytes` is the input size before optimization. `candidate_bytes` is the working size before acceptance, not the final output size; it can be larger or `None` after a failure. `bytes_saved()` counts published reduction against the source and returns zero for growth, retained originals and discarded candidates.

`optimizers` lists entries in execution order. `Skipped` means the optimizer does not handle that format; `Succeeded` does not guarantee smaller output. `Failed(String)` records a continued error. Total `elapsed` includes preparation and publication; per-tool times include support checks.

Without safe output, `Written` can include larger or partial output after a continued error. Use `on_error` or `throws(true)` for typed errors. Aborted chains return `Err`, not partial reports.

 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)
