Skip to content

Novis\Image\Image::withOverlay

$image->withOverlay(
    Image $overlay,
    ?{ gravity?: Gravity, x?: int, y?: int, opacity?: float, blend?: Blend } $options = null,
): Image

Draws another image on top of this one.

The image on top is called the overlay. It is a whole Image of its own, so it can have its own steps, for example resized or grayscaled. Those steps run first, and then the overlay is drawn.

gravity places the overlay, for example Image::Gravity::SouthEast for the bottom-right corner. The default is Image::Gravity::Center. x and y give the position of the overlay's top-left corner in pixels instead. They may be negative. The parts of the overlay that are outside the image are cut off, and the image keeps its size.

opacity makes the overlay partly transparent. It is from 0.0 to 1.0, and the default is 1.0. blend chooses how the colours are mixed. The default is Image::Blend::Normal, where the overlay covers the image. Image::Blend::Multiply makes the result darker, and Image::Blend::Screen makes it lighter.

withOverlay 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: a withFormat or keepingMetadata step on the overlay changes nothing. The result is written in the format of the image below.

Parameters

  • $overlayImage
  • $options?{ gravity?: Gravity, x?: int, y?: int, opacity?: float, blend?: Blend }default null

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

Watermark in the corner

<?nvs
use Novis\Image\Color;
// Shows how to put a small logo in the bottom-right corner of a photo.
use Novis\Image\Image;
Image $photo = Image::create(400, 300, Color::hex('#336699'));
Image $logo = Image::create(40, 20, Color::hex('#ffffff'));
var $raw = $photo->withOverlay($logo, { gravity: Image::Gravity::SouthEast })->raw();
// The image keeps its size. This prints 400x300.
echo $raw->width, 'x', $raw->height, "\n";
// The last pixel is in the logo. This prints ffffffff, which is white.
echo Core\Encoding::toHex(Core\Bytes::slice($raw->pixels, -4)), "\n";
Output
400x300
ffffffff

Faint stamp

<?nvs
use Novis\Image\Color;
// Shows how to draw a half-transparent stamp at an exact position.
use Novis\Image\Image;
Image $page = Image::create(100, 100, Color::hex('#ffffff'));
Image $stamp = Image::create(30, 10, Color::hex('#ff0000'));
// The top-left corner of the stamp is at x 5 and y 5.
var $raw = $page->withOverlay($stamp, { x: 5, y: 5, opacity: 0.5 })->raw();
// Half red and half white gives pink. This prints ff8080ff.
int $offset = (5 * 100 + 5) * 4;
echo Core\Encoding::toHex(Core\Bytes::slice($raw->pixels, $offset, 4)), "\n";
// The first pixel is outside the stamp. This prints ffffffff.
echo Core\Encoding::toHex(Core\Bytes::slice($raw->pixels, 0, 4)), "\n";
Output
ff8080ff
ffffffff

Shade with multiply

<?nvs
use Novis\Image\Color;
// Shows how an overlay with its own steps darkens a picture with `Image::Blend::Multiply`.
use Novis\Image\Image;
Image $picture = Image::create(200, 100, Color::hex('#ffcc00'));
// The overlay is a gray canvas. It is scaled to the size of the picture first.
Image $shade = Image::create(20, 10, Color::hex('#808080'))->resized({ width: 200, upscale: true });
// Multiply makes each colour darker: 255 becomes 128, and 204 becomes 102.
var $raw = $picture->withOverlay($shade, { blend: Image::Blend::Multiply })->raw();
// This prints 806600ff.
echo Core\Encoding::toHex(Core\Bytes::slice($raw->pixels, 0, 4)), "\n";
Output
806600ff