Download the PHP package spatie/image-optimizer without Composer

On this page you can find all versions of the php package spatie/image-optimizer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package image-optimizer

Easily optimize images using PHP

Latest Version on Packagist Tests Total Downloads

This package can optimize PNGs, JPGs, WEBPs, AVIFs, SVGs and GIFs by running them through a chain of various image optimization tools. Here's how you can use it:

The image at $pathToImage will be overwritten by an optimized version which should be smaller. The package will automatically detect which optimization binaries are installed on your system and use them.

Here are some example conversions that have been done by this package.

Loving Laravel? Then head over to the Laravel specific integration.

Using WordPress? Then try out the WP CLI command.

SilverStripe enthusiast? Don't waste time, go to the SilverStripe module.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

Optimization tools

The package will use these optimizers if they are present on your system:

Here's how to install all the optimizers on Ubuntu/Debian:

And here's how to install the binaries on MacOS (using Homebrew):

And here's how to install the binaries on Fedora/RHEL/CentOS:

Which tools will do what?

The package will automatically decide which tools to use on a particular image.

JPGs

JPGs will be made smaller by running them through JpegOptim. These options are used:

PNGs

PNGs will be made smaller by running them through two tools. The first one is Pngquant 2, a lossy PNG compressor. We set no extra options, their defaults are used. After that we run the image through a second one: Optipng. These options are used:

SVGs

SVGs will be minified by SVGO. SVGO's default configuration will be used, with the omission of the cleanupIDs and removeViewBox plugins because these are known to cause troubles when displaying multiple optimized SVGs on one page.

Please be aware that SVGO can break your svg. You'll find more info on that in this excellent blogpost by Sara Soueidan.

GIFs

GIFs will be optimized by Gifsicle. These options will be used:

WEBPs

WEBPs will be optimized by Cwebp. These options will be used:

(Settings are original taken from here)

AVIFs

AVIFs will be optimized by avifenc. These options will be used:

(Settings are original taken from here and here)

Usage

This is the default way to use the package:

The image at $pathToImage will be overwritten by an optimized version which should be smaller.

The package will automatically detect which optimization binaries are installed on your system and use them.

To keep the original image, you can pass through a second argumentoptimize:

In that example the package won't touch $pathToImage and write an optimized version to $pathToOutput.

Setting a timeout

You can set the maximum of time in seconds that each individual optimizer in a chain can use by calling setTimeout:

In this example each optimizer in the chain will get a maximum 10 seconds to do it's job.

Creating your own optimization chains

If you want to customize the chain of optimizers you can do so by adding Optimizers manually to an OptimizerChain.

Here's an example where we only want optipng and jpegoptim to be used:

Notice that you can pass the options an Optimizer should use to its constructor.

Writing a custom optimizers

Want to use another command line utility to optimize your images? No problem. Just write your own optimizer. An optimizer is any class that implements the Spatie\ImageOptimizer\Optimizers\Optimizer interface:

If you want to view an example implementation take a look at the existing optimizers shipped with this package.

You can easily add your optimizer by using the addOptimizer method on an OptimizerChain.

Logging the optimization process

By default the package will not throw any errors and just operate silently. To verify what the package is doing you can set a logger:

A logger is a class that implements Psr\Log\LoggerInterface. A good logging library that's fully compliant is Monolog. The package will write to log which Optimizers are used, which commands are executed and their output.

Example conversions

Here are some real life example conversions done by this package.

Methodology for JPG, WEBP, AVIF images: the original image has been fed to spatie/image (using the default GD driver) and resized to 2048px width:

jpg

Original Original
771 KB

Optimized Optimized
511 KB (-33.7%, DSSIM: 0.00052061)

credits: Jeff Sheldon, via Unsplash

webp

Original Original
461 KB

Optimized Optimized
184 KB (-60.0%, DSSIM: 0.00166036)

credits: Jeff Sheldon, via Unsplash

avif

Original Original
725 KB

Optimized Optimized
194 KB (-73.2%, DSSIM: 0.00163751)

credits: Jeff Sheldon, via Unsplash

png

Original: Photoshop 'Save for web' | PNG-24 with transparency
39 KB

Original

Optimized
16 KB (-59%, DSSIM: 0.00000251)

Optimized

svg

Original: Illustrator | Web optimized SVG export
25 KB

Original

Optimized
20 KB (-21.5%)

Optimized

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

This package has been inspired by psliwa/image-optimizer

Emotional support provided by Joke Forment

License

The MIT License (MIT). Please see License File for more information.


All versions of image-optimizer with dependencies

PHP Build Version
Package Version
Requires php Version ^7.3|^8.0
ext-fileinfo Version *
psr/log Version ^1.0 | ^2.0 | ^3.0
symfony/process Version ^4.2|^5.0|^6.0|^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package spatie/image-optimizer contains the following files

Loading the files please wait ....