Skip to content

Commit

Permalink
Bug fixes in Text rendering
Browse files Browse the repository at this point in the history
Version bump
  • Loading branch information
Josh Lockhart committed Nov 20, 2013
1 parent d91ce85 commit 51f6442
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Install this library with [Composer](http://getcomposer.org). Add this to your `

{
"require": {
"nmcteam/image-with-text": "~2.0.0"
"nmcteam/image-with-text": "~2.0"
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/NMC/ImageWithText/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @copyright 2013 Josh Lockhart
* @link https://github.com/nmcteam/image-with-text
* @license MIT
* @version 2.0.0
* @version 2.0.2
*
* MIT LICENSE
*
Expand Down
54 changes: 28 additions & 26 deletions src/NMC/ImageWithText/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @copyright 2013 Josh Lockhart
* @link https://github.com/nmcteam/image-with-text
* @license MIT
* @version 2.0.0
* @version 2.0.2
*
* MIT LICENSE
*
Expand Down Expand Up @@ -187,32 +187,34 @@ public function renderToImage(\NMC\ImageWithText\Image $image)
// Calculate line offsets
for ($i = 0; $i < count($this->lines); $i++) {
// Fetch line
$line = $this->lines[$i];

// Calculate line width in pixels
$lineBoundingBox = imagettfbbox($this->size, 0, $this->font, $line['text']);
$lineWidth = abs($lineBoundingBox[0] - $lineBoundingBox[2]); // (lower left corner, X position) - (lower right corner, X position)

// Calculate line X,Y offsets in pixels
switch ($this->align) {
case 'left':
$offsetX = $this->startX;
$offsetY = $this->startY + $this->lineHeight + ($this->lineHeight * $i);
break;
case 'center':
$imageWidth = $image->getWidth();
$offsetX = (($maxLineWidth - $lineWidth) / 2) + $this->startX;
$offsetY = $this->startY + $this->lineHeight + ($this->lineHeight * $i);
break;
case 'right':
$imageWidth = $image->getWidth();
$offsetX = $imageWidth - $line['width'] - $this->startX;
$offsetY = $this->startY + $this->lineHeight + ($this->lineHeight * $i);
break;
}
if (array_key_exists($i, $this->lines)) {
$line =& $this->lines[$i];

// Calculate line width in pixels
$lineBoundingBox = imagettfbbox($this->size, 0, $this->font, $line['text']);
$lineWidth = abs($lineBoundingBox[0] - $lineBoundingBox[2]); // (lower left corner, X position) - (lower right corner, X position)

// Render text onto image
$image->getImage()->text($line['text'], $offsetX, $offsetY, $this->size, $this->color, 0, $this->font);
// Calculate line X,Y offsets in pixels
switch ($this->align) {
case 'left':
$offsetX = $this->startX;
$offsetY = $this->startY + $this->lineHeight + ($this->lineHeight * $i);
break;
case 'center':
$imageWidth = $image->getWidth();
$offsetX = (($maxLineWidth - $lineWidth) / 2) + $this->startX;
$offsetY = $this->startY + $this->lineHeight + ($this->lineHeight * $i);
break;
case 'right':
$imageWidth = $image->getWidth();
$offsetX = $imageWidth - $line['width'] - $this->startX;
$offsetY = $this->startY + $this->lineHeight + ($this->lineHeight * $i);
break;
}

// Render text onto image
$image->getImage()->text($line['text'], $offsetX, $offsetY, $this->size, $this->color, 0, $this->font);
}
}
}

Expand Down

0 comments on commit 51f6442

Please sign in to comment.