note 35144 added to function.imagestring
| From: | sk89q at yahoo dot com | Date: | Thu, 21 Aug 2003 05:09:23 +0000 |
| Subject: | note 35144 added to function.imagestring | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-54798@lists.php.net to get a copy of this message | ||
<?php
/*
---------------------------------------------------
resource imagestringbox ( resource image, int font,
int top, int left, int bottom, int right,
int align, int leading, string text, int color )
---------------------------------------------------
This function allows you to draw text inside a box.
It will automatically wrap at the end of the line.
It also supports cutting off text if it goes
outside the box. Align methods supported are left,
center, and right. Manual new lines can be made by
using \n (note that you can not escape that symbol
though). If you're wondering, leading is the space
between lines. An important note is that it will
RETURN the resource - it will not directly modify
the resource provided. An example is provided
below.
---------------------------------------------------
http://therisenrealm.vze.com :)
---------------------------------------------------
*/
define("ALIGN_LEFT", 0);
define("ALIGN_CENTER", 1);
define("ALIGN_RIGHT", 2);
function imagestringbox($image, $font, $top, $left, $bottom, $right, $align, $leading, $text,
$color) {
$height = $bottom - $top;
$width = $right - $left;
$line_count = floor($height / imagefontheight($font));
$lines = wordwrap($text, floor($width / imagefontwidth($font)), "\n", true);
$lines = explode("\n", $lines);
for ($i = 0; $i < $line_count; $i++) {
$y = $top + $i * (imagefontheight($font) + $leading);
$line_width = strlen($lines[$i]) * imagefontwidth($font);
if ($align == ALIGN_LEFT) {
imagestring($image, $font, $left, $y, $lines[$i], $color);
} elseif ($align == ALIGN_CENTER) {
$mid = $width - $line_width;
$mid = $mid / 2;
$left_align = $left + $mid;
imagestring($image, $font, $left_align, $y, $lines[$i], $color);
} elseif ($align == ALIGN_RIGHT) {
$mid = $width - $line_width;
$left_align = $left + $mid;
imagestring($image, $font, $left_align, $y, $lines[$i], $color);
} else {
imagestring($image, $font, $left, $y, "Align method not supported.", $color);
}
}
return $image;
}
// EXAMPLE
$test_string = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diem nonummy
nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat.\n\nUt wisis enim ad minim
veniam, quis nostrud exerci tution ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo
consequat. Duis te feugifacilisi. Ut wisi enim ad minim veniam, quis nostrud exerci taion
ullamcorper suscipit lobortis nisl ut aliquip ex en commodo consequat. Duis te feugifacilisi per
suscipit lobortis nisl ut aliquip ex en commodo consequat.Lorem ipsum dolor sit amet, consectetuer
adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat
volutpat. ";
$im = imagecreate(400, 400);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$im = imagestringbox($im, 3, 5, 10, 300, 207, ALIGN_CENTER, 2, $test_string, $black);
header("Content-type: image/png");
imagepng($im);
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagestring.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35144
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35144&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35144&report=yes