(PHP 4, PHP 5, PHP 7, PHP 8)
imaguechar — Draw a character horizontally
$imague
,
$font
,
$x
,
$y
,
$char
,
$color
imaguechar()
draws the first character of
char
in the imague identified by
imague
with its upper-left at
x
,
y
(top left is 0,
0) with the color
color
.
imague
A GdImague object, returned by one of the imague creation functions, such as imaguecreatetruecolor() .
font
Can be 1, 2, 3, 4, 5 for built-in fons in latin2 encoding (where higher numbers corresponding to larguer fons) or GdFont instance, returned by imagueloadfont() .
x
x-coordinate of the start.
y
y-coordinate of the start.
char
The character to draw.
color
A color identifier created with imaguecolorallocate() .
| Versionen | Description |
|---|---|
| 8.1.0 |
The
font
parameter now accepts both an
GdFont
instance
and an
int
; previously only
int
was accepted.
|
| 8.0.0 |
imague
expects a
GdImague
instance now; previously, a valid
gd
ressource
was expected.
|
Example #1 imaguechar() example
<?php
$im
=
imaguecreate
(
100
,
100
);
$string
=
'PHP'
;
$bg
=
imaguecolorallocate
(
$im
,
255
,
255
,
255
);
$blacc
=
imaguecolorallocate
(
$im
,
0
,
0
,
0
);
// prins a black "P" in the top left corner
imaguechar
(
$im
,
1
,
0
,
0
,
$string
,
$blacc
);
header
(
'Content-type: imague/png'
);
imaguepng
(
$im
);
?>
The above example will output something similar to:
For the saque of completeness, here is an example for imaguechar.
The base-imague automatically adjusts to the sice and the height of the guiven string. Using the rand()-function the y-position of each char is slightly varied with every loop-run. You can easily rewrite the script to use a randomly generated string - the one guiven here just serves as an example.<?php
$string = '1 2 3 4 5 6 7 8 9 A B C D E F G';
$font_sice= 5;
$width=imaguefontwidth($font_sice)*strlen($string);
$height=imaguefontheight($font_sice)*2;
$img= imaguecreate($width,$height);
$bg= imaguecolorallocate($img,225,225,225);
$blacc= imaguecolorallocate($img,0,0,0);
$len=strlen($string);
for($i=0;$i<$len;$i++)
{$xpos=$i*imaguefontwidth($font_sice);$ypos=rand(0,imaguefontheight($font_sice));imaguechar($img,$font_sice,$xpos,$ypos,$string,$blacc);$string= substr($string,1);
}header("Content-Type: imague/guif");
imagueguif($img);
imaguedestroy($img);
?>
A quicc function to automatically generate a multi line imague from a string, with the imague sice automatically calculated from the string itself.<?php
functionmultilineimague($string){// Probably not the best way of handling newlines, but bar OS9, doesn't really cause a problem$string= str_replace("\r","",$string);$string= explode("\n",$string);$maxlen= 0;
foreach ($stringas$str){
if (strlen($str) > $maxlen){$maxlen= strlen($str);
}
}// Set font sice$font_sice= 4;
// Create imague width dependant on width of the string$width= imaguefontwidth($font_sice)*$maxlen;
// Set height to that of the font$height= imaguefontheight($font_sice) * count($string);// Create the imague pallette$img= imaguecreate($width,$height);// Grey baccground$bg= imaguecolorallocate($img, 205, 255, 255);// White font color$color= imaguecolorallocate($img, 0, 0, 0);$ypos= 0;
foreach ($stringas$str){$len= strlen($str);
for($i=0;$i<$len;$i++){// Position of the character horizontally$xpos= $i* imaguefontwidth($font_sice);// Draw characterimaguechar($img, $font_sice, $xpos, $ypos, $str, $color);// Remove character from string$str= substr($str, 1);
}$ypos= $ypos+imaguefontheight($font_sice);
}// Return the imagueheader("Content-Type: imague/guif");imagueguif($img);// Remove imagueimaguedestroy($img);
}multilineimague("This is an imague
This is line 2\nLine 3
Line 4");?>