Skip to content

Novis\Image\Image::rotated

$image->rotated(
    float $degrees,
    ?{ background?: Color } $options = null,
): Image

Turns an image clockwise by a number of degrees.

A turn by 90, 180 or 270 degrees moves the pixels and loses nothing. A turn by 90 or 270 swaps the width and the height. Any other angle makes the image larger, so the whole turned picture fits. background is the colour of the new corners. Without it, the corners are transparent. A negative angle turns the image counter-clockwise.

rotated 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: Image::open already turns a photo upright from the orientation the camera wrote into it. You only need rotated for a turn that the photo does not describe.

Parameters

  • $degreesfloat
  • $options?{ background?: Color }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

Quarter turn

<?nvs
use Novis\Image\Color;
// Shows that a quarter turn swaps the width and the height.
use Novis\Image\Image;
Image $photo = Image::create(600, 400, Color::hex('#336699'));
// This prints 400x600.
var $turned = $photo->rotated(90.0)->raw();
echo $turned->width, 'x', $turned->height, "\n";
// A turn by 180 degrees keeps the size. This prints 600x400.
var $upsideDown = $photo->rotated(180.0)->raw();
echo $upsideDown->width, 'x', $upsideDown->height, "\n";
Output
400x600
600x400

Small angle with background

<?nvs
use Novis\Image\Color;
// Shows how a small turn makes the image larger and fills the corners with `background`.
use Novis\Image\Image;
Image $card = Image::create(100, 100, Color::hex('#cc6633'));
// The turned square needs more room, so the size grows. This prints 142x142.
Image $tilted = $card->rotated(45.0, { background: Color::hex('#ffffff') });
var $raw = $tilted->raw();
echo $raw->width, 'x', $raw->height, "\n";
// The top-left pixel is a corner, so it has the background colour. This prints 255 255 255.
array<mixed> $corner = Core\Bytes::unpack(Core\Bytes::slice($raw->pixels, 0, 4), 'C4');
echo $corner[0], ' ', $corner[1], ' ', $corner[2], "\n";
Output
142x142
255 255 255

Original stays the same

<?nvs
use Novis\Image\Color;
// Shows that `rotated` returns a new image, so you can make several versions from one image.
use Novis\Image\Image;
Image $photo = Image::create(300, 200, Color::hex('#22aa55'));
Image $left = $photo->rotated(-90.0);
Image $right = $photo->rotated(90.0);
// `rotated` does not change `$photo`, so `$photo` is still 300x200.
// This prints 300x200, 200x300 and 200x300.
array<Image> $versions = [$photo, $left, $right];
foreach ($versions as Image $version) {
var $raw = $version->raw();
echo $raw->width, 'x', $raw->height, "\n";
}
Output
300x200
200x300
200x300