PHP, WEB

Turn Text into an Image

The PHP library GD is awesome! One the things it can do is to turn your text into an Image!

Paste the code below in a php file and open it in your browser!

1
2
3
4
5
6
7
8
9
10
11
12
header("Content-type: image/png");
$text = "Hello GD Library.";
$font  = 4;
$height = imagefontheight($font);
$width  = imagefontwidth($font) * strlen($text);
$image = imagecreatetruecolor ($width, $height);
$white = imagecolorallocate ($image, 217, 217, 217);
$black = imagecolorallocate ($image, 0, 0, 0);
imagefill($image, 0, 0, $white);
imagestring ($image, $font, 0, 0, $text, $black);
imagepng ($image);
imagedestroy($image);

You will see something like this: