linein designs

26 Feb, 2009

PHP Font Preview Function (text to image)

Posted by: john In: php| programing

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:

No related posts.

Tags:

2 Comments »

Comment by Paul Reinheimer

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.

 
Comment by j patton

EXACTLY what I have been looking for.
Simple and total control.
Thank you!

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post



Please Support Our Sponsors

A Payday advance can be a great way to get back on track.

Banner

About

My name is John Veldboom and have been in design and web programing for just over 8 years now. Started off as a hobby in high school but it has developed into a full time job now. I always tell people that if I wasn't doing this at work each day, I would be at home doing it for free. I love it!

Please visit the contact page to drop me a message.