2017 © Pedro Peláez
 

library responsive-images

Create responsive images with PHP, using the srcset and sizes spec.

image

brendt/responsive-images

Create responsive images with PHP, using the srcset and sizes spec.

  • Sunday, April 23, 2017
  • by brendt
  • Repository
  • 1 Watchers
  • 15 Stars
  • 268 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 34 Versions
  • 2 % Grown

The README.md

Build Status Scrutinizer Code Quality, (*1)

Responsive Images

Automatically generate responsive images to work with the srcset and sizes spec. (http://responsiveimages.org/), (*2)

composer require brendt/responsive-images

Usage

use Brendt\Image\ResponsiveFactory;

$factory = new ResponsiveFactory();
$image = $factory->create('img/image.jpeg');
<img src="<?= $image->src() ?>" 
     srcset="<?= $image->srcset() ?>"/>

This sample would generate something like:, (*3)

<img src="/image.jpeg" 
     srcset="/image-384.jpg 384w,
             /image-768.jpg 768w,
             /image-1152.jpg 1152w,
             /image-1536.jpg 1536w,
             /image.jpg 1920w" />

Configuration

The ResponsiveFactory can take a ResponsiveFactoryConfigurator object which will set the needed parameters. A default configurator DefaultConfigurator is provider out of the box, and uses the following parameters:, (*4)

[
    'driver'           => 'gd',
    'includeSource'    => true,
    'publicPath'       => './',
    'sourcePath'       => './',
    'rebase'           => false,
    'enableCache'      => false,
    'optimize'         => false,
    'scaler'           => 'filesize',
    'stepModifier'     => 0.5,
    'minFileSize'      => 5000,
    'maxFileSize'      => null,
    'minWidth'         => 300,
    'maxWidth'         => null,
    'sizes'            => [ 1920, 840, 300 ],
    'optimizerOptions' => [],
]

You can override these parameters by providing and array to the DefaultConfigurator, or create a whole new configurator which implements ResponsiveFactoryConfigurator., (*5)

$factory = new ResponsiveFactory(new DefaultConfigurator([
    'driver'       => 'imagick',
    'sourcePath'   => './src',
    'publicPath'   => './public',
    'enableCache'  => true,
]));

All configuration options

  • driver: the image driver to use. Defaults to gd. Possible options are gd or imagick.
  • includeSource: whether to include the source image in the srcset. Defaults to true.
  • sourcePath: the path to load image source files. Defaults to ./.
  • publicPath: the path to render image files. Defaults to ./.
  • rebase: ignore the path of the requested image when searching in the source directory. Defaults to false.
  • enableCache: enable or disable image caching. Enabling the cache wont' override existing images. Defaults to false.
  • optimize: enable or disable the use of different optimizers (if installed on the system). Defaults to false.
  • scaler: which scaler algorithm to use. Defaults to filesize. Possible options are filesize, width or sizes.
  • stepModifier: a percentage (between 0 and 1) which is used to create different image sizes. The higher this modifier, the more image variations will be rendered. Defaults to 0.5.
  • minFileSize: the minimum image filesize in bytes. Defaults to 5000B (5KB).
  • maxFileSize: the maximum image filesize in bytes. Defaults to null.
  • minWidth: the minimum image size in pixels. No images with size smaller than this number will be rendered. Defaults to 300 pixels.
  • maxWidth: the maximum image size in pixels. No images with size smaller than this number will be rendered. Defaults to null.
  • sizes: this parameter is used when the sizes scaler is enabled. This scaler will generate a fixed set of sizes, based on this array. The expected values are the widths of the generated images. Defaults to [] (empty array).
  • optimizerOptions: options to pass to the image optimizer library. See https://github.com/psliwa/image-optimizer for more information.

Paths

The sourcePath parameter is used to define where image source files are located. In case of the first example and above configuration, the image file should be saved in ./src/img/image.jpeg., (*6)

The publicPath parameter is used to save rendered images into. This path should be the public directory of your website. The above example would render images into ./public/img/image.jpeg., (*7)

Path rebasing

When the rebase option is enabled, source file lookup will differ: only the filename is used to search the file in the source directory. An example would be the following., (*8)

// Without rebase

$options = [
    'sourcePath' => './src/images',
    'publicPath' => './public',
];

$image = $factory->create('/img/responsive/image.jpeg');

// Source file is searched in './src/images/img/responsive/image.jpeg' 
// Public files are saved in './public/img/responsive/image-x.jpg'
// With rebase

$options = [
    'sourcePath' => './src/images',
    'publicPath' => './public',
    'rebase'     => true,
];

$image = $factory->create('/img/responsive/image.jpeg');

// Source file is searched in './src/images/image.jpeg'  
// Public files are saved in './public/img/responsive/image-x.jpg'

The Versions

23/04 2017

dev-master

9999999-dev

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

23/04 2017

dev-develop

dev-develop

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

23/04 2017

1.9.1

1.9.1.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

20/04 2017

1.9.0

1.9.0.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

20/04 2017

1.8.2

1.8.2.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

19/04 2017

1.8.1

1.8.1.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

19/04 2017

1.8.0

1.8.0.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

19/04 2017

1.7.3

1.7.3.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

15/04 2017

1.7.2

1.7.2.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

15/04 2017

1.7.1

1.7.1.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

15/04 2017

1.7.0

1.7.0.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

15/04 2017

1.6.0

1.6.0.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

13/04 2017

1.5.1

1.5.1.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

29/03 2017

1.5.0

1.5.0.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

11/03 2017
04/03 2017
26/02 2017

1.2.2

1.2.2.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

25/02 2017

1.2.1

1.2.1.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

25/02 2017

1.2.0

1.2.0.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

22/02 2017

1.1.2

1.1.2.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

17/02 2017

1.1.1

1.1.1.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

17/02 2017

1.1.0

1.1.0.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

17/02 2017

1.0.2

1.0.2.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

14/02 2017

1.0.1

1.0.1.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

14/02 2017

1.0.0

1.0.0.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

28/11 2016

0.2.1

0.2.1.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

27/11 2016

0.2.0

0.2.0.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

25/11 2016

0.1.3

0.1.3.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

24/11 2016

0.1.2

0.1.2.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

24/11 2016

0.1.1

0.1.1.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent

23/11 2016

0.1.0

0.1.0.0

Create responsive images with PHP, using the srcset and sizes spec.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brent