From eccac6aaf4e0be72247d4af5ee24933125199be4 Mon Sep 17 00:00:00 2001 From: JCOGS Design <13821249+jcogs-design@users.noreply.github.com> Date: Tue, 7 Jun 2022 19:06:42 +0100 Subject: [PATCH] Remove some php 8.1 deprecation warnings replace some implicit float->int conversions with use of round(x,0) --- src/Box.php | 114 ++++++++++++++++++++++------------------------------ 1 file changed, 47 insertions(+), 67 deletions(-) diff --git a/src/Box.php b/src/Box.php index 3721a50..8338cb0 100644 --- a/src/Box.php +++ b/src/Box.php @@ -1,13 +1,10 @@ 0, + 'y' => 0, + 'width' => 100, + 'height' => 100 + ); public function __construct(&$image) { $this->im = $image; $this->fontColor = new Color(0, 0, 0); $this->strokeColor = new Color(0, 0, 0); - $this->box = new Rectangle(0, 0, 100, 100); } /** @@ -138,7 +139,8 @@ public function setTextShadow(Color $color, $xShift, $yShift) { $this->textShadow = array( 'color' => $color, - 'offset' => new Point($xShift, $yShift) + 'x' => $xShift, + 'y' => $yShift ); } @@ -198,7 +200,10 @@ public function setTextAlign($x = 'left', $y = 'top') */ public function setBox($x, $y, $width, $height) { - $this->box = new Rectangle($x, $y, $width, $height); + $this->box['x'] = $x; + $this->box['y'] = $y; + $this->box['width'] = $width; + $this->box['height'] = $height; } /** @@ -240,7 +245,10 @@ public function draw($text) if ($this->debug) { // Marks whole texbox area with color $this->drawFilledRectangle( - $this->box, + $this->box['x'], + $this->box['y'], + $this->box['width'], + $this->box['height'], new Color(rand(180, 255), rand(180, 255), rand(180, 255), 80) ); } @@ -250,10 +258,10 @@ public function draw($text) switch ($this->alignY) { case VerticalAlignment::Center: - $yAlign = ($this->box->getHeight() / 2) - ($textHeight / 2); + $yAlign = ($this->box['height'] / 2) - ($textHeight / 2); break; case VerticalAlignment::Bottom: - $yAlign = $this->box->getHeight() - $textHeight; + $yAlign = $this->box['height'] - $textHeight; break; case VerticalAlignment::Top: default: @@ -263,12 +271,13 @@ public function draw($text) $n = 0; foreach ($lines as $line) { $box = $this->calculateBox($line); + $boxWidth = $box[2] - $box[0]; switch ($this->alignX) { case HorizontalAlignment::Center: - $xAlign = ($this->box->getWidth() - $box->getWidth()) / 2; + $xAlign = ($this->box['width'] - $boxWidth) / 2; break; case HorizontalAlignment::Right: - $xAlign = ($this->box->getWidth() - $box->getWidth()); + $xAlign = ($this->box['width'] - $boxWidth); break; case HorizontalAlignment::Left: default: @@ -277,20 +286,18 @@ public function draw($text) $yShift = $lineHeightPx * (1 - $this->baseline); // current line X and Y position - $xMOD = $this->box->getX() + $xAlign; - $yMOD = $this->box->getY() + $yAlign + $yShift + ($n * $lineHeightPx); + $xMOD = $this->box['x'] + $xAlign; + $yMOD = $this->box['y'] + $yAlign + $yShift + ($n * $lineHeightPx); if ($line && $this->backgroundColor) { // Marks whole texbox area with given background-color $backgroundHeight = $this->fontSize; $this->drawFilledRectangle( - new Rectangle( - $xMOD, - $this->box->getY() + $yAlign + ($n * $lineHeightPx) + ($lineHeightPx - $backgroundHeight) + (1 - $this->lineHeight) * 13 * (1 / 50 * $this->fontSize), - $box->getWidth(), - $backgroundHeight - ), + $xMOD, + $this->box['y'] + $yAlign + ($n * $lineHeightPx) + ($lineHeightPx - $backgroundHeight) + (1 - $this->lineHeight) * 13 * (1 / 50 * $this->fontSize), + $boxWidth, + $backgroundHeight, $this->backgroundColor ); } @@ -298,33 +305,28 @@ public function draw($text) if ($this->debug) { // Marks current line with color $this->drawFilledRectangle( - new Rectangle( - $xMOD, - $this->box->getY() + $yAlign + ($n * $lineHeightPx), - $box->getWidth(), - $lineHeightPx - ), + $xMOD, + $this->box['y'] + $yAlign + ($n * $lineHeightPx), + $boxWidth, + $lineHeightPx, new Color(rand(1, 180), rand(1, 180), rand(1, 180)) ); } if ($this->textShadow !== false) { $this->drawInternal( - new Point( - $xMOD + $this->textShadow['offset']->getX(), - $yMOD + $this->textShadow['offset']->getY() - ), + $xMOD + $this->textShadow['x'], + $yMOD + $this->textShadow['y'], $this->textShadow['color'], $line ); + } $this->strokeText($xMOD, $yMOD, $line); $this->drawInternal( - new Point( - $xMOD, - $yMOD - ), + $xMOD, + $yMOD, $this->fontColor, $line ); @@ -349,7 +351,7 @@ protected function wrapTextWithOverflow($text) $line = $words[0]; for ($i = 1; $i < count($words); $i++) { $box = $this->calculateBox($line." ".$words[$i]); - if ($box->getWidth() >= $this->box->getWidth()) { + if (($box[4]-$box[6]) >= $this->box['width']) { $lines[] = $line; $line = $words[$i]; } else { @@ -369,38 +371,16 @@ protected function getFontSizeInPoints() return 0.75 * $this->fontSize; } - protected function drawFilledRectangle(Rectangle $rect, Color $color) + protected function drawFilledRectangle($x, $y, $width, $height, Color $color) { - imagefilledrectangle( - $this->im, - $rect->getLeft(), - $rect->getTop(), - $rect->getRight(), - $rect->getBottom(), + imagefilledrectangle($this->im, round($x,0), round($y,0), round($x + $width,0), round($y + $height,0), $color->getIndex($this->im) ); } - /** - * Returns the bounding box of a text. - * @param string $text - * @return Rectangle - */ protected function calculateBox($text) { - $bounds = imagettfbbox($this->getFontSizeInPoints(), 0, $this->fontFace, $text); - - $xLeft = $bounds[0]; // (lower|upper) left corner, X position - $xRight = $bounds[2]; // (lower|upper) right corner, X position - $yLower = $bounds[1]; // lower (left|right) corner, Y position - $yUpper = $bounds[5]; // upper (left|right) corner, Y position - - return new Rectangle( - $xLeft, - $yUpper, - $xRight - $xLeft, - $yLower - $yUpper - ); + return imageftbbox($this->getFontSizeInPoints(), 0, $this->fontFace, $text); } protected function strokeText($x, $y, $text) @@ -409,19 +389,19 @@ protected function strokeText($x, $y, $text) if ($size <= 0) return; for ($c1 = $x - $size; $c1 <= $x + $size; $c1++) { for ($c2 = $y - $size; $c2 <= $y + $size; $c2++) { - $this->drawInternal(new Point($c1, $c2), $this->strokeColor, $text); + $this->drawInternal($c1, $c2, $this->strokeColor, $text); } } } - protected function drawInternal(Point $position, Color $color, $text) + protected function drawInternal($x, $y, Color $color, $text) { - imagettftext( + imagefttext( $this->im, $this->getFontSizeInPoints(), 0, // no rotation - $position->getX(), - $position->getY(), + round($x,0), + round($y,0), $color->getIndex($this->im), $this->fontFace, $text