note 26508 added to function.imagestring
| From: | alexnow at rack1 dot php dot net | Date: | Fri, 01 Nov 2002 22:53:04 +0000 |
| Subject: | note 26508 added to function.imagestring | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-38891@lists.php.net to get a copy of this message | ||
Just my little addition to the ImageStringAlignAndWrap() function.
This "version" lets you align vertically too: "bottom" , "center" and
the default: "top".
It can also display a shadow of the text.
If you don't want a shadow simply set $drop to '0'.
The last parameter $padding is optional, it sets a margin between the edge of the image and the
text.
I hope it helps!
- Alex http://alexnow.com
------------------------------------------
if(!function_exists('ImageStringAlignAndWrap')){ // prevent function redeclare
function ImageStringAlignAndWrap($image, $font, $text, $color, $shadow, $drop, $maxwidth,
$alignment, $valign, $padding="5")
{
$fontwidth = ImageFontWidth($font);
$fontheight = ImageFontHeight($font);
$margin = floor($padding + $drop)/2; // So that shadow is not off image on right align &
bottom valign
if ($maxwidth != NULL) {
$maxcharsperline = floor( ($maxwidth - ($margin * 2)) / $fontwidth);
$text = wordwrap($text, $maxcharsperline, "\n", 1);
}
$lines = explode("\n", $text);
switch($valign){
case "center":
$y = (imageSY($image) - ($fontheight * sizeof($lines)))/2;
break;
case "bottom":
$y = imageSY($image) - (($fontheight * sizeof($lines)) + $margin);
break;
default:
$y = $margin;
break;
}
switch($alignment){
case "right":
while (list($numl, $line) = each($lines)) {
ImageString($image, $font, (imagesx($image) - $fontwidth*strlen($line))-$margin+$drop,
($y+$drop), $line, $shadow);
ImageString($image, $font, (imagesx($image) - $fontwidth*strlen($line))-$margin, $y,
$line, $color);
$y += $fontheight;
}
break;
case "center":
while (list($numl, $line) = each($lines)) {
ImageString($image, $font, floor((imagesx($image) - $fontwidth*strlen($line))/2)+$drop,
($y+$drop), $line, $shadow);
ImageString($image, $font, floor((imagesx($image) - $fontwidth*strlen($line))/2), $y,
$line, $color);
$y += $fontheight;
}
break;
default:
while (list($numl, $line) = each($lines)) {
ImageString($image, $font, $margin+$drop, ($y+$drop), $line, $shadow);
ImageString($image, $font, $margin, $y, $line, $color);
$y += $fontheight;
}
break;
}
} // end function
} // end function_exists
------------------------------------------
Execute with something like:
ImageStringAlignAndWrap($im, $font, $string, $white, $black, 3, $imageWidth, "left",
"top","7");
or
ImageStringAlignAndWrap($im, $font, $string, $white, $black, 3, $imageWidth, "center",
"center");
--
http://www.php.net/manual/en/function.imagestring.php
http://master.php.net/manage/user-notes.php?action=edit+26508
http://master.php.net/manage/user-notes.php?action=delete+26508
http://master.php.net/manage/user-notes.php?action=reject+26508