How to draw a text string image horizontally by using imagestring() function in PHP?

PHPServer Side ProgrammingProgramming

imagestring() is an inbuilt function in PHP that is used to draw a string horizontally.

Syntax

bool imagestring($image, $font, $x, $y, $string, $color)

Parameters

imagestring() accepts six different parameters − $image, $font, $x, $y, $string, and $color.

  • $image − The $image parameter uses imagecreatetruecolor() function to create a blank image in the given size.

  • $font − The $font parameter is used to set the font size values from 1, 2, 3, 4, and 5 for inbuilt fonts.

  • $x − Holds the position of the font in the horizontal X-axis, upper leftmost corner.

  • $y − Holds the position of the font in the vertical Y-axis, topmost corner.

  • $string − The $string parameter holds the string to be written.

  • $color − This parameter holds the color of the image.

Return Values

imagestring() returns True on success and False on failure.

Example 1

<?php
   // Create the size and image by using imagecreate() function.
   $img = imagecreate(700, 300);

   // Set the background color of the image
   $background_color = imagecolorallocate($img, 0, 0, 255);

   // Set the text color of the image
   $text_color = imagecolorallocate($img, 255, 255, 255);

   // Function to create an image that contains the string.
   imagestring($img, 50, 180, 150, "Tutorialspoint", $text_color);
   imagestring($img, 30, 160, 120, "Simply Easy Learning", $text_color);
   header("Content-Type: image/png");
   imagepng($img);
   imagedestroy($img);
?>

Output

Example 2

<?php
   // Create the size of the image or blank image
   $img = imagecreate(700, 300);

   // Set the background color of the image
   $background_color = imagecolorallocate($img, 122, 122, 122);

   // Set the text color of the image
   $text_color = imagecolorallocate($img, 255, 255, 0);

   // Function to create an image that contains a string.
   imagestring($img, 10, 30, 60,"Tutorialspoint:Simply Easy Learning", $text_color);
   header("Content-Type: image/png");
   imagepng($img);
   imagedestroy($img);
?>

Output

raja
Published on 09-Aug-2021 13:12:31
Advertisements