note 93593 added to function.imagefttext
| From: | KeepSake at crimebloc dot com | Date: | Thu, 17 Sep 2009 16:05:22 +0000 |
| Subject: | note 93593 added to function.imagefttext | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160795@lists.php.net to get a copy of this message | ||
For a design project I am required to have spacing between characters; since imagefttext does not
support this feature I have created a function which does support this.
The arguments are identical to imagefttext, accept that (array)$extrainfo now accepts the
'character_spacing' spacing parameter. The return values are as expected, and include the
image boundaries of the entire string including the character spacing.
The downside is that $angle rotates each letter instead of rotating the entire word (could be seen
as a feature on its own).
I hope this is of some use to someone.
- KeepSake
<?php
// Required header (assuming we use png images)
header("Content-type: image/png");
// Create a basic image with a dark background.
$image = imagecreatetruecolor(300, 20);
imagefill($image, 0, 0, imagecolorallocate($image, 21, 21, 21));
// Function call, arguments are the same as imagefttext, expect that (array)$extrainfo takes a new
paramenter called character_spacing.
$imageBox = imagefttext2($image, 9, 0, 2, 15, imagecolorallocate($image, 255, 255, 255),
'tahomabold.ttf', 'The quick brown fox...', array('character_spacing'
=> 5));
// Output the file, and clear the resources
imagepng($image);
imagedestroy($image);
function imagefttext2($imageResource, $font_size, $text_angle, $start_x, $start_y, $color,
$font_file, $text, $extra_info = array()) {
if($extra_info['character_spacing'] == NULL ||
!is_numeric($extra_info['character_spacing'])) {
$extra_info['character_spacing'] = 0;
}
$lastX = $start_x - $extra_info['character_spacing'];
foreach(str_split($text) as $v) {
$coordinates = imagefttext($imageResource, $font_size, $text_angle, $lastX +
$extra_info['character_spacing'], $start_y, $color, $font_file, $v, $extra_info);
$lastX = max($coordinates[2], $coordinates[4]);
}
// Return the newly generated image box coordinates:
return array($start_x, $start_y, $coordinates[2], $coordinates[3], $coordinates[4],
$coordinates[5], $start_x, $coordinates[7]);
}
?>
----
Server IP: 87.124.86.41
Probable Submitter: 90.152.4.38 (proxied: 90.152.4.38)
----
Manual Page -- http://www.php.net/manual/en/function.imagefttext.php
Edit -- https://master.php.net/note/edit/93593
Del: integrated -- https://master.php.net/note/delete/93593/integrated
Del: useless -- https://master.php.net/note/delete/93593/useless
Del: bad code -- https://master.php.net/note/delete/93593/bad+code
Del: spam -- https://master.php.net/note/delete/93593/spam
Del: non-english -- https://master.php.net/note/delete/93593/non-english
Del: in docs -- https://master.php.net/note/delete/93593/in+docs
Del: other reasons-- https://master.php.net/note/delete/93593
Reject -- https://master.php.net/note/reject/93593
Search -- https://master.php.net/manage/user-notes.php