PHP and Rust API | 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)  PHP and Rust API

 Version   v1      

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

  PHP and Rust API    
- [ 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)
- [ PHP and Rust API ](https://spatie.be/docs/image-optimizer-rust/v1/parity)

 PHP and Rust API
================

###  On this page 

1. [ Differences to keep in mind ](#content-differences-to-keep-in-mind)

If you've used [spatie/image-optimizer](https://github.com/spatie/image-optimizer), most method names will look familiar. This table shows how to use the same operations in Rust.

PHP featureRust API`Image` construction, MIME, path, extension`Image::new`, `mime`, `path`, `extension``OptimizerChainFactory::create()``OptimizerChainFactory::create()`quality configuration`OptimizerChainFactory::with_quality(q)?`explicit factory optimizer config`OptimizerChain::new().add_optimizer(...)`chain inspection, append, replace`get_optimizers`, `add_optimizer`, `set_optimizers``setTimeout``set_timeout(Duration)``useLogger` / `DummyLogger``use_logger` / `DummyLogger`, plus `TracingLogger``throws(true/false)``throws(true/false)``throws(callable)``on_error` returning `Result``optimize(path)``optimize(path)``optimize(input, output)``optimize_to(input, output)`seven optimizer classes`Tool::{Jpegoptim,Pngquant,Optipng,Svgo,Gifsicle,Cwebp,Avifenc}.with_options(...)`options, binary path and executable overrides`set_options`, `set_binary_path`, `set_binary_name`, `set_decode_binary_name`image path + shell command constructionborrowed `Image` + `BinaryOptimizer::command` returning `Command``SelfHandlingOptimizer` and base class`Optimizer` trait with `optimize``getTmpPath` and chain cleanupRAII temporary PNG owned by AVIF optimization; custom optimizers own their resourcesUse the `Optimizer` trait for both command-line tools and optimizers that call an API. Configuration methods consume and return the chain, so keep the returned value or chain the calls together. Methods that can fail return `Result`; use `?` to pass an error to your caller.

Differences to keep in mind
-----------------------------------------------------------------------------------------------------------------------------------------

- Options are literal arguments, not shell commands. Pass a flag and its value as separate items, such as `["-q", "85"]`.
- Input must be a regular local file. Local file URLs are resolved to paths. A separate output must not point to the source, including through hard links.
- MIME detection uses a bundled Rust database rather than PHP's system libmagic, so unusual files may be classified differently.
- Rust panics are not caught. Custom optimizers must implement `Send + Sync` and own their temporary files so Rust can clean them up when they go out of scope.
- Native-tool versions affect encoded bytes and accepted options. Check the tool versions when comparing results across systems.

You can also enable [safe output](usage#keeping-the-original-safe) or request [optimization reports](usage#reading-optimization-reports). These are additions to the PHP API. They are opt-in; the default methods still write directly and return `Result`.

 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)
