Home  >  Article  >  Backend Development  >  How to draw a text string image horizontally using the imagestring() function in PHP?

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

王林
王林forward
2023-09-05 09:33:041224browse

imagestring() is a built-in function in PHP that draws 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 An image in a given size.

  • $font − The $font parameter is used to set the font size value to 1, 2, 3, 4 and 5

    For built-in fonts.

  • $x - Maintains the font's position on the horizontal X-axis, top left corner.

  • $y - Maintains the font's position on the vertical Y-axis, top.

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

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

Return value

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

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

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

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

The above is the detailed content of How to draw a text string image horizontally using the imagestring() function in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete