Skip to content

Novis\Image\Image::flattened

$image->flattened(Color $background): Image$image->flattened(
    Color $background,
): Image

Puts an image on a background of one colour.

Every transparent or half-transparent pixel is mixed with the background colour. The result has no transparent pixels. The alpha of the background colour is not used.

flattened returns a new Image and does not change the one you called it on. The work happens when encode, encodeVariants or raw runs.

Good to know: JPEG has no transparency. Call flattened before you save an image with transparent parts as Format::Jpeg, so you choose the colour of those parts.

Parameters

  • $backgroundColor

Return value

Returns Image.

Changelog

VersionChange
0.0.6Each Novis\Image\Image method that returns a new image is named for it, for example resized(), blurred() and withText(). keepingMetadata() replaces metadata({ keep: true }), and encodeVariants() replaces variants(). Image::compare returns a Comparison with the property differenceImage, which was Diff with diff.

Examples

White behind a logo

<?nvs
use Novis\Image\Color;
use Novis\Image\Format;
// Shows how to put a transparent image on a white background before you save it as JPEG.
use Novis\Image\Image;
// A canvas without a fill colour is transparent.
Image $logo = Image::create(100, 100);
var $raw = $logo->flattened(Color::hex('#ffffff'))->raw();
array<mixed> $pixel = Core\Bytes::unpack(Core\Bytes::slice($raw->pixels, 0, 4), 'C4');
// This prints 255 255 255 255: a white pixel that is not transparent.
echo $pixel[0], ' ', $pixel[1], ' ', $pixel[2], ' ', $pixel[3], "\n";
tainted bytes $jpeg = $logo->flattened(Color::hex('#ffffff'))->withFormat(Format::Jpeg)->encode();
echo Image::mime(Image::info($jpeg)->format), "\n";
Output
255 255 255 255
image/jpeg

Half transparent

<?nvs
use Novis\Image\Color;
// Shows how a half-transparent colour is mixed with the background.
use Novis\Image\Image;
// Red with an alpha of 0.5 is half transparent.
Image $glass = Image::create(10, 10, Color::rgba(255, 0, 0, 0.5));
// Half red and half white gives pink. This prints 255 127 127 255.
var $raw = $glass->flattened(Color::hex('#ffffff'))->raw();
array<mixed> $pixel = Core\Bytes::unpack(Core\Bytes::slice($raw->pixels, 0, 4), 'C4');
echo $pixel[0], ' ', $pixel[1], ' ', $pixel[2], ' ', $pixel[3], "\n";
Output
255 127 127 255

Colour the bands

<?nvs
use Novis\Image\Color;
// Shows how to colour the empty bands that `Image::Fit::Contain` adds around a picture.
use Novis\Image\Image;
Image $photo = Image::create(400, 200, Color::hex('#336699'));
// The bands above and below the picture become light gray.
var $raw = $photo->resized({ width: 200, height: 200, fit: Image::Fit::Contain })->flattened(Color::hex('#eeeeee'))->raw();
// The top-left pixel is in a band. This prints 238 238 238 255.
array<mixed> $pixel = Core\Bytes::unpack(Core\Bytes::slice($raw->pixels, 0, 4), 'C4');
echo $pixel[0], ' ', $pixel[1], ' ', $pixel[2], ' ', $pixel[3], "\n";
Output
238 238 238 255