diff --git a/example/index.php b/example/index.php index a16d910..1c4e7b1 100644 --- a/example/index.php +++ b/example/index.php @@ -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', 'top right', 0, 0, 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'); diff --git a/src/abeautifulsite/SimpleImage.php b/src/abeautifulsite/SimpleImage.php index 9ba3300..a71be2a 100644 --- a/src/abeautifulsite/SimpleImage.php +++ b/src/abeautifulsite/SimpleImage.php @@ -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; @@ -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);