imagettftext() needs a readable TTF font file. Build the font path from __DIR__ so the script is not dependent on the current working directory.
<?php
$image = imagecreatetruecolor(600, 160);
$background = imagecolorallocate($image, 255, 255, 255);
$text = imagecolorallocate($image, 25, 70, 140);
imagefilledrectangle($image, 0, 0, 599, 159, $background);
$font = __DIR__ . '/fonts/arial.ttf';
imagettftext($image, 24, 0, 30, 90, $text, $font, 'PHP GD TrueType 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");
$height=100; // Canvas Height
$width=300; // Canvas Width
$text="plus2net"; // Text string to be written on Image.
$x=10;
$y=70;
$font_size=60; // size of the font to be written
$rotation=0; // angle of rotation of text
$font_f = 'arial.ttf'; // font family , set the path to arial.ttf file
///// Create the image ////////
$im = @ImageCreate ($width,$height)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 204, 204, 204);
$text_color = ImageColorAllocate ($im, 51, 51, 255);
imagettftext($im, $font_size, $rotation, $x, $y, $text_color, $font_f, $text);
ImageJpeg ($im); // image displayed
imagedestroy($im); // Memory allocation for the image is removed.
?>
Output is here
<img src=gd-imagettftext-demo.php>
imagettftext($source_image,$font_size,$rotation, $int_x,$int_y,
$colour,$font_f,$text,$options)
$source_image : Image resource created before$font_size: Size of font $rotation : Angle of rotation in degree, default 0, positive value means counter-clockwise rotation.$int_x: Position of font in Horizontal X axis, Leftmost point is 0 $int_y: Postion of the font in Vertical Y Axis, Topmost point is 0$colour: Font colour$font_f: Font family$text: Text string to be written on the image $options: Color created before using imagecolorallocate().<?php
header ("Content-Type: image/jpeg");
$height=100; // Canvas Height
$width=300; // Canvas Width
$text="plus2net"; // Text string to be written on Image.
$x=0;
$y=70;
$font_size=60; // size of the font to be written
$rotation=0; // angle of rotation of text
$font_f = 'font/arial.ttf'; // font family
//$font = 'Times'; // font family
///// Create the image ////////
$im = @ImageCreate ($width,$height)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 204, 204, 204);
$text_color = ImageColorAllocate ($im, 51, 51, 255);
$shadow_color= ImageColorAllocate ($im, 0, 0, 0);
imagefttext($im, $font_size, $rotation, $x+2, $y+2, $shadow_color, $font, $text);
imagefttext($im, $font_size, $rotation, $x, $y, $text_color, $font_f, $text);
ImageJpeg ($im); // image displayed
imagedestroy($im); // Memory allocation for the image is removed.
?>
Output is here
$degDelta = 360 / ($length); // Change in angle for each char
<?php
header("Content-Type: image/jpeg");
$im = imagecreate(200,100); // width , height
$background = imagecolorallocate($im, 204,204,204);
$blue = imagecolorallocate($im, 0,0,255);
$cx = 100;// center X position , from left
$cy = 70;// center Y position , from top
$cr = 40; // Radius
$font_size=15;
$angle_degree=180; // total angle of rotation
$text='plus2net.com'; // text to be used
$text_length = strlen($text); // number of chars in string
$angle_Delta = $angle_degree / ($text_length); // Change in angle
if ($text_length > 0) {
for ($d = 0; $d < $text_length; $d++) {
// x and y coordinates for the char
$deltaX = $cx - cos(deg2rad($angle_Delta * $d)) * $cr;
$deltaY = $cy - sin(deg2rad($angle_Delta * $d)) * $cr;
imagefttext($im,$font_size,-($angle_Delta * $d + $angle_Delta / 2)+90,
$deltaX, $deltaY, $blue, 'arial.ttf', $text[$d]);
}
}
//$path='D:\\php_files\\php_tutorial\\images\\gd-curved-text1.jpg';
//Imagejpeg ($im,$path); // save to path
Imagejpeg($im); // display in browser
imagedestroy($im); //memory is removed.
?>
GD functions Imagestring()
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.