Skip to content

Commit

Permalink
Added support for text stroke in text() method
Browse files Browse the repository at this point in the history
  • Loading branch information
claviska committed Aug 8, 2015
1 parent 0e69996 commit 09a37c0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@
// Text
$img->load('butterfly.jpg')->text('Butterfly', __DIR__.'/delicious.ttf', 32, '#FFFFFF', 'bottom', 0, -20)->save('processed/butterfly-text.jpg');

// 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');

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

Expand Down
38 changes: 36 additions & 2 deletions 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) {
function text($text, $font_file, $font_size = 12, $color = '#000000', $position = 'center', $x_offset = 0, $y_offset = 0, $stroke_color = null, $stroke_size = null) {

// todo - this method could be improved to support the text angle
$angle = 0;
Expand Down Expand Up @@ -1021,7 +1021,15 @@ function text($text, $font_file, $font_size = 12, $color = '#000000', $position
// Add the text
imagesavealpha($this->image, true);
imagealphablending($this->image, true);
imagettftext($this->image, $font_size, $angle, $x, $y, $color, $font_file, $text);
if( isset($stroke_color) && isset($stroke_size) ) {
// Text with stroke
$rgba = $this->normalize_color($color);
$stroke_color = imagecolorallocatealpha($this->image, $rgba['r'], $rgba['g'], $rgba['b'], $rgba['a']);
$this->imagettfstroketext($this->image, $font_size, $angle, $x, $y, $color, $stroke_color, $stroke_size, $font_file, $text);
} else {
// Text without stroke
imagettftext($this->image, $font_size, $angle, $x, $y, $color, $font_file, $text);
}

return $this;

Expand Down Expand Up @@ -1199,6 +1207,32 @@ protected function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x

}

/**
* Same as imagettftext(), but allows for a stroke color and size
*
* @param object &$image A GD image object
* @param float $size The font size
* @param float $angle The angle in degrees
* @param int $x X-coordinate of the starting position
* @param int $y Y-coordinate of the starting position
* @param int &$textcolor The color index of the text
* @param int &$stroke_color The color index of the stroke
* @param int $stroke_size The stroke size in pixels
* @param string $fontfile The path to the font to use
* @param string $text The text to output
*
* @return array This method has the same return values as imagettftext()
*
*/
protected function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $stroke_size, $fontfile, $text) {
for( $c1 = ($x - abs($stroke_size)); $c1 <= ($x + abs($stroke_size)); $c1++ ) {
for($c2 = ($y - abs($stroke_size)); $c2 <= ($y + abs($stroke_size)); $c2++) {
$bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);
}
}
return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text);
}

/**
* Ensures $value is always within $min and $max range.
*
Expand Down

0 comments on commit 09a37c0

Please sign in to comment.