PHP | imagestring() Function

The imagestring() function is an inbuilt function in PHP which is used to draw the string horizontally. This function draws the string at given position.

Syntax:

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

Parameters: This function accepts six parameters as mentioned above and described below:



  • $image: The imagecreatetruecolor() function is used to create a blank image in a given size.
  • $font: This parameter is used to set the font size. Inbuilt font in latin2 encoding can be 1, 2, 3, 4, 5 or other font identifiers registered with imageloadfont() function.
  • $x: This parameter is used to hold the x-coordinate of the upper left corner.
  • $y: This parameter is used to hold the y-coordinate of the upper left corner.
  • $string: This parameter is used to hold the string to be written.
  • $color: This parameter is used to hold the color of image.

Return Value: This function returns TRUE on success or FALSE on failure.

Below programs illustrate the imagestring() function in PHP.
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Create the size of image or blank image
$image = imagecreate(500, 300);
   
// Set the background color of image
$background_color = imagecolorallocate($image, 0, 153, 0);
   
// Set the text color of image
$text_color = imagecolorallocate($image, 255, 255, 255);
   
// Function to create image which contains string.
imagestring($image, 5, 180, 100,  "GeeksforGeeks", $text_color);
imagestring($image, 3, 160, 120,  "A computer science portal", $text_color);
   
header("Content-Type: image/png");
   
imagepng($image);
imagedestroy($image);
?>

chevron_right


Output:
create image function

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create the size of image or blank image
$image = imagecreate(500, 300);
  
// Set the background color of image
$background_color = imagecolorallocate($image, 255, 255, 255);
  
// Set the text color of image
$text_color = imagecolorallocate($image, 0, 153, 0);
  
// Function to create image which contains string.
imagestring($image, 5, 50, 100, 
     "GeeksforGeeks: A computer science portal", $text_color);
  
header("Content-Type: image/png");
  
imagepng($image);
imagedestroy($image);
?>

chevron_right


Output:
imagestring function

Related Articles:

Reference: http://php.net/manual/en/function.imagestring.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.