Optimizing images | image | Spatie

 SPATIE

  Image
========

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Image](https://spatie.be/docs/image/v1)  Image-manipulations  Optimizing images

 Version   v3   v2   v1

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

- [ Introduction ](https://spatie.be/docs/image/v1/introduction)
- [ Postcardware ](https://spatie.be/docs/image/v1/postcardware)
- [ Installation and setup ](https://spatie.be/docs/image/v1/installation-and-setup)
- [ Questions &amp; issues ](https://spatie.be/docs/image/v1/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/image/v1/changelog)
- [ About us ](https://spatie.be/docs/image/v1/about-us)

Usage
-----

- [ Basic usage ](https://spatie.be/docs/image/v1/usage/basic-usage)
- [ Saving images ](https://spatie.be/docs/image/v1/usage/saving-images)
- [ Retrieving properties ](https://spatie.be/docs/image/v1/usage/retrieving-properties)
- [ Preparing manipulations ](https://spatie.be/docs/image/v1/usage/preparing-manipulations)
- [ Advanced manipulations ](https://spatie.be/docs/image/v1/usage/advanced-manipulations)

Image manipulations
-------------------

- [ Overview ](https://spatie.be/docs/image/v1/image-manipulations/overview)
- [ Resizing images ](https://spatie.be/docs/image/v1/image-manipulations/resizing-images)
- [ Optimizing images ](https://spatie.be/docs/image/v1/image-manipulations/optimizing-images)
- [ Adjustments ](https://spatie.be/docs/image/v1/image-manipulations/adjustments)
- [ Image canvas ](https://spatie.be/docs/image/v1/image-manipulations/image-canvas)
- [ Effects ](https://spatie.be/docs/image/v1/image-manipulations/effects)
- [ Watermarks ](https://spatie.be/docs/image/v1/image-manipulations/watermarks)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/image                                                                                                                                                                                                                                    `

Optimizing images
=================

###  On this page

1. [ Requirements ](#content-requirements)
2. [ How to use ](#content-how-to-use)
3. [ Customizing the optimization ](#content-customizing-the-optimization)

Requirements
--------------------------------------------------------------------------------------------

Optimization of images is done by the underlying [spatie/image-optimizer](https://github.com/spatie/image-optimizer). It assumes that there are a few optimization tools, such as [JpegOptim](http://freecode.com/projects/jpegoptim) an [Pngquant](https://pngquant.org/) present on your system. For more info, check out [the relevant docs](https://github.com/spatie/image-optimizer#optimization-tools).

How to use
--------------------------------------------------------------------------------------

To shave off some kilobytes of the images the package can optimize images by calling the `optimize` method.

Here's the original image of New York used in all examples has a size of 622 Kb. Let's optimize it.

```
Image::load('example.jpg')
    ->optimize()
    ->save('example-optimized.jpg');
```

![Optimized Image](../images/example-optimized.jpg).

The size of the optimized image is 573 Kb.

No matter where or how many times you call `optimize` in you chain, it will always be performed as the last operation once.

Customizing the optimization
--------------------------------------------------------------------------------------------------------------------------------------------

To optimization of images is done by the underlying [spatie/image-optimizer](https://github.com/spatie/image-optimizer) package. You can pass your own customized chains as array. The keys should be fully qualified class names of optimizers and the values the options that they should get. Here's an example

```
Image::load('example.jpg')
    ->optimize([Jpegoptim::class => [
        '--all-progressive',
    ]])
    ->save();
```

If you need more control over the optimizer chain, you can still pass your own instance of `OptimizerChain`. It can be especially useful if you need to set a custom timeout or a custom binary path. You may not have enough privileges to install the necessary binaries on your server but you can still upload some [precompiled binaries](https://github.com/imagemin?q=bin&type=&language=).

```
$optimizer = new OptimizerChain();
$optimizer->setTimeout(10);
$optimizer->addOptimizer((new Jpegoptim([
    '--all-progressive',
]))->setBinaryPath('/home/user/bin'));

Image::load('example.jpg')
    ->setOptimizeChain($optimizer)
    ->optimize()
    ->save();
```
