function Rectangle::rotate

Same name in other branches
  1. 9 core/lib/Drupal/Component/Utility/Rectangle.php \Drupal\Component\Utility\Rectangle::rotate()
  2. 8.9.x core/lib/Drupal/Component/Utility/Rectangle.php \Drupal\Component\Utility\Rectangle::rotate()
  3. 10 core/lib/Drupal/Component/Utility/Rectangle.php \Drupal\Component\Utility\Rectangle::rotate()

Rotates the rectangle.

Parameters

float $angle: Rotation angle.

Return value

$this

File

core/lib/Drupal/Component/Utility/Rectangle.php, line 83

Class

Rectangle
Rectangle rotation algebra class.

Namespace

Drupal\Component\Utility

Code

public function rotate($angle) {
    // PHP 5.5 GD bug: https://bugs.php.net/bug.php?id=65148: To prevent buggy
    // behavior on negative multiples of 30 degrees we convert any negative
    // angle to a positive one between 0 and 360 degrees.
    $angle -= floor($angle / 360) * 360;
    // For some rotations that are multiple of 30 degrees, we need to correct
    // an imprecision between GD that uses C floats internally, and PHP that
    // uses C doubles. Also, for rotations that are not multiple of 90 degrees,
    // we need to introduce a correction factor of 0.5 to match the GD
    // algorithm used in PHP 5.5 (and above) to calculate the width and height
    // of the rotated image.
    if ((int) $angle == $angle && $angle % 90 == 0) {
        $imprecision = 0;
        $correction = 0;
    }
    else {
        $imprecision = -1.0E-5;
        $correction = 0.5;
    }
    // Do the trigonometry, applying imprecision fixes where needed.
    $rad = deg2rad($angle);
    $cos = cos($rad);
    $sin = sin($rad);
    $a = $this->width * $cos;
    $b = $this->height * $sin + $correction;
    $c = $this->width * $sin;
    $d = $this->height * $cos + $correction;
    if ((int) $angle == $angle && in_array($angle, [
        60,
        150,
        300,
    ])) {
        $a = $this->fixImprecision($a, $imprecision);
        $b = $this->fixImprecision($b, $imprecision);
        $c = $this->fixImprecision($c, $imprecision);
        $d = $this->fixImprecision($d, $imprecision);
    }
    // This is how GD on PHP5.5 calculates the new dimensions.
    $this->boundingWidth = abs((int) $a) + abs((int) $b);
    $this->boundingHeight = abs((int) $c) + abs((int) $d);
    return $this;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.