Link to home
Start Free TrialLog in
Avatar of Mark Gilbert
Mark GilbertFlag for United States of America

asked on

Replace characters of string with predesigned images (font retention) - URGENT!

Greetings experts,

I have a website that I am working on and it is important to keep the font intact.  I know that we could impliment some sort of font download in the css but I don't like this idea as I have seen it fail numerous times...and the font files sometimes get currupted when downloaded thus not giving the desired result.

I have created an entire character set using a particular font and have rendered each letter to correspond with the inputted text--I know that we will loose out on the kerning by doing this but that's a compromise we are very willing to take.  What I would like to happen is as follows:

This is some text
replaced with
<img src=capsT.gif><img src=h.gif><img src=i.gif> etc etc.

So, I thought that str_replace would be the best function for this using an array for all the characters I have as follows:

$title = "This is some text";
$imageLocation = "images/characters/";

$letters = array("A",  "B",  "C",  "D",  "E",  "F",  "G",  "H",  "I",  "J",  "K",  "L",  "M",  "N",  "O",  "P",  "Q",  "R",  "S",  "T",  "U",  "V",  "W",  "X",  "Y",  "Z",  "a",  "b",  "c",  "d",  "e",  "f",  "g",  "h",  "i",  "j",  "k",  "l",  "m",  "n",  "o",  "p",  "q",  "r",  "s",  "t",  "u",  "v",  "w",  "x",  "y",  "z",  "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "&",   "@",      ":",     ",",    "!",           "#",    "\"",            "-",     ".",      "+",    "£",     "?",        ";",          "'");
$images  = array("ca", "cb", "cc", "cd", "ce", "cf", "cg", "ch", "ci", "cj", "ck", "cl", "cm", "cn", "co", "cp", "cq", "cr", "cs", "ct", "cu", "cv", "cw", "cx", "cy", "cz", "la", "lb", "lc", "ld", "le", "lf", "lg", "lh", "li", "lj", "lk", "ll", "lm", "ln", "lo", "lp", "lq", "lr", "ls", "lt", "lu", "lv", "lw", "lx", "ly", "lz", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "and", "atsign", "colon", "coma", "exclamation", "hash", "inverted-coma", "minus", "period", "plus", "pound", "question", "semi-colon", "semi-inverted-coma");

echo str_replace($letters, "<img src='".$imageLocation.$images.".gif'>", $title);

If I echo out without the image tags, it appears to do the job, but when I try and output the images, the whole thing just fails, times out and doesn't give me anything.  It is possible that my arrays could be too large, but I don't work much with arrays so I wouldn't be too sure.

Is there a way of optimizing the above code so that no matter how many extra characters I may add (for example the requirement to have a percentage sign may come up), and as long as the title string is of resonable length for a page heading, that the script functions in a timely but effective manner?  This script could be ran by 100's of users at the same time.

Please help experts, I really need to get this working, and once it is, it will be an excellent snippet for other developers out there with similar problems.

Thanks for your help.
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Place the code just infront of the str_replace

reset($images); //put internal arraypointer at first location
while(list($key, $value) = each($images)) {
   $images[$key] = '<img src="'.$imageLocation.$value.'.gif" alt="'.$value.'" />';
}

echo str_replace($letter, $images, $title);

Have you considered using php to generate title images using the (TTF)  font of your liking?

-r-
Avatar of Mark Gilbert

ASKER

Hi Roonaan,

Thanks for your input.  I have tried the above code and it seems as if it is going into an endless loop.  What appears to be happening is that when the str_replace is being ran, it's looking for the letters in $letter, replacing with the strings in $images and then it's doing it over and over.  So as we are adding stuff in $images, it's rerunning on the new values.

This seems very strange but it's apparent that we are going to go no-where if we are adding characters to the values.
Oh, if we were to use the text to tif, how complicated is this, and once the images have been created, how large would they be?
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As to the generated images: http://nl2.php.net/manual/en/function.imagettftext.php

I am not sure what would be the size.

-r-
Roonaan, your function has worked a treat.  Thanks very much for all your help and input, it really is greatly appreciated.

I'll have a look into the imagettftext function and give that a bash.  At least for the time being I have something working which is a good start.

Thanks again and have a great day.