PHPackages                             pboivin/flou - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. pboivin/flou

ActiveLibrary[Image &amp; Media](/categories/media)

pboivin/flou
============

PHP responsive images and lazy loading toolbox.

v1.7.0(3y ago)131971MITPHPPHP &gt;=8.0

Since Apr 9Pushed 2y ago2 watchersCompare

[ Source](https://github.com/pboivin/flou)[ Packagist](https://packagist.org/packages/pboivin/flou)[ Docs](https://github.com/pboivin/flou)[ RSS](/packages/pboivin-flou/feed)WikiDiscussions main Synced 4w ago

READMEChangelog (10)Dependencies (5)Versions (16)Used By (1)

flou
====

[](#flou)

[![Build Status](https://github.com/pboivin/flou/workflows/tests/badge.svg)](https://github.com/pboivin/flou/actions)[![Latest Stable Version](https://camo.githubusercontent.com/00f6a1d2729579a7746b233aea0caf3396f6586d1e9fcd6d03e4ad911497a788/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70626f6976696e2f666c6f75)](https://packagist.org/packages/pboivin/flou)[![License](https://camo.githubusercontent.com/bdf3e45db5af83502d43c755ae5552246b94555779a23cbc6b062aa47c9a4c97/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f70626f6976696e2f666c6f75)](https://packagist.org/packages/pboivin/flou)

Flou is a PHP package integrating [Glide (PHP)](https://github.com/thephpleague/glide) and [vanilla-lazyload (JS)](https://github.com/verlok/vanilla-lazyload). It's optimized to quickly implement lazy loading and responsive images from a local folder of source images.

**Features:**

- Transform local images on initial page load (does not expose Glide URLs)
- Can leverage custom Glide configurations (e.g. source images on S3)
- Generate responsive HTML for `img` and `picture` elements
- Useable with static site generators and in CLI scripts
- Framework agnostic (a set of plain PHP classes)

**Requirements:**

- PHP &gt;= 8.0

**Table of contents:**

- [Installing](#installing)
- [Getting Started](#getting-started)
- [Image Transformations](#image-transformations)
- [Image Rendering](#image-rendering)
- [Image Sets (Responsive Images)](#image-sets-responsive-images)
- [Remote Images](#remote-images)
- [Examples](#examples)
- [Development](#development)
- [License](#license)

**Demo project:**

See the [flou-jigsaw-demo](https://github.com/pboivin/flou-jigsaw-demo) repository for an example project integrating Flou with the [Jigsaw](https://github.com/tighten/jigsaw) PHP static site generator.

Installing
----------

[](#installing)

The package can be installed via Composer:

```
composer require pboivin/flou

```

This also installs Glide as a Composer dependency.

You can pull in the vanilla-lazyload library via a CDN:

```

```

or via NPM:

```
npm install --save vanilla-lazyload

```

Consult the [vanilla-lazyload documentation](https://github.com/verlok/vanilla-lazyload#-getting-started---script) for more installation options.

Getting Started
---------------

[](#getting-started)

First, initialize the `LazyLoad` JS object. Add the following script to your page template:

```

    document.addEventListener("DOMContentLoaded", () => {
        new LazyLoad({
            elements_selector: ".lazyload",
        });
    });

```

Then, initialize the `ImageFactory` PHP object with your project-specific configuration:

```
use Pboivin\Flou\ImageFactory;

$flou = new ImageFactory([
    'sourcePath' => '/home/user/my-site.com/public/images/source',
    'cachePath' => '/home/user/my-site.com/public/images/cache',
    'sourceUrlBase' => '/images/source',
    'cacheUrlBase' => '/images/cache',
]);
```

#### Configuration

[](#configuration)

The required options are:

NameTypeDescription`sourcePath`stringThe full path to the source images.`cachePath`stringThe full path where Glide will store the image transformations.`sourceUrlBase`stringThe base URL for the source images.`cacheUrlBase`stringThe base URL for the transformed images.Other options:

NameTypeDescription`glideParams`array[Default Glide parameters for LQIP elements.](#default-glide-parameters)`renderOptions`array[Default render options for all images.](#default-render-options)#### Framework Integration

[](#framework-integration)

If you're using a framework with a Service Container, you can register the `$flou` instance as a singleton for your entire application. This will be your entry point to transform and render images.

#### Extra JS and CSS

[](#extra-js-and-css)

Some examples below require additional JS and CSS. You'll find a more complete sample in the [assets directory](./assets).

Image Transformations
---------------------

[](#image-transformations)

#### Transforming source images

[](#transforming-source-images)

Use the `image()` method to transform a single image into a low-quality image placeholder (LQIP):

```
$image = $flou->image('01.jpg');
```

You can also provide custom Glide parameters for the image transformation:

```
$image = $flou->image('01.jpg', [
    'w' => 10,
    'h' => 10,
    'fit' => 'crop',
]);
```

You'll find all available parameters in the [Glide documentation](https://glide.thephpleague.com/2.0/api/quick-reference/).

As you can see, the default parameters are used to generate LQIP from source images, but you are not restricted to that. You may generate as many transformations as you need from the source image:

```
$phone = $flou->image('01.jpg', ['w' => 500]);
$tablet = $flou->image('01.jpg', ['w' => 900]);
$desktop = $flou->image('01.jpg', ['w' => 1300]);
```

If you're working with responsive images and the `srcset` attribute, have a look at the next section ([Image Sets](#image-sets-responsive-images)).

#### Default Glide parameters

[](#default-glide-parameters)

You can customize the default Glide parameters in the `ImageFactory` configuration:

```
$flou = new ImageFactory([
    // ...
    'glideParams' => [
        'h' => 10,
        'fm' => 'gif',
    ],
]);
```

#### Image objects

[](#image-objects)

The `image()` method returns an `Image` object, from which you can conveniently access the source image file and the transformed (cached) image file:

```
$image = $flou->image('01.jpg');

# Source image data:
echo $image->source()->url();       # /images/source/01.jpg
echo $image->source()->path();      # /home/user/my-site.com/public/images/source/01.jpg
echo $image->source()->width();     # 3840
echo $image->source()->height();    # 2160
echo $image->source()->ratio();     # 1.77777778

# Transformed image data:
echo $image->cached()->url();       # /images/cache/01.jpg/de828e8798017be816f79e131e41dcc9.jpg
...
```

Use the `toArray()` method to export the image to a plain array:

```
$data = $image->toArray();

# [
#     "source" => [
#         "url" => "/images/source/01.jpg",
#         "path" => "/home/user/my-site.com/public/images/source/01.jpg",
#         "width" => 3840,
#         "height" => 2160,
#         "ratio" => 1.77777778,
#     ],
#     "cached" => [
#         "url" => "/images/cache/01.jpg/de828e8798017be816f79e131e41dcc9.jpg",
#         ...
#     ],
# ]
```

#### Image resampling

[](#image-resampling)

Image resampling is a simple way to reuse a transformed image as the source of another transformation. Use the `resample()` method to begin:

```
$greyscale = $flou->resample('01.jpg', [
    'filt' => 'greyscale',
    'w' => 2000,
]);
```

This is the same as calling `image()`, but returns an instance of `ResampledImage` instead. The resampled image can then be used again as a source for `image()`:

```
$image = $flou->image($greyscale, ['w' => 50]);

# Source image:
echo $image->source()->url();       # /images/cache/01.jpg/a50df0a8c8a84cfc6a77cf74b414d020.jpg
echo $image->source()->width();     # 2000
...

# Transformed image:
echo $image->cached()->url();       # /images/cache/_r/01.jpg/a50df0a8c8a84cfc6a77cf74b414d020.jpg/9a5bdd58bbc27a556121925569af7b0c.jpg
echo $image->cached()->width();     # 50
...
```

Image Rendering
---------------

[](#image-rendering)

#### Rendering single images

[](#rendering-single-images)

The `render()` method on the image returns an `ImageRender` object, which prepares HTML suitable for the vanilla-lazyload library. Then, `img()` is used to render an `img` element:

```
$image = $flou->image('01.jpg');

echo $image
        ->render()
        ->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
```

See HTML Output```

```

Options passed into `img()` are included as HTML attributes on the element. Attribute values are not escaped by default.

Some attributes are automatically generated (e.g. `src`, `width`, `height`, etc.). You can override them with a `!` prefix:

```
echo $image
        ->render()
        ->img([
            'class' => 'w-full',
            'alt' => 'Lorem ipsum',
            '!src' => false,
        ]);
```

See HTML Output```

```

#### Render options

[](#render-options)

The `ImageRender` object can be configured to optimize the HTML output:

- **`useAspectRatio()`:** Prevents content shifting when the LQIP is replaced with the source image:

    ```
    echo $image
            ->render()
            ->useAspectRatio()
            ->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);

    # or use a custom aspect-ratio:

    echo $image
            ->render()
            ->useAspectRatio(16 / 9)
            ->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
    ```

      See HTML Output```

    ```
- **`usePaddingTop()`:** A workaround for older browsers not supporting the `aspect-ratio` CSS property:

    ```
    echo $image
            ->render()
            ->usePaddingTop()
            ->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);

    # or use a custom aspect-ratio:

    echo $image
            ->render()
            ->usePaddingTop(16 / 9)
            ->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
    ```

      See HTML Output```

    ```
- **`useWrapper()`:** Wraps the image with an extra `div` and separates the LQIP element from the main `img` element. This is used to add a fade-in effect when the image is loaded.

    (Requires additional JS and CSS. [See fade-in example.](#fade-in-image-on-load))

    ```
    echo $image
            ->render()
            ->useWrapper()
            ->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
    ```

      See HTML Output```

    ```
- **`useBase64Lqip()`:** Inlines a Base64 version of the LQIP in the `src` attribute of the `img` element. This reduces the number of HTTP requests needed to display a page, at the cost of making the HTML a bit heavier.

    ```
    echo $image
            ->render()
            ->useBase64Lqip()
            ->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
    ```

      See HTML Output```

    ```

#### Default render options

[](#default-render-options)

You can set the default render options for all images in the `ImageFactory` configuration:

```
$flou = new ImageFactory([
    // ...
    'renderOptions' => [
        'aspectRatio' => true,
        'wrapper' => true,
        'base64Lqip' => true,
        // ...
    ],
]);
```

See Available OptionsNameTypeDescription`baseClass`stringCSS class for `img` element. Default: `'lazyload'``wrapperClass`stringCSS class for wrapper element. Default: `'lazyload-wrapper'``lqipClass`stringCSS class for LQIP element. Default: `'lazyload-lqip'``paddingClass`stringCSS class for padding-specific wrapper element. Default: `'lazyload-padding'``aspectRatio`boolean or numberUse aspect ratio. Default: `false``paddingTop`boolean or numberUse padding-top workaround. Default: `false``wrapper`booleanUse wrapper element. Default: `false``base64Lqip`booleanUse Base64 LQIP value. Default: `false`
#### Noscript variation

[](#noscript-variation)

Use the `noScript()` method to render an `img` element without any lazy loading behavior:

```
echo $image
        ->render()
        ->noScript(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
```

See HTML Output```

```

This is used to add a `noscript` image fallback. ([See Noscript image fallback example](#noscript-fallback))

It can also be used creatively to work on the source image with CSS classes and HTML attributes. ([See Browser native lazy loading example](#native-lazy-loading-example))

Image Sets (Responsive Images)
------------------------------

[](#image-sets-responsive-images)

#### Single source (`img` element)

[](#single-source-img-element)

Use the `imageSet()` method to transform a source image into a set of responsive images:

```
$imageSet = $flou->imageSet([
    'image' => '01.jpg',
    'sizes' => '(max-width: 500px) 100vw, 50vw',
    'widths' => [500, 900, 1300, 1700],
]);
```

This returns an `ImageSet` object, which prepares all variations of the source image. The `render()` method on the image set returns an `ImageSetRender` instance, as seen before with single images:

```
echo $imageSet
        ->render()
        ->useAspectRatio()
        ->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
```

See HTML Output```

```

Like `ImageRender`, you can optimize `ImageSetRender` with the [same methods](#image-render-configuration):

- `useAspectRatio()`
- `usePaddingTop()`
- `useWrapper()`
- `useBase64Lqip()`
- `noScript()`

#### Multiple sources (`picture` element)

[](#multiple-sources-picture-element)

With a similar configuration, `imageSet()` also handles multiple source images:

```
$imageSet = $flou->imageSet([
    [
        'image' => 'portrait.jpg',
        'media' => '(max-width: 1023px)',
        'sizes' => '100vw',
        'widths' => [400, 800, 1200],
    ],
    [
        'image' => 'landscape.jpg',
        'media' => '(min-width: 1024px)',
        'sizes' => '66vw',
        'widths' => [800, 1200, 1600],
    ],
]);
```

Then, the `picture()` method is used to render a `picture` element:

```
echo $imageSet
        ->render()
        ->picture(['class' => 'my-image', 'alt' => 'Lorem ipsum']);
```

See HTML Output```

```

See also: [Art-directed `picture` element example](#art-directed-picture-element)

#### Multiple formats (`picture` element)

[](#multiple-formats-picture-element)

The configuration allows multiple image formats for each source:

```
$imageSet = $flou->imageSet([
    'image' => '01.jpg',
    'sizes' => '100vw',
    'widths' => [400, 800, 1200, 1600],
    'formats' => ['webp', 'jpg'],
]);

echo $imageSet
        ->render()
        ->picture(['class' => 'my-image', 'alt' => 'Lorem ipsum']);
```

See HTML Output```

```

#### Custom Glide parameters

[](#custom-glide-parameters)

You can provide an array of base Glide parameters as a second argument to `imageSet()`:

```
$imageSet = $flou->imageSet([
    'image' => '01.jpg',
    'sizes' => '100vw',
    'widths' => [400, 800, 1200, 1600],
    'formats' => ['webp', 'jpg'],
], [
    'q' => 80,
]);
```

You'll find all available parameters in the [Glide documentation](https://glide.thephpleague.com/2.0/api/quick-reference/).

Note: You may use all parameters except `w` and `fm`, which are automatically generated from the `widths` and `formats` configuration above.

Remote Images
-------------

[](#remote-images)

The base `ImageFactory` class is optimized for cases where both source and cached images exist on the local filesystem. The `RemoteImageFactory` class was introduced to enable new use-cases:

- Working with remote Glide endpoints
- Integrating with existing Glide Server configurations

This adds support for images stored on remote filesystems, such as Amazon S3.

#### Configuration

[](#configuration-1)

The options for `RemoteImageFactory` are:

NameTypeDescription`glideServer`League\\Glide\\ServerA `Server` instance.`glideUrlBase`stringAlternatively, the base URL for a remotely accessible Glide server.`glideUrlSignKey`stringPrivate key used for Glide HTTP signatures. (optional)#### Glide Endpoint

[](#glide-endpoint)

If you already have a Glide instance setup and publicly accessible, you can hook into it with the following configuration:

```
$flou = new Pboivin\Flou\RemoteImageFactory([
    'glideUrlBase' => '/glide', // or use a full URL: https://cdn.my-site.com/glide
    'glideUrlSignKey' => 'secret',
]);

$image = $flou->image('test.jpg');

//...
```

#### Glide Server

[](#glide-server)

Alternatively, you can pass in a fully configured Glide `Server` object:

```
// @see https://flysystem.thephpleague.com/docs/adapter/aws-s3-v3/

$sourceFilesystem = new League\Flysystem\Filesystem(
    new League\Flysystem\AwsS3V3\AwsS3V3Adapter(/* S3 adapter configuration */);
);

$server = League\Glide\ServerFactory::create([
    'source' => $sourceFilesystem,
    'cache' => '/home/my-site.com/storage/glide-cache',
    'base_url' => '/glide',
]);

$flou = new Pboivin\Flou\RemoteImageFactory([
    'glideServer' => $server,
    'glideUrlSignKey' => 'secret',
]);

$image = $flou->image('test.jpg');

//...
```

If you're using Laravel, you can access the filesystem driver from the `Storage` facade:

```
// @see https://laravel.com/docs/filesystem

$sourceFilesystem = Illuminate\Support\Facades\Storage::disk('s3')->getDriver(),

//...
```

#### Caveats

[](#caveats)

When using `RemoteImageFactory`, it is too costly to fetch remote images to analyze their dimensions. Therefore, rendered images will not include `width` and `height` attributes. I recommend leveraging `useAspectRatio()` with a fixed aspect ratio value if possible.

Similarly, `useBase64Lqip()` will return a blank placeholder instead of a Base64 encoded LQIP.

[Image resampling](#image-resampling) is not available for remote images.

Examples
--------

[](#examples)

#### Fade-in image on load

[](#fade-in-image-on-load)

*Extra JS and CSS:*

```

    /**
     * vanilla-lazyload API reference:
     * https://github.com/verlok/vanilla-lazyload#options
     */

    document.addEventListener("DOMContentLoaded", () => {
        new LazyLoad({
            elements_selector: ".lazyload",

            callback_loaded: (el) => {
                const wrapper = el.closest(".lazyload-wrapper");
                if (wrapper) {
                    wrapper.classList.add("loaded");
                }
            }
        });
    });

    /* Example styles — adjust to taste */

    .lazyload-wrapper {
        position: relative;
        overflow: hidden;
    }

    .lazyload-wrapper .lazyload-lqip {
        filter: blur(10px);
        transform: scale(1.1);
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }

    .lazyload-wrapper.loaded .lazyload-lqip {
        opacity: 0;
        transition: opacity 0.5s;
        transition-delay: 0.5s;
    }

```

*Usage:*

```
