Skip to content

Novis\Image\Image::flipped

$image->flipped(Axis $axis): Image$image->flipped(
    Axis $axis,
): Image

Mirrors an image.

flipped(Image::Axis::Horizontal) swaps left and right, like a mirror next to the image. flipped(Image::Axis::Vertical) swaps top and bottom. The size stays the same, and no pixel changes its colour.

flipped 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: flipping the same way twice gives the original pixels back.

Parameters

  • $axisAxis

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

Top to bottom

<?nvs
use Novis\Image\Color;
// Shows how `flipped(Image::Axis::Vertical)` moves the top of an image to the bottom.
use Novis\Image\Image;
// A red bar sits at the top of a transparent square.
Image $bar = Image::create(100, 20, Color::hex('#cc3333'))
->resized({ width: 100, height: 100, fit: Image::Fit::Contain, gravity: Image::Gravity::North });
// The fourth number is the alpha of the top-left pixel. This prints 255, and then 0 after the flip.
array<Image> $versions = [$bar, $bar->flipped(Image::Axis::Vertical)];
foreach ($versions as Image $version) {
array<mixed> $pixel = Core\Bytes::unpack(Core\Bytes::slice($version->raw()->pixels, 0, 4), 'C4');
echo $pixel[3], "\n";
}
Output
255
0

Mirror a selfie

<?nvs
use Novis\Image\Color;
use Novis\Image\Format;
// Shows how to mirror a photo from left to right, as a front camera shows it.
use Novis\Image\Image;
// A blue stripe sits at the left edge of a transparent image.
Image $selfie = Image::create(20, 100, Color::hex('#3366cc'))
->resized({ width: 100, height: 100, fit: Image::Fit::Contain, gravity: Image::Gravity::West });
// After the flip, the stripe is at the right edge. The top-left pixel is transparent, so this prints 0.
tainted bytes $mirrored = $selfie->flipped(Image::Axis::Horizontal)->withFormat(Format::Png)->encode();
array<mixed> $pixel = Core\Bytes::unpack(Core\Bytes::slice(Image::open($mirrored)->raw()->pixels, 0, 4), 'C4');
echo $pixel[3], "\n";
Output
0

Flip twice

<?nvs
use Novis\Image\Color;
// Shows that flipping the same way twice gives the original pixels back.
use Novis\Image\Image;
Image $photo = Image::create(30, 10, Color::hex('#22aa55'))
->resized({ width: 40, height: 40, fit: Image::Fit::Contain, gravity: Image::Gravity::NorthEast });
tainted bytes $original = $photo->raw()->pixels;
tainted bytes $once = $photo->flipped(Image::Axis::Horizontal)->raw()->pixels;
tainted bytes $twice = $photo->flipped(Image::Axis::Horizontal)->flipped(Image::Axis::Horizontal)->raw()->pixels;
// This prints "once: different" and "twice: same".
echo 'once: ', $once == $original ? 'same' : 'different', "\n";
echo 'twice: ', $twice == $original ? 'same' : 'different', "\n";
Output
once: different
twice: same