Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
abordage committed May 28, 2022
1 parent 640d310 commit b9d3620
Showing 1 changed file with 70 additions and 50 deletions.
120 changes: 70 additions & 50 deletions src/OpenGraphImages.php
Original file line number Diff line number Diff line change
@@ -335,39 +335,6 @@ protected function getTextBoxSize(string $text, string $font, int $size): array
return $box;
}

/**
* @throws ImagickException
* @throws ImagickDrawException
*/
protected function fitTextToBox(): void
{
for ($wordwrap = 50; $wordwrap >= 20; $wordwrap--) {
$text = $this->multiLine($this->text, $wordwrap);
$textBoxSize = $this->getTextBoxSize($text, $this->font, $this->fontSize);

$this->textBoxWidth = $textBoxSize['width'];
$this->textBoxHeight = $textBoxSize['height'];

if ($this->textBoxWidth <= $this->maxTextWidth) {
if ($this->textBoxHeight > $this->maxTextHeight) {
for ($countLines = count(explode("\n", $text)); $countLines > 1; $countLines--) {
$position = mb_strrpos($text, "\n") ?: null;
$text = mb_substr($text, 0, $position);

$textBoxSize = $this->getTextBoxSize($text, $this->font, $this->fontSize);
$this->textBoxHeight = $textBoxSize['height'];
if ($textBoxSize['height'] <= $this->maxTextHeight) {
break;
}
}
}

break;
}
}
$this->text = $text;
}

/**
* @throws ImagickException
* @throws ImagickDrawException
@@ -464,38 +431,66 @@ protected function setParameters(): void
* @throws ImagickException
* @throws ImagickDrawException
*/
protected function setAppNameParameters(): void
protected function setTextParameters(): void
{
$this->setAppNameBox();
$this->setAppNameCoordinates();
$this->setTextToBox();
$this->setTextCoordinates();
}

switch ($this->appNameDecorationStyle) {
case 'line':
$this->setLineCoordinates();
/**
* @throws ImagickException
* @throws ImagickDrawException
*/
protected function setTextToBox(): void
{
$this->maxTextWidth = intval($this->imageWidth * 0.8);
$this->maxTextHeight = intval($this->imageHeight * 0.6);

break;
case 'label':
$this->setLabelCoordinates();
$this->fitTextToWidth();
$this->fitTextToHeight();
}

break;
case 'rectangle':
$this->setRectangleCoordinates();
/**
* @throws ImagickException
* @throws ImagickDrawException
*/
protected function fitTextToWidth(): void
{
for ($wordwrap = 50; $wordwrap >= 20; $wordwrap--) {
$this->text = $this->multiLine($this->text, $wordwrap);
$textBoxSize = $this->getTextBoxSize($this->text, $this->font, $this->fontSize);

$this->textBoxWidth = $textBoxSize['width'];
$this->textBoxHeight = $textBoxSize['height'];

if ($this->textBoxWidth <= $this->maxTextWidth) {
break;
}
}
}

/**
* @throws ImagickException
* @throws ImagickDrawException
*/
protected function setTextParameters(): void
protected function fitTextToHeight(): void
{
$this->maxTextWidth = intval($this->imageWidth * 0.8);
$this->maxTextHeight = intval($this->imageHeight * 0.6);

$this->fitTextToBox();
if ($this->textBoxHeight > $this->maxTextHeight) {
for ($countLines = count(explode("\n", $this->text)); $countLines > 1; $countLines--) {
$position = mb_strrpos($this->text, "\n") ?: null;
$this->text = mb_substr($this->text, 0, $position);

$textBoxSize = $this->getTextBoxSize($this->text, $this->font, $this->fontSize);
$this->textBoxHeight = $textBoxSize['height'];
if ($textBoxSize['height'] <= $this->maxTextHeight) {
break;
}
}
}
}

protected function setTextCoordinates(): void
{
$paddingX = intval(($this->imageWidth - $this->maxTextWidth) / 2);

switch (strtolower($this->textAlignment)) {
@@ -536,6 +531,31 @@ protected function setTextParameters(): void
$this->textStartY = intval(($this->imageHeight - $this->textBoxHeight) / 2);
}

/**
* @throws ImagickException
* @throws ImagickDrawException
*/
protected function setAppNameParameters(): void
{
$this->setAppNameBox();
$this->setAppNameCoordinates();

switch ($this->appNameDecorationStyle) {
case 'line':
$this->setLineCoordinates();

break;
case 'label':
$this->setLabelCoordinates();

break;
case 'rectangle':
$this->setRectangleCoordinates();

break;
}
}

/**
* @throws ImagickException
* @throws ImagickDrawException

0 comments on commit b9d3620

Please sign in to comment.