imagettftext() adding text to Image by GD

Adding text to image by using PHP GD function imagettftext() with options to rotate & x y positions


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().
Note : The font here arial.ttf file has to be placed and proper path is to be given.

Example :

<?Php
header ("Content-type: image/jpg");

$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

We saved the above code in a PHP file as gd-imagettftext-demo.php and displayed it as Image by using HTML code.
<img src=gd-imagettftext-demo.php>

Adding shadow to text

Dropping shadow from text written over Image with colour using imagettftext() PHP GD function
<?Php
header ("Content-type: image/jpg");

$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


Curved Text


Adding curved text written over Image with colour using imagettftext() PHP GD function


Change the total angle of rotation required.
$degDelta = 360 / ($length); // Change in angle for each char


<?php
header("Content-type: image/jpg");
$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()
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    GD functions in PHP for handling images & graphics

    How to check GD support in PHP
    Header for an image in browser
    Drawing of graphic lines using GD
    Plotting line graph by drawing data from table
    Adding vertical grid to line graph
    Adding rectangles using imagerectangle()
    Imagearc drawing arc and circle
    Imagefilledarc drawing filled arc and circle
    Resizing and adding border to Images
    Script for Watermarking image on the fly using PHP GD
    Generate image by using user input form data

    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer