imagestring() is useful for compact labels using GD built-in fonts. For scalable TrueType fonts, use imagettftext().
<?php
$image = imagecreatetruecolor(420, 120);
$background = imagecolorallocate($image, 250, 250, 250);
$text = imagecolorallocate($image, 20, 80, 160);
imagefilledrectangle($image, 0, 0, 419, 119, $background);
imagestring($image, 5, 20, 45, 'plus2net PHP GD', $text);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>Current example first; the original tutorial examples below are retained and cleaned up for continued learning.
<?php
header ("Content-Type: image/jpeg");
///// Create the image ////////
$im = @ImageCreate (100,40)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 204, 204, 204); // Assign background color
$text_color = ImageColorAllocate ($im, 51, 51, 255); // text color is given
ImageString($im,5,14,0,'PLUS2NET',$text_color); // Text added
ImageJpeg ($im); // image displayed
imagedestroy($im); // Memory allocation for the image is removed.
?>
Output is here
imagestring($source_image,$int_font,$int_x,$int_y,$text,$int_color)
$source_image : Image resource created before$int_font: Values from 1 to 5 for built in fonts $int_x: Position of font in Horizontal X axis, Leftmost point is 0 $int_y: Position of the font in Vertical Y Axis, Topmost point is 0$text: Text string to be written on the image $int_color : Color created before using imagecolorallocate().<img src=gd-imagestring-demo.php>
ImageString($im,5,14,0,'PLUS2NET',$text_color);
ImageString($im,5,0,0,'PLUS2NET',$text_color);
$font_height=imagefontheight(5);
$font_width=imagefontwidth(5);
We will use above functions to know height and width of the fonts used. Based on this value we can allign Text to Right or bottom of the image.
<?php
header ("Content-Type: image/jpeg");
$height=40; // Canvas Height
$width=100; // Canvas Width
$font_width=imagefontwidth(5); // Width of each char used in text string
$text="PLUS2NET"; // Text string to be written on Image.
$no_text=strlen($text); // Number of chars in our text string.
$text_width=$no_text * $font_width; // Width of text string.
$x=$width-$text_width; // X - Postion of text string to allign Right.
///// Create the image ////////
$im = @ImageCreate ($width,$height)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 204, 204, 204); // Assign background color
$text_color = ImageColorAllocate ($im, 51, 51, 255); // text color is given
ImageString($im,5,$x,10,$text,$text_color); // Random string from session added
ImageJpeg ($im); // image displayed
imagedestroy($im); // Memory allocation for the image is removed.
?>
<?php
header ("Content-Type: image/jpeg");
$height=40; // Canvas Height
$width=100; // Canvas Width
$font_height=imagefontheight(5);
$font_width=imagefontwidth(5);
$text="PLUS2NET"; // Text to be written on Image.
$no_text=strlen($text); // Number of chars in our text string.
$text_width=$no_text * $font_width; // Width of text string.
$x=$width-$text_width; // X - Postion of text string to allign Right.
$y=$height-$font_height; // Y- Postion of text string to allign bottom.
///// Create the image ////////
$im = @ImageCreate ($width,$height)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 204, 204, 204); // Assign background color
$text_color = ImageColorAllocate ($im, 51, 51, 255); // text color is given
ImageString($im,5,$x,$y,$text,$text_color); // Random string from session added
ImageJpeg ($im); // image displayed
imagedestroy($im); // Memory allocation for the image is removed.
?>
<?php
header ("Content-Type: image/jpeg");
///// Create the image ////////
$font_height=imagefontheight(5);
$font_width=imagefontwidth(5);
$text='PLUS2NET.COM'; // Text string can be changed to reflect width of canvas to change.
$width = (strlen($text)*$font_width)+10 ; // Canvas width = String with, added with some margin
$height=$font_height + 10; // Canvas height = String height, added with some margin
$im = @ImageCreate ($width,$height)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 204, 204, 204); // Assign background color
$text_color = ImageColorAllocate ($im, 51, 51, 255); // text color is given
ImageString($im,5,5,$height/3,$text,$text_color); // Random string from session added
ImageJpeg ($im); // image displayed
imagedestroy($im); // Memory allocation for the image is removed.
?>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.