Skip to content

Commit

Permalink
Merge pull request #1 from mikedidomizio/#83
Browse files Browse the repository at this point in the history
#83 Added the ability to right align text
  • Loading branch information
mikedidomizio committed Oct 12, 2015
2 parents feb0685 + 5359a60 commit ab141bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@
// Text with stroke
$img->load('butterfly.jpg')->text('Butterfly', __DIR__.'/delicious.ttf', 32, '#FFFFFF', 'bottom', 0, -20, '#000', 2)->save('processed/butterfly-text-with-stroke.jpg');

// Right align text
$paragraph = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
$img->load('butterfly.jpg')->text($paragraph, __DIR__.'/delicious.ttf', 32, '#FFFFFF', 'center', 0, 1, null, null, 'right')->save('processed/butterfly-right-align-text.jpg');

// Resizing GIFs with transparency
$img->load('basketball.gif')->resize(50, 50)->save('processed/basketball-resize.gif');

Expand Down
12 changes: 11 additions & 1 deletion src/abeautifulsite/SimpleImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ function smooth($level) {
* @throws Exception
*
*/
function text($text, $font_file, $font_size = 12, $color = '#000000', $position = 'center', $x_offset = 0, $y_offset = 0, $stroke_color = null, $stroke_size = null) {
function text($text, $font_file, $font_size = 12, $color = '#000000', $position = 'center', $x_offset = 0, $y_offset = 0, $stroke_color = null, $stroke_size = null, $alignment = null) {

// todo - this method could be improved to support the text angle
$angle = 0;
Expand Down Expand Up @@ -1018,6 +1018,16 @@ function text($text, $font_file, $font_size = 12, $color = '#000000', $position
break;
}

if($alignment === "left") {
// Left aligned text
$x = -($x * 2);
} else if($alignment === "right") {
// Right aligned text
$dimensions = imagettfbbox($font_size, $angle, $font_file, $text);
$alignment_offset = abs($dimensions[4] - $dimensions[0]);
$x = -(($x * 2) + $alignment_offset);
}

// Add the text
imagesavealpha($this->image, true);
imagealphablending($this->image, true);
Expand Down

0 comments on commit ab141bd

Please sign in to comment.