Text to Image with PHP

If you have at least some experience with PHP you probably know that there are several image engines available for you to work with.
Today, I’ll be showing you a simple way of generating an image from a user provided string using the GD library. The goal of this code will be to generate a simple image with white background and black text centred inside of it.

I will be using my naming rules as seen at “Naming conventions and practices in PHP“.As time goes by I will be extending this code to provide additional functionality, but for now, lets get started:

1
2
<?php
    function textToImage($str_text = 'Default text'){

We open PHP tags and declare a new function called textToImage. This function takes one argument called $str_text, note that I prepended str_ to the variable name, so we know it should be a string. If no argument is provided when we call the function it will use it’s default value, in this case “Default text“.

1
2
3
4
5
    header ('Content-type: image/png');

    $int_image_width  = 200;
    $int_image_height = 20;
    $obj_image = imagecreate($int_image_width, $int_image_height);

Inside the function, we first specify that the document type is a png image. Keep in mind that you will not be able to write this function on a file and include that file into your code. This means that you will have a file specifically reserved for the textToImage function and will call it from the other files. I will get into that later. Afterwards, we specify the height and width of the image and create a new image with the imagecreate function.

1
2
    $color_black = imagecolorallocate($obj_image, 0, 0, 0);
    $color_white = imagecolorallocate($obj_image,255,255,255);

That right, you can’t use colors out-of-the-box with the gd library, you first need to declare a variable that will contain a colour identifier for the image object using the imagecolorallocate function and then use it when needed.

1
2
3
4
    $int_font_id = 1;
    $int_x       = 5;
    $int_y       = 3;
    imagestring($obj_image,$int_font_id,$int_x,$int_y,$str_text,$color_white);

The variable $int_font_id contains a font identifier from 1 to 5 (can be higher if you declared more fonts using the loadfont function), $int_x and $int_y are both coordinates of the upper left corner of the text, relative to the container image. This means that if the values of both variables are 0, the text will start at the top-left corner of the image.
The imagestring function, places the value of $str_text on the $obj_image image at the given X-Y coordinates and with the specified font.

1
2
3
    imagepng($obj_image);
    imagedestroy($obj_image);
}

Those two lines output the image and destroy the image object to free memory after it’s generated.
Last but not least you want to call the function to see it working, do that like this:

1
2
textToImage("Test string");
?>

If you want to specify the text variable from another file, you can change “Test string” to “$_GET['str_text']” and wherever you want to use that image, just do the following:

1
<img src='http://site.com/route/texToString.php?str_text=StringHere' />

Thus generating an image on the fly with the use of the simple php function we created together.

No posts at the moment. Check back again later!
Etiquetas: , , ,

About Juan

Hola! Me llamo Juan y soy un programador de 23 años de Madrid. Este es mi blog personal dedicado a la programación en general aunque con cierta predilección hacia PHP y Java.