For work we needed a way to allow the customer to preview what their Bible imprint text would look like before they placed their order. I’m sure there are ways to do this with JavaScript, but we chose to do this with PHP to prevent the site from relying on Javascript (there was already enough on the page).
View Demo
Download files (32 KB)
function text2image($text,$text_type)
{
$text = ereg_replace("[^A-Za-z0-9 -]", "", $text); // change to allow whatever characters you made images for
$length = strlen($text); // finds number of characters
for($b=0; $b<$length; $b++) // loops through each character to convert to image
{
$text_type_function = $text_type; // resets font type each time
if($text_type_function == 'sc') // script font type
{
if(ctype_upper($text[$b])){$text_type_function = 'sc_u';} // changes text type to upper
elseif(is_numeric($text[$b])){$text_type_function = 'sc';} // checks for number (not really used in this function yet)
if($text[$b] == ' '){$word .= ' ';} // could change to be blank image, but text spaces seem easier
else
{
$image_word .= '
';
}
}
else // else block type
{
if(ctype_upper($text[$b])){$text_type_function = 'bm_u';}
elseif(is_numeric($text[$b])){$text_type_function = 'bm';}
$text[$b] = strtolower($text[$b]);
if($text[$b] == ' '){$word .= ' ';}
else
{
$image_word .= '
';
}
}
}
return $image_word;
}
// useage
$text = 'Preview this font please';
echo text2image($text,'sc');
The above function works for the two fonts included in the zip file, but to add more, just create an image of all the letters and add another elseif statement to the function.
If you have any questions or need any help with adding more fonts, please either use the contact form or leave a comment below.
Similar Posts:
- jQuery AJAX Loading – Display Images or Text Until Script is Finished (Part 2)
- UPDATED: Strip Off Characters from String Using PHP (substr)
- jQuery Loading – Display Images or Text Until Script is Finished
- Run Javascript Code After an AJAX Request
- Text to Image Using PHP and img4me


April 9th, 2009 @ 9:26 am
I like it, nice and low tech. My first thought was using a tool like GD or ImageMagick to write teh text on the image, but doing it your way saves installing fonts on the server and other headaches.
Just a small heads up the ereg library may end up being depricated in a future release of PHP, they’re recomending people switch over to the PCRE stuff now.
September 11th, 2009 @ 4:10 am
EXACTLY what I have been looking for.
Simple and total control.
Thank you!