Imagine filter to resize and pad images to a set width and height

dev-master 2014-02-24 05:41 UTC

This package is not auto-updated.

Last update: 2022-09-26 16:39:21 UTC


This is a simple Imagine Filter to allow you to resize and pad images with whitespace to ensure consistent sizing without cropping or stretching.


Add "rtablada/resize-and-pad": "dev-master" to your composer.json file.


Using this filter is quite similar to the Transformation Filter in Imagine. Inject your instance of Imagine and then call setSize. Here we will use a GD instance, but any instance of Imagine\Image\ImageInterface will do.

$imagine = new \Imagine\GD\Imagine;
$resizer = new \Rtablada\Images\ResizeAndPad($imagine);
$image = $imagine->open($pathToImage);

$resizer->setSize(200, 200);
$output = $resizer->apply($image);

The setSize and apply functions allow for chaining so the above could be written like this:

$resizer->setSize(200, 200)->apply($image)->save($outputPath);

Use with Stapler

This Filter was originally built for use within a Laravel project using Stapler. Using this filter with Stapler is quite simple when defining your styles:

$this->hasAttachedFile('avatar', [
	'styles' => [
		'medium' => '300x300',
		'thumb' => function($file, $imagine) {
			$resizer = \Rtablada\Images\ResizeAndPad($imagine);
			return $resizer->setSize(100,100)->apply($file);