note 94306 modified in function.imagestring by danbrown
| From: | danbrown@php.net | Date: | Wed, 28 Oct 2009 16:05:15 +0000 |
| Subject: | note 94306 modified in function.imagestring by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162187@lists.php.net to get a copy of this message | ||
I created this very simple function to place text in the horizontal center of an image.
image = the image.
image_width = the width of the image.
string = the text you want to add.
font_size = font size, between 1 and 5.
y = vertical position of the text.
color = the color of the text
<?php
//place text in the center of the image
function CenterImageString($image, $image_width, $string, $font_size, $y, $color)
{
$text_width = imagefontwidth($font_size)*strlen($string);
$center = ceil($image_width / 2);
$x = $center - (ceil($text_width/2));
ImageString($image, $font_size, $x, $y, $string, $color);
}
?>
To use the function in your script, you can do like this:
<?php
header("Content-type: image/png");
$picture = imagecreate(100,100);
$black = ImageColorAllocate ($picture, 0, 0, 0);
CenterImageString($picture, 100, "Centered text", 2, 45, $black);
ImagePng ($picture);
?>
--was--
I created this very simple function to place text in the horizontal center of an image.
image = the image.
image_width = the width of the image.
string = the text you want to add.
font_size = font size, between 1 and 5.
y = vertical position of the text.
color = the color of the text
<?
//place text in the center of the image
function CenterImageString($image, $image_width, $string, $font_size, $y, $color)
{
$text_width = imagefontwidth($font_size)*strlen($string);
$center = ceil($image_width / 2);
$x = $center - (ceil($text_width/2));
ImageString($image, $font_size, $x, $y, $string, $color);
}
?>
To use the function in your script, you can do like this:
<?
header("Content-type: image/png");
$picture = imagecreate(100,100);
$black = ImageColorAllocate ($picture, 0, 0, 0);
CenterImageString($picture, 100, "Centered text", 2, 45, $black);
ImagePng ($picture);
?>
http://php.net/manual/en/function.imagestring.php