Skip to content

Commit

Permalink
replace intval() with (int)
Browse files Browse the repository at this point in the history
(int) is consider the best practice as it is up to 6x faster
  • Loading branch information
Nielsvanpach committed Dec 1, 2023
1 parent 9946fad commit d853ce0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions src/Drivers/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ protected function rgbaFromString(string $colorValue): array
$result[3] = 1;
} elseif (preg_match($rgbPattern, $colorValue, $matches)) {
$result = [];
$result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0;
$result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0;
$result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0;
$result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? (int)($matches[1]) : 0;
$result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? (int)($matches[2]) : 0;
$result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? (int)($matches[3]) : 0;
$result[3] = 1;
} elseif (preg_match($rgbaPattern, $colorValue, $matches)) {
$result = [];
$result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0;
$result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0;
$result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0;
$result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? (int)($matches[1]) : 0;
$result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? (int)($matches[2]) : 0;
$result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? (int)($matches[3]) : 0;
$result[3] = ($matches[4] >= 0 && $matches[4] <= 1) ? $matches[4] : 0;
} else {
throw InvalidColor::make($colorValue);
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/ImagickColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getInt(): int
$red = $this->getRedValue();
$green = $this->getGreenValue();
$blue = $this->getBlueValue();
$alpha = intval(round($this->getAlphaValue() * 255));
$alpha = (int)(round($this->getAlphaValue() * 255));

return ($alpha << 24) + ($red << 16) + ($green << 8) + $blue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/ImagickDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public function pixelate(int $pixelate = 50): self
$width = $this->getWidth();
$height = $this->getHeight();

$this->image->scaleImage(max(1, intval($width / $pixelate)), max(1, intval($height / $pixelate)));
$this->image->scaleImage(max(1, (int)($width / $pixelate)), max(1, (int)($height / $pixelate)));
$this->image->scaleImage($width, $height);

return $this;
Expand Down
16 changes: 8 additions & 8 deletions src/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function resizeWidth(
}

if (in_array(Constraint::PreserveAspectRatio, $constraints)) {
$calculatedHeight = max(1, intval(round($this->width / (new Size($originalWidth, $originalHeight))->aspectRatio())));
$calculatedHeight = max(1, (int)(round($this->width / (new Size($originalWidth, $originalHeight))->aspectRatio())));

if (in_array(Constraint::DoNotUpsize, $constraints)) {
$this->height = $calculatedHeight > $maximumHeight
Expand Down Expand Up @@ -105,7 +105,7 @@ public function resizeHeight(int $desiredHeight = null, array $constraints = [])
}

if (in_array(Constraint::PreserveAspectRatio, $constraints)) {
$calculatedWidth = max(1, intval(round($this->height * (new Size($originalWidth, $originalHeight))->aspectRatio())));
$calculatedWidth = max(1, (int)(round($this->height * (new Size($originalWidth, $originalHeight))->aspectRatio())));

if (in_array(Constraint::DoNotUpsize, $constraints)) {
$this->width = $calculatedWidth > $maximumWidth
Expand Down Expand Up @@ -134,7 +134,7 @@ public function align(AlignPosition $position, int|float $offsetX = 0, int|float
case AlignPosition::TopMiddle:
case AlignPosition::CenterTop:
case AlignPosition::MiddleTop:
$x = intval($this->width / 2);
$x = (int)($this->width / 2);
$y = 0 + $offsetY;
break;

Expand All @@ -150,7 +150,7 @@ public function align(AlignPosition $position, int|float $offsetX = 0, int|float
case AlignPosition::CenterLeft:
case AlignPosition::MiddleLeft:
$x = 0 + $offsetX;
$y = intval($this->height / 2);
$y = (int)($this->height / 2);
break;

case AlignPosition::Right:
Expand All @@ -159,7 +159,7 @@ public function align(AlignPosition $position, int|float $offsetX = 0, int|float
case AlignPosition::CenterRight:
case AlignPosition::MiddleRight:
$x = $this->width - $offsetX;
$y = intval($this->height / 2);
$y = (int)($this->height / 2);
break;

case AlignPosition::BottomLeft:
Expand All @@ -173,7 +173,7 @@ public function align(AlignPosition $position, int|float $offsetX = 0, int|float
case AlignPosition::BottomMiddle:
case AlignPosition::CenterBottom:
case AlignPosition::MiddleBottom:
$x = intval($this->width / 2);
$x = (int)($this->width / 2);
$y = $this->height - $offsetY;
break;

Expand All @@ -187,8 +187,8 @@ public function align(AlignPosition $position, int|float $offsetX = 0, int|float
case AlignPosition::Middle:
case AlignPosition::CenterCenter:
case AlignPosition::MiddleMiddle:
$x = intval($this->width / 2) + $offsetX;
$y = intval($this->height / 2) + $offsetY;
$x = (int)($this->width / 2) + $offsetX;
$y = (int)($this->height / 2) + $offsetY;
break;

default:
Expand Down

0 comments on commit d853ce0

Please sign in to comment.