note 35170 deleted from function.imagestring by aidan
| From: | aidan@php.net | Date: | Thu, 26 Aug 2004 06:26:42 +0000 |
| Subject: | note 35170 deleted from function.imagestring by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75281@lists.php.net to get a copy of this message | ||
Note Submitter: sk89q at yahoo dot com
----
[NEW VERSION] Fixed bottom line irregularness (didn't take into effect leading); Vertical
alignment added (inspiration by those people below)
resource imagestringbox ( resource image, int font, int left, int top, int right, int bottom, int
align, int valign, 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 and cut the text if it is further than the bottom line. The text can be aligned vertically and
horizontally. Manual new lines can be made by using \n (note that you can not escape that symbol
though). The space between lines can also be set using leading (negative values are accepted).
Remember to visit my site: http://therisenrealm.vze.com
:)
<?php
// ### CODE ###
define("ALIGN_LEFT", 0);
define("ALIGN_CENTER", 1);
define("ALIGN_RIGHT", 2);
define("VALIGN_TOP", 0);
define("VALIGN_MIDDLE", 1);
define("VALIGN_BOTTOM", 2);
function imagestringbox(&$image, $font, $left, $top, $right, $bottom, $align, $valign,
$leading, $text, $color) {
// Get size of box
$height = $bottom - $top;
$width = $right - $left;
// Break the text into lines, and into an array
$lines = wordwrap($text, floor($width / imagefontwidth($font)), "\n", true);
$lines = explode("\n", $lines);
// Other important numbers
$line_height = imagefontheight($font) + $leading;
$line_count = floor($height / $line_height);
$line_count = ($line_count > count($lines)) ? (count($lines)) : ($line_count);
// Loop through lines
for ($i = 0; $i < $line_count; $i++) {
// Vertical Align
switch ($valign) {
case VALIGN_TOP: // Top
$y = $top + ($i * $line_height);
break;
case VALIGN_MIDDLE: // Middle
$y = $top + (($height - ($line_count * $line_height)) / 2) + ($i * $line_height);
break;
case VALIGN_BOTTOM: // Bottom
$y = ($top + $height) - ($line_count * $line_height) + ($i * $line_height);
break;
default:
return false;
}
// Horizontal Align
$line_width = strlen($lines[$i]) * imagefontwidth($font);
switch ($align) {
case ALIGN_LEFT: // Left
$x = $left;
break;
case ALIGN_CENTER: // Center
$x = $left + (($width - $line_width) / 2);
break;
case ALIGN_RIGHT: // Right
$x = $left + ($width - $line_width);
break;
default:
return false;
}
// Draw
imagestring($image, $font, $x, $y, $lines[$i], $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. t wisis enim ad minim veniam,
quis nostrud exerci tution ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo
consequat."; // Example text
$im = imagecreate(400, 400); // Create image
// Colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$grey = imagecolorallocate($im, 200, 200, 200);
// Grid so you can see
imagerectangle($im, 10, 6, 206, 300, $grey);
imagerectangle($im, 10, 6, 206, 153, $grey);
imagerectangle($im, 10, 6, 108, 300, $grey);
// Use it!
imagestringbox($im, 3, 10, 6, 206, 300, ALIGN_RIGHT, VALIGN_BOTTOM, 2, $test_string, $black);
// Output
header("Content-type: image/png");
imagepng($im);
?>