note 25840 added to function.imagestring

From: Date: Tue, 08 Oct 2002 15:41:33 +0000
Subject: note 25840 added to function.imagestring
Groups: php.notes 
Request: Send a blank email to php-notes+get-37791@lists.php.net to get a copy of this message
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. -- http://www.php.net/manual/en/function.imagestring.php http://master.php.net/manage/user-notes.php?action=edit+25840 http://master.php.net/manage/user-notes.php?action=delete+25840 http://master.php.net/manage/user-notes.php?action=reject+25840

« previous php.notes (#37791) next »