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

王林
Release: 2023-09-05 09:52:02
forward
1198 people have browsed it

imagestring() is a built-in function in PHP that draws a string horizontally.

Syntax

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

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);
?>
Copy after login

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);
?>
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!