Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some php 8.1 deprecation warnings #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 47 additions & 67 deletions src/Box.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<?php
namespace GDText;

use GDText\Struct\Point;
use GDText\Struct\Rectangle;

class Box
{
/**
* @var resource
* @var resource|\GdImage
*/
protected $im;

Expand Down Expand Up @@ -77,16 +74,20 @@ class Box
protected $backgroundColor = false;

/**
* @var Rectangle
* @var array
*/
protected $box;
protected $box = array(
'x' => 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);
}

/**
Expand Down Expand Up @@ -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
);
}

Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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)
);
}
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -277,54 +286,47 @@ 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
);
}

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
);
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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
Expand Down