note 25840 deleted from function.imagestring by aidan
| From: | aidan@php.net | Date: | Thu, 26 Aug 2004 06:27:12 +0000 |
| Subject: | note 25840 deleted from function.imagestring by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75285@lists.php.net to get a copy of this message | ||
Note Submitter: bu at hit dot bg
----
Inspired by the ImageStringWrap function I wrote a similar one that wraps the text too but also
aligns it to the left, right or the center of the image. Here is the source of this function:
function ImageStringAlignAndWrap($image, $font, $text, $color, $maxwidth, $alignment)
{
$fontwidth = ImageFontWidth($font);
$fontheight = ImageFontHeight($font);
if ($maxwidth != NULL) {
$maxcharsperline = floor($maxwidth / $fontwidth);
$text = wordwrap($text, $maxcharsperline, "\n", 1);
}
$lines = explode("\n", $text);
$y = 0; //starting vertical position. this can also be made a parameter of the function if you
neet a top margin
if ($alignment == "right") {
while (list($numl, $line) = each($lines)) {
ImageString($image, $font, imagesx($image) - $fontwidth*strlen($line), $y, $line,
$color);
$y += $fontheight;
}
} elseif ($alignment == "center") {
while (list($numl, $line) = each($lines)) {
ImageString($image, $font, floor((imagesx($image) - $fontwidth*strlen($line))/2), $y,
$line, $color);
$y += $fontheight;
}
} else {
while (list($numl, $line) = each($lines)) {
ImageString($image, $font, 0, $y, $line, $color);
$y += $fontheight;
}
}
}
You may see a working demo of this idea on the following address: http://maquett.com/bu/demo/image.php
There I've added some functionallity - the user controls the background and text color, the
alignment, the size of the font and the size of the image.
Check it out :)
P.S.: Any comments will be appreciated.