Text To Image Using PHP

by Hiroshi on August 7, 2008

in Image Handling, Security, Spam Control, Useful Scripts

Suppose you want to protect you email address and want to put it online in form of image or you are displaying visitors emails in form of images while they had entered simple text, then this script is very useful. Text used in this example is my-email@hotmail.com under variable text. Find it and replace it or give input by a form and have image version of your entered text.

/* NOTE: This code assumes that you have the GD 2 Library installed on your php server and that all goes well.
It does not contain any error handling.
    	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    	header("Cache-Control: no-cache");
    	// dont cache
    	header("Pragma: no-cache");
    	// dont cache again, for whatever reason, I forget.
    	// Above headers are to prevent cache of
    	// the image so that changes will be seen immediately.
    	header("Content-Type: image/jpeg");
    	// Specify that we are outputting a jpeg image.
    	$padding = 5;
    	// Padding: Distance from edges of image to text. Much like the padding property in CSS
    	$font = 5;
    	// Define The Font. Can be integers 1 through 5 or can be imageloadfont(imagefont.gdf).
    	// Must Be A PHP Font (*.gdf) for this script to work properly.
    	$text = "my-email@hotmail.com";
    	// Define The Text On The Image
    	$height = imagefontheight($font) + ($padding * 2);
    	// Sets Height Of Image
    	$width = imagefontwidth($font) * strlen($text) + ($padding * 2);
    	// Sets Width Of Image (Adjusted for font size);
    	$image_handle = imagecreatetruecolor($width, $height);
    	// Initializes The Image Object
    	$text_color = imagecolorallocate($image_handle, 0, 0, 0);
    	// Sets The Forecolor to Black (rgb: 0-0-0)
    	$background_color = imagecolorallocate($image_handle, 255, 255, 255);
    	// Sets The Backcolor to white (rgb: 255-255-255)
    	$bg_height = imagesy($image_handle);
    	// Sets the background Y2 position
    	$bg_width = imagesx($image_handle);
    	// Sets The Background X2 Position
    	imagefilledrectangle($image_handle, 0, 0, $bg_width, $bg_height, $background_color);
    	// Draws a background (filled rectangle) over the entire span of the image.
    	imagestring($image_handle, $font, $padding, $padding, $text, $text_color);
    	// Draw the text onto the foreground of the image.
    	// php draws from back to front so the rectangle comes first.
    	// Output the image. filename value of NULL tells GD to output to the client
    	// as opposed to writing to a file. enter a file name to output to a file.
    	// qality value of 100 tells php to output the best possible quality jpeg as possible
    	// quality values are from 1 to 100

Download Script

Related Posts

Previous post:

Next post: