Skip to content

How to Use Assetic for Image Optimization with Twig Functions

Warning: You are browsing the documentation for Symfony 3.x, which is no longer maintained.

Read the updated version of this page for Symfony 7.1 (the current stable version).

Caution

Starting from Symfony 2.8, Assetic is no longer included by default in the Symfony Standard Edition. Refer to this article to learn how to install and enable Assetic in your Symfony application.

Among its many filters, Assetic has four filters which can be used for on-the-fly image optimization. This allows you to get the benefits of smaller file sizes without having to use an image editor to process each image. The results are cached and can be dumped for production so there is no performance hit for your end users.

Using Jpegoptim

Jpegoptim is a utility for optimizing JPEG files. To use it with Assetic, make sure to have it already installed on your system and then, configure its location using the bin option of the jpegoptim filter:

1
2
3
4
5
# app/config/config.yml
assetic:
    filters:
        jpegoptim:
            bin: path/to/jpegoptim

It can now be used from a template:

1
2
3
4
{% image '@AppBundle/Resources/public/images/example.jpg'
    filter='jpegoptim' output='/images/example.jpg' %}
    <img src="{{ asset_url }}" alt="Example"/>
{% endimage %}

Removing all EXIF Data

By default, the jpegoptim filter removes some meta information stored in the image. To remove all EXIF data and comments, set the strip_all option to true:

1
2
3
4
5
6
# app/config/config.yml
assetic:
    filters:
        jpegoptim:
            bin: path/to/jpegoptim
            strip_all: true

Lowering Maximum Quality

By default, the jpegoptim filter doesn't alter the quality level of the JPEG image. Use the max option to configure the maximum quality setting (in a scale of 0 to 100). The reduction in the image file size will be at the expense of its quality:

1
2
3
4
5
6
# app/config/config.yml
assetic:
    filters:
        jpegoptim:
            bin: path/to/jpegoptim
            max: 70

Shorter Syntax: Twig Function

If you're using Twig, it's possible to achieve all of this with a shorter syntax by enabling and using a special Twig function. Start by adding the following configuration:

1
2
3
4
5
6
7
8
# app/config/config.yml
assetic:
    filters:
        jpegoptim:
            bin: path/to/jpegoptim
    twig:
        functions:
            jpegoptim: ~

The Twig template can now be changed to the following:

1
<img src="{{ jpegoptim('@AppBundle/Resources/public/images/example.jpg') }}" alt="Example"/>

You can also specify the output directory for images in the Assetic configuration file:

1
2
3
4
5
6
7
8
# app/config/config.yml
assetic:
    filters:
        jpegoptim:
            bin: path/to/jpegoptim
    twig:
        functions:
            jpegoptim: { output: images/*.jpg }

Tip

For uploaded images, you can compress and manipulate them using the LiipImagineBundle community bundle.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version