joomla/joomla-framework — pull request #222 by piotr-cz (2013-09-24 07:54:01+00:00)
Title: Feature: ImageFilterBackgroundfill

New image filter that allows filling background of image with selected color.
Obviously only applies to transparent images (like GIFs and PNGs) with 8bit as well as on 24bit color depth.

At the moment when saving images with transparency as JPEG, background color defaults to black.

I found this functionality useful when converting PNGs to JPEGs.

Note:
There is [similar PR](https://github.com/joomla/joomla-cms/pull/1562) in the CMS.

## Usage
```PHP
// Load up image
$img = new JImage(JPATH_SITE . '/images/joomla_green.gif');

// Set background color using JImageFilterBackground
$img->filter('backgroundfill', array('color' => '#ff0000'));

// Save new image to file
$img->toFile(JPATH_SITE . '/images/joomla_green_bg.jpg', IMAGETYPE_JPEG, array('quality' => 85));
```

![joomla_green](https://f.cloud.github.com/assets/612953/819965/b3d07e0e-efbb-11e2-84eb-0bedc2838be0.gif)
![joomla_green_bg](https://f.cloud.github.com/assets/612953/819966/bbfafc1c-efbb-11e2-9ee9-51a73ab30336.jpg)

Alternatively color may be defined as an array.
If a color value is omitted, it defaults to 0.
```PHP
$img->filter('backgroundfill', array('color' => array('red' => 255, 'green' => 0, 'blue' => 0));
```

It's possible to use semi-transparent color too ('ff' or 0 means full opacity):
`array('color' => '#ff000080')` equals to `array('color' => array('red' => 255, 'alpha' => 64));`

## Side note
PHP defines alpha in 7-bit range (0..128), see [php.net/imagecolorallocatealpha](http://www.php.net/manual/en/function.imagecolorallocatealpha.php)
RGBA hexadecimal uses 8-bit values (FF..00), see [wikipedia:RGBA](http://en.wikipedia.org/wiki/RGBA#RGBA_hexadecimal)