PHPackages                             wa72/adaptimage - 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. wa72/adaptimage

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

wa72/adaptimage
===============

Resize adaptive images to pre-defined sizes, generate thumbnails, and cache them

v0.5.2(2y ago)1121MITPHPPHP &gt;=7.4CI failing

Since Feb 11Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/wasinger/adaptimage)[ Packagist](https://packagist.org/packages/wa72/adaptimage)[ RSS](/packages/wa72-adaptimage/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (3)Versions (9)Used By (0)

AdaptImage
==========

[](#adaptimage)

[![Build Status](https://camo.githubusercontent.com/fdddaef226eca53d979987591a4e8aa66b88736ca2660552531b3b6ba2fd26c3/68747470733a2f2f7472617669732d63692e6f72672f776173696e6765722f6164617074696d6167652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/wasinger/adaptimage)[![Latest Version](https://camo.githubusercontent.com/94a3c156fce8139a161ad2a11b2a11b5b9cac30afab71b8a1b181392ddfb0430/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f776137322f6164617074696d6167652e737667)](https://packagist.org/packages/wa72/adaptimage)

A small PHP library that lets you easily resize images to pre-defined sizes (useful for adaptive images), generate thumbnails, and cache them.

Built on top of the [Imagine](https://github.com/avalanche123/Imagine) library, it is designed to be framework agnostic, object orientated, customizable, and extendable.

**Features**:

- Allows to define multiple allowed image sizes. Each defined image size can be given additional [Imagine filters](http://imagine.readthedocs.org/en/latest/_static/API/Imagine/Filter/FilterInterface.html), e.g. for sharpening.
- The resized images are written to cache files whose names are computed from the input image and the applied transformation. You can easily write your own naming roules by implementing [`OutputPathGeneratorInterface`](src/Output/OutputPathGeneratorInterface.php). The next time a resized image is required the cached file is returned. The cached file will be regenerated when the original file changes.
- **New**: Helper classes for responsive images according to the HTML5 spec with `srcset` and `sizes` attritubes that generate the resized image files and the value for the `srcset` attribute.
- Adaptive images: acquire an image for an arbitrary screen width and get a resized image with the nearest defined size. You can select whether you want the next smaller images (that completely fits into the given width) or the next bigger image (that can be downscaled by the browser to fill the screen).
- Thumbnails: supports "inset" and "outbound" (crop) modes, supports custom crops for single images.
- Ability to simulate resize operations, i.e. calculating the resulting width and height of an image without actually transforming it. Useful for generating lots of thumbnail `` tags in an html page with width and height attributes.
- When generating resized images lockfiles are used to prevent race conditions.

Installation
------------

[](#installation)

```
composer require "wa72/adaptimage"

```

Usage
-----

[](#usage)

First, define the allowed image sizes in the application, matching your CSS media query breakpoints. This is done using `ImageResizeDefinition` objects that besides the desired image width and height may contain additional transformation filters, such as for sharpening or adding watermarks.

```
use Wa72\AdaptImage\ImageResizeDefinition;
use Wa72\AdaptImage\ImagineFilter\Sharpen;

$sizes = array(
    ImageResizeDefinition::create(1600, 1200),
    ImageResizeDefinition::create(1280, 1024),
    ImageResizeDefinition::create(768, 576),
    ImageResizeDefinition::create(1024, 768)->addFilter(new Sharpen() // example with additional sharpen filter
);
```

Next, we need an object implementing [`OutputPathGeneratorInterface`](src/Output/OutputPathGeneratorInterface.php) that is able to compute the path and filename where a resized image should be stored (depending on the input file and the applied transformations). [`OutputPathGeneratorBasedir`](src/Output/OutputPathGeneratorBasedir.php) is a class that generates output filenames that are all placed in per-transformation subdirectories within a common base directory.

```
use Wa72\AdaptImage\Output\OutputPathGeneratorBasedir;

$cachedir = __DIR__  . '/cache';
$output_path_generator = new OutputPathGeneratorBasedir($cachedir);
```

Now we are ready to define an [`AdaptiveImageResizer`](src/AdaptiveImageResizer.php) and a [`ThumbnailGenerator`](src/ThumbnailGenerator.php) that will do the work for us:

```
use Wa72\AdaptImage\AdaptiveImageResizer;
use Wa72\AdaptImage\ThumbnailGenerator;
use Imagine\Imagick\Imagine; // or some other Imagine version

$imagine = new Imagine();

$thumbnail_generator = new ThumbnailGenerator($imagine, $output_path_generator, 150, 150, 'inset');
$resizer = new AdaptiveImageResizer($imagine, $output_path_generator, $sizes);
```

Both the `ThumbnailGenerator` and the `AdaptiveImageResizer` get an `ImageFileInfo` object of the original file as input and will return another `ImageFileInfo` object pointing to the generated resized image file. The ThumbnailGenerator will generate a thumbnail with the dimensions defined in the constructor, while the AdaptiveImageResizer will from the defined image sizes select the one that best matches `$client_screen_width` and will scale the image to the defined size. Both will resize the image only if there isn't a resized version yet or if the original file is newer than the resized one, but just return the cached file if it is already there.

```
use Wa72\AdaptImage\ImageFileInfo;

$image = ImageFileInfo::createFromFile('/path/to/original/image');

$thumbnail = $thumbnail_generator->thumbnail(true, $image);

$client_screen_width = 1024; // typically you get this value from a cookie

$resized_image = $resizer->resize(true, $image, $client_screen_width);
```

`$thumbnail` and `$resized_image` are `ImageFileInfo` objects containing the path and name of the generated file as well as it's image type, width, and height. Use this information for generating html `img` tags or deliver the resized image to the user, e.g. using a `BinaryFileResponse` from Symfony:

```
$response = new Symfony\Component\HttpFoundation\BinaryFileResponse($resized_image->getPathname());
$response->prepare($request);
return $response;
```

For more documentation, please see the source code, it should be well commented.

© 2015 Christoph Singer. Licensed under the MIT license.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~428 days

Recently: every ~468 days

Total

8

Last Release

752d ago

PHP version history (3 changes)v0.1.0PHP &gt;=5.4

v0.5.0PHP &gt;=7.1

v0.5.1PHP &gt;=7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/bcf22c073165b9bc11275e601e5a77ca36137628c683cbeb1b8162c522d19718?d=identicon)[wasinger](/maintainers/wasinger)

---

Top Contributors

[![wasinger](https://avatars.githubusercontent.com/u/2566999?v=4)](https://github.com/wasinger "wasinger (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/wa72-adaptimage/health.svg)

```
[![Health](https://phpackages.com/badges/wa72-adaptimage/health.svg)](https://phpackages.com/packages/wa72-adaptimage)
```

###  Alternatives

[liip/imagine-bundle

This bundle provides an image manipulation abstraction toolkit for Symfony-based projects.

1.7k38.3M217](/packages/liip-imagine-bundle)[contao/imagine-svg

Contao Imagine SVG library

863.0M15](/packages/contao-imagine-svg)[folklore/image

Image manipulation library for Laravel 5 based on Imagine and inspired by Croppa for easy url based manipulation

270248.2k5](/packages/folklore-image)[rokka/imagine-vips

libvips adapter for imagine

43564.1k5](/packages/rokka-imagine-vips)[orchestra/imagine

Imagine (Wrapper) Component for Laravel

70207.0k3](/packages/orchestra-imagine)[media-alchemyst/media-alchemyst

An Object Oriented wrapper for easy multimedia conversion, based on Imagine, FFMpeg, SwfTools, Unoconv and other libs

65216.4k1](/packages/media-alchemyst-media-alchemyst)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
