June 23rd in PHP Scripts by .

PHP script to convert text string to image

PHP script to write email address into a image

I was after a php script that dynamically converts text to image and finally i decided to code on my own. It also does convert small length text to image like names, emails etc..

This script automatically calculates the width of text into pixels inserts the PNG or gif image.

this is a string to < >

To call the script for dynamically generating an image just use the html code

<img src="txt2img.php?text=<? echo $str ?>" border="0">

and the code for the script is

//source for txt2img.php

<?

if(!isset($_GET['txt']))
{
exit();
}

header ("Content-type: image/png");
$string = $_GET['txt'];
$font   = 4;
$width  = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);

$im = @imagecreate ($width,$height);
$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
$text_color = imagecolorallocate ($im, 0, 0,0);//black text
imagestring ($im, $font, 0, 0,  $string, $text_color);
imagepng ($im);
?>

Download: txt2img.zip

Requirements:

Apache
PHP GD Library with PNG/GIF/JPEG support

Similar Posts:

3 Comments

  • canalaiz
    January 4, 2010
  • Bambang Puji
    January 14, 2012
  • shimpy
    May 16, 2012

Leave A Comment.





Please wrap all source codes with [code][/code] tags. Powered by