Friday, October 17, 2025
HomeLanguagesPHP | imagechar() Function

PHP | imagechar() Function

The imagechar() function is an inbuilt function in PHP which is used to draw a character horizontally. This function draws the first character of string in the image identified by image with its x and y-axis. The coordinate of the top-left corner is (0, 0).

Syntax:

bool imagechar( $image, $font, $x, $y, $c, $color )

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

  • $image: It is returned by one of the image creation functions, such as imagecreatetruecolor(). It is used to create size of image.
  • $font: This parameter is used to set font size of character. Its value can be 1, 2, 3, 4, 5 for built-in fonts in latin2 encoding. The higher numbers represents the larger fonts and small number represent small font.
  • $x: This parameter is used to set x-coordinate to print character in image.
  • $y: This parameter is used to set y-coordinate to print character in image.
  • $c: The character which is printed.
  • $color: It sets the color of image. A color identifier created by imagecolorallocate() function.

Return Value: This function returns true on success or false on failure.

Below programs illustrate the imagechar() function in PHP:

Program 1:




<?php
  
// Creates image size
$image = imagecreate(400, 300);
  
$string = 'GeeksForGeeks';
  
// Set background color
$bg = imagecolorallocate($image, 0, 153, 0);
  
// Set text color.
$white = imagecolorallocate($image, 255, 255, 255);
  
// Prints a white G character
imagechar($image, 5, 190, 150, $string, $white);
  
header('Content-type: image/png');
imagepng($image);
  
?>


Output:
image

Program 2:




<?php
  
// Create image size
$image = imagecreate(400, 300);
  
$string = 'neveropen';
  
// Find string length
$len = strlen($string);
  
// Set background color
$bg = imagecolorallocate($image, 0, 153, 0);
  
// Set text color
$white = imagecolorallocate($image, 255, 255, 255);
  
for($i = 0; $i < $len; $i++)
  
    // Prints white character of string using loop
    imagechar($image, 6, 190 + 10 * $i, 150, $string[$i], $white);
  
header('Content-type: image/png');
imagepng($image);
  
?>


Output:
image

Related Articles:

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS