imagestring DRAW a STRING horizontally in an image.
This function considers upper-left at $x = 0, $y = 0.
1 ≤ $font ≤ 5 for built-in fonts in latin2 encoding (where higher numbers corresponding to larger fonts) or any of your own font identifiers registered with imageloadfont.
TTF fonts are allowed by this function.
This function returns TRUE on success or FALSE on failure.
<?php
bool imagestring ( GdImage $image,
GdFont|int $font,
int $x,
int $y,
string $string,
int $color )
where,
$image = The image identifier
$font = The font to be used
$x = The x-coodinate to start in the image
$y = The y-coodinate to start in the image
$string = The STRING to written
$color = The color to be used
?>
$image
The image identifier.
$font
The font to be used.
$x
The x-coodinate to start in the image.
$y
The y-coodinate to start in the image.
$string
The string to be horizontally written.
$color
The color to be used to draw the character created by imagecolorallocate.
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$gdf_font_name = "gdf/04b.gdf";
$font_size = 16;
$angle_txt = 0;
$pos_x = 20;
$pos_y = 20;
$txt = '+ Alea jacta est!';
$gdffile01 = 'png/PNG 013 01.png';
$im01 = imagecreate(400, 60);
$bkg_color = imagecolorallocate($im01, 192, 192, 192);
$txt_color = imagecolorallocate($im01, 80, 0, 0);
$gdf_fnt = imageloadfont($gdf_font_name);
imagestring($im01, $gdf_fnt, $pos_x, $pos_y,
$txt, $txt_color);
imagepng($im01, $gdffile01);
imagedestroy($im01);
?>
<?php echo $gdffile01; ?><br><br>
<img src="<?php echo $gdffile01; ?>"
alt="<?php echo $gdffile01; ?>"
title="<?php echo $gdffile01; ?>">
RESULT PNG 013 01.png+ Alea jacta est! was written with the font 04b.gdf.
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$ttf_font_name = "ttf/VeraSeBd.ttf";
$font_size = 16;
$angle_txt = 0;
$pos_x = 20;
$pos_y = 20;
$txt = '+ Alea jacta est!';
$ttffile02 = 'png/PNG 014 02.png';
$im02 = imagecreate(150, 60);
$bkg_color = imagecolorallocate($im02, 255, 0, 72);
$txt_color = imagecolorallocate($im02, 255, 255, 255);
$ttf_fnt = $ttf_font_name;
imagestring($im02, 2, $pos_x, $pos_y, $txt, $txt_color);
imagepng($im02, $ttffile02);
imagedestroy($im02);
?>
<?php echo $ttffile02; ?><br><br>
<img src="<?php echo $ttffile02; ?>"
alt="<?php echo $ttffile02; ?>"
title="<?php echo $ttffile02; ?>">
RESULT PNG 014 02.png+ Alea jacta est!.
When using a ttf font, no matter which one is used, the result will look similar to the one displayed in this exercise.
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$ttf_font_name = "ttf/Vera.ttf";
$font_size = 32;
$angle_txt = 0;
$pos_x = 20;
$pos_y = 20;
$txt = '+ Alea jacta est!';
$ttffile03 = 'png/PNG 015 03.png';
$im03 = imagecreatetruecolor(200, 60);
$bkg_color = imagecolorallocate($im03, 255, 192, 72);
$txt_color = imagecolorallocate($im03, 255, 0, 255);
$ttf_fnt = $ttf_font_name;
imagestring($im03, $font_size, $pos_x, $pos_y,
$txt, $txt_color);
imagepng($im03, $ttffile03);
imagedestroy($im03);
?>
<?php echo $ttffile03; ?><br><br>
<img src="<?php echo $ttffile03; ?>"
alt="<?php echo $ttffile03; ?>"
title="<?php echo $ttffile03; ?>">
RESULT PNG 015 03.png+ Alea jacta est!.
When using a ttf font, no matter which one is used, the result will look similar to the one displayed in this exercise.
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$ttf_font_name = "ttf/Vera.ttf";
$font_size = 32;
$angle_txt = 0;
$pos_x = 20;
$pos_y = 20;
$txt = '+ Alea jacta est!';
$ttffile04 = 'gif/GIF 008 04.gif';
$im04 = imagecreate(200, 60);
$bkg_color = imagecolorallocate($im04, 255, 192, 72);
$txt_color = imagecolorallocate($im04, 0, 0, 0);
$ttf_fnt = $ttf_font_name;
imagestring($im04, $font_size, $pos_x, $pos_y,
$txt, $txt_color);
imagegif($im04, $ttffile04);
imagedestroy($im04);
?>
<?php echo $ttffile04; ?><br><br>
<img src="<?php echo $ttffile04; ?>"
alt="<?php echo $ttffile04; ?>"
title="<?php echo $ttffile04; ?>">
RESULT GIF 008 04.gif+ Alea jacta est!.
When using a ttf font, no matter which one is used, the result will look similar to the one displayed in this exercise.
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$ttf_font_name = "ttf/Vera.ttf";
$font_size = 32;
$angle_txt = 0;
$pos_x = 20;
$pos_y = 20;
$txt = '+ Alea jacta est!';
$ttffile05 = 'gif/GIF 008 05.gif';
$im05 = imagecreate(200, 60);
$red = mt_rand(0, 255);
$green = mt_rand(0, 255);
$blue = mt_rand(0, 255);
$bkg_color = imagecolorallocate($im05, $red, $green, $blue);
$txt_color = imagecolorallocate($im05, 255, 255, 255);
$ttf_fnt = $ttf_font_name;
imagestring($im05, $font_size, $pos_x, $pos_y, $txt, $txt_color);
imagegif($im05, $ttffile05);
imagedestroy($im05);
?>
<?php echo $ttffile05; ?><br><br>
<img src="<?php echo $ttffile05; ?>"
alt="<?php echo $ttffile05; ?>"
title="<?php echo $ttffile05; ?>">
RESULT GIF 008 05.gif+ Alea jacta est!
This is just one of the possible results for this exercise.
EXERCISE
<?php
// Try this code
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$var = getcwd();
define('PBWP', 'file://' . $var . '/bmp/');
echo PBWP . '<br><br>';
// STREAM WRAPPER MODE
$im06 = imagecreatetruecolor (200, 100);
$yellow = imagecolorallocate($im06, 255, 255, 0);
$green = imagecolorallocate($im06, 0, 127, 0);
$img06 = PBWP . 'BMP 0029 06.bmp';
$font06 = 6;
$x06 = 20;
$y06 = 45;
$cormpressed06 = FALSE;
$txt06 = "This is a test!";
imagestring($im06, $font06, $x06, $y06, $txt06, $yellow);
$bool06 = imagebmp($im06, $img06, $cormpressed06);
if ($bool06 == TRUE)
{
$mess06 = 'The image was created!';
echo '$bool06 = ';
var_dump($bool06);
echo '<br><br>' . $mess06 . '<br><br>';
$img06a = 'bmp/BMP 0029 06.bmp';
// NORMAL MODE
echo '<img src="' . $img06a .
'" alt="' . $img06a . '" title="' . $img06a . '">';
imagedestroy($im06);
}
else
{
$mess06 = 'The image COULD NOT BE CREATED!';
echo $mess06;
}
?>
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$im07 = imagecreate (200, 200);
$blue = imagecolorallocate($im07, 0, 0, 8);
$green = imagecolorallocate($im07, 0, 127, 0);
$img_file_name = 'gd/GD 0029 07.gd';
$font07 = 1;
$x07 = 20;
$y07 = 100;
imagestring($im07, $font07, $x07, $y07,
"This is a test", $green);
$bool07 = imagegd($im07, $img_file_name);
if ($bool07 == TRUE)
{
$mess07 = 'The image was created, however,
<br>CAN NOT BE DISPLAYED by this browser!';
echo '$bool07 = ';
var_dump($bool07);
echo '<br><br>' . $mess07;
imagedestroy($im07);
}
else
{
$mess07 = 'The image COULD NOT BE CREATED!';
echo $mess07;
}
?>
EXERCISE
<?php
// Try this code
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$im08 = imagecreatetruecolor (200, 200);
$blue = imagecolorallocate($im08, 0, 0, 8);
$green = imagecolorallocate($im08, 0, 127, 0);
$img_file_name = 'gd2/GD2 0029 08.gd2';
$font08 = 1;
$x08 = 20;
$y08 = 100;
$chunk_size08 = 108;
$txt08 = "One more test.";
$type08 = IMG_GD2_COMPRESSED;
imagestring($im08, $font08, $x08, $y08, $txt08, $green);
$bool08 = imagegd2($im08, $img_file_name, $chunk_size08, $type08);
if ($bool08 == TRUE)
{
$mess08 = 'The image was created, however,
<br>CAN NOT BE DISPLAYED by this browser!';
echo '$bool08 = ';
var_dump($bool08);
echo '<br><br>' . $mess08;
imagedestroy($im08);
}
else
{
$mess08 = 'The image COULD NOT BE CREATED!';
echo $mess08;
}
?>
EXERCISE
<?php
// Try this code
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$var = getcwd();
define ('PTW', 'file://' . $var . '/webp/');
echo PTW . '<br><br>';
$im09 = imagecreatetruecolor (200, 100);
$blue = imagecolorallocate($im09, 0, 0, 8);
$green = imagecolorallocate($im09, 0, 255, 0);
$img_file_stw = PTW . 'WEBP 0029 09.webp';
// STREAM WRAPPER MODE
$img_file_nor = 'webp/WEBP 0029 09.webp';
// NORMAL MODE
$font09 = 5;
$x09 = 20;
$y09 = 45;
$quality09 = 60;
$txt09 = "This is a test";
imagestring($im09, $font09, $x09, $y09, $txt09, $green);
$bool09 = imagewebp($im09, $img_file_stw, $quality09);
if ($bool09 == TRUE)
{
$mess09 = 'The image was Created!';
echo '$bool09 = ';
var_dump($bool09);
echo '<br><br>' . $mess09;
echo '<br><br><img src="' . $img_file_nor .
'" alt="' . $img_file_nor . '" title="' . $img_file_nor . '">';
imagedestroy($im09);
}
else
{
$mess09 = 'The image COULD NOT BE CREATED!';
echo $mess09;
}
?>
EXERCISE
<?php
// Try this code
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$im10 = imagecreate(240, 100);
$foreground10 =
imagecolorallocate($im10, 195, 226, 245);
$txtimgxbm10 = "A B R A C A D A B R A";
$f10 = 5;
$x10 = 20;
$y10 = 45;
$txtcolor10 = imagecolorallocate($im10, 0, 0, 0);
imagestring ($im10, $f10, $x10, $y10, $txtimgxbm10, $txtcolor10 );
$imgxbm10 = 'xbm/XBM 0029 10.xbm';
$bool10 = imagexbm($im10, $imgxbm10);
if($bool10 == TRUE)
{
var_dump($bool10);
echo '<br><br>The image has been created,
but can not be displayed!<br>';
}
else
{
echo 'The image was not created!';
}
imagedestroy($im10);
?>
EXERCISE
<?php
// Try this code
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$var = getcwd();
define('PXBM', 'file://' . $var . '/xbm/');
echo PXBM . '<br><br>';
// STREAM WRAPPER MODE
$im11 = imagecreatetruecolor(240, 100);
$foreground11 = imagecolorallocate($im11, 195, 226, 245);
$txtimgxbm11 = "A B R A C A D A B R A";
$f11 = 5;
$x11 = 20;
$y11 = 45;
$txtcolor11 = imagecolorallocate($im11, 0, 0, 0);
imagestring ($im11, $f11, $x11, $y11, $txtimgxbm11, $txtcolor11 );
$imgxbm11 = PXBM . 'XBM 0029 11.xbm';
$bool11 = imagexbm($im11, $imgxbm11);
if($bool11 == TRUE)
{
var_dump($bool11);
echo '<br><br>The image has been created,
but can not be displayed!<br>';
}
else
{
echo 'The image was not created!';
}
imagedestroy($im11);
?>