Convert Text to Image Using PHP

People have been trying to convert a text to an image for various reason. They usually need it to turn an e-mail address so it cannot be easily harvested from the content. But this script will not only able to do that, it can be use for many other thing, such as styling your website. As we notice, there are many True-type font we want to use for better site typography like using it on a document heading or else. The problem is, not many people have the font we want. So, how can we solve that? One of the solution is using an image, and this tutorial will explain how to convert any text to image on your website using PHP.

Script to Convert Text to Image

First, we need to create a PHP file to write the script. At the very begining of the script. we need to configure how we want the image to be generated by simply write this code:

<?php
	$font_size = 32px; // The generated size (in pixel) of the font
	$font_color = '000000'; // The generated color of the font (in HEX)
	$font = 'arena-condensed.ttf'; // Font file we need to use
	$text = 'PHP GD Text to Image example'; // Text we need to convert
?>

Before we generate the image, we should convert the color into Hexadecimal. Add this below the above script:

<?php
	list($r, $g, $b) = array(
		$font_color[0].$font_color[1],
		$font_color[2].$font_color[3],
		$font_color[4].$font_color[5]
	);
	$r = hexdec($r);
	$g = hexdec($g);
	$b = hexdec($b);
	$color = array( $r, $g, $b );
?>

Now that we get all the parameter we need, the next step is the conversion. Add this below the previous script:

<?php
	// Get the image size
	$size = @imagettfbbox($font_size,0,$font,$text);
	$xsize = abs($size[0]) + abs($size[2]);
	$ysize = abs($size[5]) + abs($size[1]);
	$image = @imagecreatetruecolor($xsize+5, $ysize);

	// Set the image transparency
	@imagesavealpha($image, true);
	$transparent = @imagecolorallocatealpha($image,0,0,0,127);
	@imagefill($image,0,0,$transparent);

	// Coloring the text
	$font_color	= @imagecolorallocate($image, $color[0], $color[1], $color[2]);
	@imagettftext($image,$font_size,0,0,abs($size[5]),$font_color,$font,$text);

	// Return the image to the browser
	header("Content-Type: image/png");

	@imagepng($image);
	@imagedestroy($image);

	ob_flush();
?>

We’re done! Wrap up all the code in a file and upload it to your server. Remember to upload the font you are going to use (in this case, it should be named arena-condensed.ttf ) in the same directory with the script. Call the file on your browser to see the result, it should be something like this:

PHP GD Text to Image Example

Click here for more information about PHP GD

5 comments on "Convert Text to Image Using PHP"
2 trackbacks
  1. abcphp.com

    Convert Text to Image Using PHP | Arie Putranto…

    People have been trying to convert a text to an image for various reason. They usually need it to turn an e-mail address so it cannot be easily harvested from the content. But this script will not only able to do that, it can be use for many other thin…

  2. php-html.net

    Convert Text to Image Using PHP…

    People have been trying to convert a text to an image for various reason. They usually need it to turn an e-mail address so it cannot be easily harvested from the content. But this script will not only able to do that, it can be use for many other thin…

Leave a Response