Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts

    FUNCTION: text2image()

    I don't know if this is too long for here but I imagine it will post so that should make it short enough.

    I wanted on my site to dynamically use function too display text in the form of premade images. After help from missing-score I finally got it to work without seperating each character with a comma (making it so I couldn't use the comma image) which was just lame.

    Anyway, with this function you can dynamically place text in the form of images anywhere on your site where the function is included. You would have to make all the letters and such though. You can easily also expand to this to support other characters.

    Now I have not yet gotten word wrapping into it and if anyone wants to help me with that, that be cool. Havn't messed with it yet.

    text2image.function.php
    PHP Code:
    <?php

    function text2image($message) {

     
    $message strip_tags($message);
     
    $messageLength strlen($message);
     
    $i 0;
     while(
    $i $messageLength) {
        if (
    $message{$i} == "|") {
          echo 
    '<br>';
        } elseif (
    $message{$i} == "a") {
          echo 
    '<img src="images/letters/a.gif">';
        } elseif (
    $message{$i} == "b") {
          echo 
    '<img src="images/letters/b.gif">';
        } elseif (
    $message{$i} == "c") {
          echo 
    '<img src="images/letters/c.gif">';
        } elseif (
    $message{$i} == "d") {
          echo 
    '<img src="images/letters/d.gif">';
        } elseif (
    $message{$i} == "e") {
          echo 
    '<img src="images/letters/e.gif">';
        } elseif (
    $message{$i} == "f") {
          echo 
    '<img src="images/letters/f.gif">';
        } elseif (
    $message{$i} == "g") {
          echo 
    '<img src="images/letters/g.gif">';
        } elseif (
    $message{$i} == "h") {
          echo 
    '<img src="images/letters/h.gif">';
        } elseif (
    $message{$i} == "i") {
          echo 
    '<img src="images/letters/i.gif">';
        } elseif (
    $message{$i} == "j") {
          echo 
    '<img src="images/letters/j.gif">';
        } elseif (
    $message{$i} == "k") {
          echo 
    '<img src="images/letters/k.gif">';
        } elseif (
    $message{$i} == "l") {
          echo 
    '<img src="images/letters/l.gif">';
        } elseif (
    $message{$i} == "m") {
          echo 
    '<img src="images/letters/m.gif">';
        } elseif (
    $message{$i} == "n") {
          echo 
    '<img src="images/letters/n.gif">';
        } elseif (
    $message{$i} == "o") {
          echo 
    '<img src="images/letters/o.gif">';
        } elseif (
    $message{$i} == "p") {
          echo 
    '<img src="images/letters/p.gif">';
        } elseif (
    $message{$i} == "q") {
          echo 
    '<img src="images/letters/q.gif">';
        } elseif (
    $message{$i} == "r") {
          echo 
    '<img src="images/letters/r.gif">';
        } elseif (
    $message{$i} == "s") {
          echo 
    '<img src="images/letters/s.gif">';
        } elseif (
    $message{$i} == "t") {
          echo 
    '<img src="images/letters/t.gif">';
        } elseif (
    $message{$i} == "u") {
          echo 
    '<img src="images/letters/u.gif">';
        } elseif (
    $message{$i} == "v") {
          echo 
    '<img src="images/letters/v.gif">';
        } elseif (
    $message{$i} == "w") {
          echo 
    '<img src="images/letters/w.gif">';
        } elseif (
    $message{$i} == "x") {
          echo 
    '<img src="images/letters/x.gif">';
        } elseif (
    $message{$i} == "y") {
          echo 
    '<img src="images/letters/y.gif">';
        } elseif (
    $message{$i} == "z") {
          echo 
    '<img src="images/letters/z.gif">';
        } elseif (
    $message{$i} == "A") {
          echo 
    '<img src="images/letters/upper/a.gif">';
        } elseif (
    $message{$i} == "B") {
          echo 
    '<img src="images/letters/upper/b.gif">';
        } elseif (
    $message{$i} == "C") {
          echo 
    '<img src="images/letters/upper/c.gif">';
        } elseif (
    $message{$i} == "D") {
          echo 
    '<img src="images/letters/upper/d.gif">';
        } elseif (
    $message{$i} == "E") {
          echo 
    '<img src="images/letters/upper/e.gif">';
        } elseif (
    $message{$i} == "F") {
          echo 
    '<img src="images/letters/upper/f.gif">';
        } elseif (
    $message{$i} == "G") {
          echo 
    '<img src="images/letters/upper/g.gif">';
        } elseif (
    $message{$i} == "H") {
          echo 
    '<img src="images/letters/upper/h.gif">';
        } elseif (
    $message{$i} == "I") {
          echo 
    '<img src="images/letters/upper/i.gif">';
        } elseif (
    $message{$i} == "J") {
          echo 
    '<img src="images/letters/upper/j.gif">';
        } elseif (
    $message{$i} == "K") {
          echo 
    '<img src="images/letters/upper/k.gif">';
        } elseif (
    $message{$i} == "L") {
          echo 
    '<img src="images/letters/upper/l.gif">';
        } elseif (
    $message{$i} == "M") {
          echo 
    '<img src="images/letters/upper/m.gif">';
        } elseif (
    $message{$i} == "N") {
          echo 
    '<img src="images/letters/upper/n.gif">';
        } elseif (
    $message{$i} == "O") {
          echo 
    '<img src="images/letters/upper/o.gif">';
        } elseif (
    $message{$i} == "P") {
          echo 
    '<img src="images/letters/upper/p.gif">';
        } elseif (
    $message{$i} == "Q") {
          echo 
    '<img src="images/letters/upper/q.gif">';
        } elseif (
    $message{$i} == "R") {
          echo 
    '<img src="images/letters/upper/r.gif">';
        } elseif (
    $message{$i} == "S") {
          echo 
    '<img src="images/letters/upper/s.gif">';
        } elseif (
    $message{$i} == "T") {
          echo 
    '<img src="images/letters/upper/t.gif">';
        } elseif (
    $message{$i} == "U") {
          echo 
    '<img src="images/letters/upper/u.gif">';
        } elseif (
    $message{$i} == "V") {
          echo 
    '<img src="images/letters/upper/v.gif">';
        } elseif (
    $message{$i} == "W") {
          echo 
    '<img src="images/letters/upper/w.gif">';
        } elseif (
    $message{$i} == "X") {
          echo 
    '<img src="images/letters/upper/x.gif">';
        } elseif (
    $message{$i} == "Y") {
          echo 
    '<img src="images/letters/upper/y.gif">';
        } elseif (
    $message{$i} == "Z") {
          echo 
    '<img src="images/letters/upper/z.gif">';
        } elseif (
    $message{$i} == "0") {
          echo 
    '<img src="images/numbers/0.gif">';
        } elseif (
    $message{$i} == "1") {
          echo 
    '<img src="images/numbers/1.gif">';
        } elseif (
    $message{$i} == "2") {
          echo 
    '<img src="images/numbers/2.gif">';
        } elseif (
    $message{$i} == "3") {
          echo 
    '<img src="images/numbers/3.gif">';
        } elseif (
    $message{$i} == "4") {
          echo 
    '<img src="images/numbers/4.gif">';
        } elseif (
    $message{$i} == "5") {
          echo 
    '<img src="images/numbers/5.gif">';
        } elseif (
    $message{$i} == "6") {
          echo 
    '<img src="images/numbers/6.gif">';
        } elseif (
    $message{$i} == "7") {
          echo 
    '<img src="images/numbers/7.gif">';
        } elseif (
    $message{$i} == "8") {
          echo 
    '<img src="images/numbers/8.gif">';
        } elseif (
    $message{$i} == "9") {
          echo 
    '<img src="images/numbers/9.gif">';
        } elseif (
    $message{$i} == "'") {
          echo 
    '<img src="images/letters/upper/squo.gif">';
        } elseif (
    $message{$i} == "\"") {
          echo 
    '<img src="images/letters/upper/dquo.gif">';
        } elseif (
    $message{$i} == ".") {
          echo 
    '<img src="images/letters/upper/per.gif">';
        } elseif (
    $message{$i} == " ") {
          echo 
    '&nbsp;&nbsp;';
        } elseif (
    $message{$i} == "?") {
          echo 
    '<img src="images/letters/upper/que.gif">';
        } elseif (
    $message{$i} == "!") {
          echo 
    '<img src="images/letters/upper/exc.gif">';
        } elseif (
    $message{$i} == ",") {
          echo 
    '<img src="images/letters/upper/coma.gif">';
        } else { }
        
    $i++;
     }

    }

    ?>
    example.php
    PHP Code:
    <?php 
    include_once("text2image.function.php");

    echo 
    text2image("Hello world!");

    ?>
    Last edited by Element; 11-27-2005 at 02:08 AM.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Neat, I thought this was going to be a GD thing, but this is a lot more unique. You can add word wrapping the same way you did spaces, just replace "\n" with <br />. Here's a version that outputs XHTML formatted tags:

    PHP Code:
    <?php

    function XHTMLtext2image($string)
    {
        
    $miscmarks = array (','=>'<img src="images/letters/upper/coma.gif" alt="," />'
                            
    '!'=>'<img src="images/letters/upper/exc.gif" alt="!" />'
                            
    '?'=>'<img src="images/letters/upper/que.gif" alt="?" />',
                            
    ' '=>'&nbsp;&nbsp;',
                            
    '.'=>'<img src="images/letters/upper/per.gif" alt="." />',
                            
    '"'=>'<img src="images/letters/upper/dquo.gif" alt="&quot;" />',
                            
    '\''=>'<img src="images/letters/upper/squo.gif" alt="\'" />',
                            
    "\n"=>'<br />');
        
    $ret '';
        
    $len strlen($string);
        
        for (
    $ii 0$ii $len; ++$ii)
        {
            if (
    $string[$ii] >= 'a' && $string[$ii] <= 'z')
            {
                
    $ret .= '<img src="images/letters/' $string[$ii] . '.gif" alt="' $string[$ii] . '" />';
                continue;
            }
            elseif (
    $string[$ii] >= 'A' && $string[$ii] <= 'Z')
            {
                
    $ret .= '<img src="images/letters/upper/' $string[$ii] . '.gif" alt="' $string[$ii] . '" />';
                continue;
            }
            elseif (isset(
    $miscmarks[$string[$ii]]))
            {
                
    $ret .= $miscmarks[$string[$ii]];
                continue;
            }
            elseif (
    is_numeric($string[$ii]))
            {
                
    $ret .= '<img src="images/numbers/' $string[$ii] . '.gif" alt="' $string[$ii] . '" />';
                continue;
            }
        }
        return(
    $ret);
    }

    echo 
    XHTMLtext2image("Hello, \nWorld!");
    ?>
    edit optimized to run a few seconds faste per 1000 calls 8)
    Last edited by ralph l mayo; 11-27-2005 at 03:53 AM.

  • #3
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by ralph l mayo
    Neat, I thought this was going to be a GD thing, but this is a lot more unique. You can add word wrapping the same way you did spaces, just replace "\n" with <br />. Here's a version that outputs XHTML formatted tags:

    PHP Code:
    <?php

    function XHTMLtext2image($string)
    {
        
    $miscmarks = array (','=>'<img src="images/letters/upper/coma.gif" alt="," />'
                            
    '!'=>'<img src="images/letters/upper/exc.gif" alt="!" />'
                            
    '?'=>'<img src="images/letters/upper/que.gif" alt="?" />',
                            
    ' '=>'&nbsp;&nbsp;',
                            
    '.'=>'<img src="images/letters/upper/per.gif" alt="." />',
                            
    '"'=>'<img src="images/letters/upper/dquo.gif" alt="&quot;" />',
                            
    '\''=>'<img src="images/letters/upper/squo.gif" alt="\'" />',
                            
    "\n"=>'<br />');
        
    $ret '';
        
        for (
    $ii 0$ii strlen($string); ++$ii)
        {
            if (isset(
    $miscmarks[$string[$ii]]))
            {
                
    $ret .= $miscmarks[$string[$ii]];
            }
            elseif (
    is_numeric($string[$ii]))
            {
                
    $ret .= '<img src="images/numbers/' $string[$ii] . '.gif" alt="' $string[$ii] . '" />';
            }
            elseif (
    $string[$ii] >= 'a' && $string[$ii] <= 'z')
            {
                
    $ret .= '<img src="images/letters/' $string[$ii] . '.gif" alt="' $string[$ii] . '" />';
            }
            elseif (
    $string[$ii] >= 'A' && $string[$ii] <= 'Z')
            {
                
    $ret .= '<img src="images/letters/upper/' $string[$ii] . '.gif" alt="' $string[$ii] . '" />';
            }
        }
        return(
    $ret);
    }

    echo 
    XHTMLtext2image("Hello, \nWorld!");
    ?>
    Thats not what I mean. I have that in there. But since this looks at each characters I use '|' to for a line break. Like, seperating lines, for example:

    PHP Code:
    echo text2image("Hello world!|Hello again, world!"); 

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Quote Originally Posted by Element
    Thats not what I mean. I have that in there. But since this looks at each characters I use '|' to for a line break. Like, seperating lines, for example:

    PHP Code:
    echo text2image("Hello world!|Hello again, world!"); 
    You don't have to use |, \n is one character and it actually means newline. Here's a demo with wrapping at 40 chars:

    PHP Code:
    $string "I don't know if this is too long for here but I imagine it will post so that should make it short enough.

    I wanted on my site to dynamically use function too display text in the form of premade images. After help from missing-score I finally got it to work without seperating each character with a comma (making it so I couldn't use the comma image) which was just lame.

    Anyway, with this function you can dynamically place text in the form of images anywhere on your site where the function is included. You would have to make all the letters and such though. You can easily also expand to this to support other characters. 

    Now I have not yet gotten word wrapping into it and if anyone wants to help me with that, that be cool. Havn't messed with it yet."
    ;

    $string wordwrap($string40); // for my function, I think yours would be wordwrap($string, 40, '|') but I haven't tested it
    echo XHTMLtext2image($string);
    echo 
    XHTMLtext2image("Newline\ncharacter"); 
    Output is:
    Code:
    I don't know if this is too long for 
    here but I imagine it will post so 
    that should make it short enough.
    
    I wanted on my site to dynamically use
    ...
    Newline
    Character
    with each letter replaced by the full <img> tag
    Last edited by ralph l mayo; 11-27-2005 at 03:33 AM.

  • #5
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by ralph l mayo
    You don't have to use |, \n is one character and it actually means newline. Here's a demo with wrapping at 40 chars:

    PHP Code:
    $string "I don't know if this is too long for here but I imagine it will post so that should make it short enough.

    I wanted on my site to dynamically use function too display text in the form of premade images. After help from missing-score I finally got it to work without seperating each character with a comma (making it so I couldn't use the comma image) which was just lame.

    Anyway, with this function you can dynamically place text in the form of images anywhere on your site where the function is included. You would have to make all the letters and such though. You can easily also expand to this to support other characters. 

    Now I have not yet gotten word wrapping into it and if anyone wants to help me with that, that be cool. Havn't messed with it yet."
    ;

    $string wordwrap($string40); // for my function, I think yours would be wordwrap($string, 40, '|') but I haven't tested it
    echo XHTMLtext2image($string);
    echo 
    XHTMLtext2image("Newline\ncharacter"); 
    Output is:
    Code:
    I don't know if this is too long for 
    here but I imagine it will post so 
    that should make it short enough.
    
    I wanted on my site to dynamically use
    ...
    Newline
    Character
    with each letter replaced by the full <img> tag
    . . . . Nooo \n is 2 characters. Its just outputed as a linebreak. If you want to post that as a seperate script go right ahead, but what your not understanding is the script looks at one character at a time, so for instance it would only look at "\" of "\n" not both characters. All this script does is individually looks at each character, and if it is part of the image replacements, it will output the image tag, or HTML if it is another character like a space or the | linebreak.
    Last edited by Element; 11-27-2005 at 09:50 PM.

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    PHP Code:
    <?php
    print strlen("\n"); // It returns 1. Live with it
    ?>
    I'll add my attempt here (with a bit of gd):
    PHP Code:
    <?php
    $path 
    './chars/';

    function 
    text2image($text)
    {
        global 
    $path;
        
    $ret '';
        for(
    $i 0$n strlen($text); $i $n$i++)
        {
            
    $ret .= ($text{$i} == chr(10)) ? '<br />' get_img(ord($text{$i}));
        }
        return 
    $ret;
    }

    function 
    get_img($chr)
    {
        global 
    $path;
        if(!
    file_exists($path $chr '.png'))
        {
            
    $im imagecreate(1010);
            
    imagecolorallocatealpha($im255255255255);
            
    $black imagecolorallocate($im000);
            
    imagechar($im110chr($chr), $black);
            
    imagepng($im$path $chr '.png');
        }
        return 
    '<img src="' $path $chr '.png" alt="' chr($chr) . '" />';
    }

    print 
    text2image('Hello World!' "\n" 'Nice to meet you!');
    ?>
    Last edited by marek_mar; 11-27-2005 at 10:05 PM.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #7
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by marek_mar
    PHP Code:
    <?php
    print strlen("\n"); // It returns 1. Live with it
    ?>
    I'll add my attempt here (with a bit of gd):
    PHP Code:
    <?php
    $path 
    './chars/';

    function 
    text2image($text)
    {
        global 
    $path;
        
    $ret '';
        for(
    $i 0$n strlen($text); $i $n$i++)
        {
            
    $ret .= ($text{$i} == chr(10)) ? '<br />' get_img(ord($text{$i}));
        }
        return 
    $ret;
    }

    function 
    get_img($chr)
    {
        global 
    $path;
        if(!
    file_exists($path $chr '.png'))
        {
            
    $im imagecreate(1010);
            
    imagecolorallocatealpha($im255255255255);
            
    $black imagecolorallocate($im000);
            
    imagechar($im110chr($chr), $black);
            
    imagepng($im$path $chr '.png');
        }
        return 
    '<img src="' $path $chr '.png" alt="' chr($chr) . '" />';
    }

    print 
    text2image('Hello World!' "\n" 'Nice to meet you!');
    ?>
    Its only returning one because thats what it is outputed as, one character, but before it i outputd it is still 2. This being why the script over looks "\n" when I had it in there in the beginning.

    I just tried both methods and they both work. The only problem is they arn't faster. Infact they're much, much slower. Ralphs method seems to out put the code one at time, watching the text pop up on the screen, where as mine has all the images there on load and is already loading the images.

    Mareks is nice witht he GD, but it leaves the page blank for a few seconds while to parses th GD.
    Last edited by Element; 11-27-2005 at 10:31 PM.

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It makes the images only once.
    The second time it runs will be much faster.
    If you can't live with \n as one character use chr(10) instead.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #9
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Quote Originally Posted by Element
    Ralphs method seems to out put the code one at time, watching the text pop up on the screen, where as mine has all the images there on load and is already loading the images.
    Hey now. My only appears slower because it uses buffered output instead of echoing every tag as it comes to it. If I change the return behavior to echo:
    PHP Code:
    function text2image($message) { // like above }
    function XHTMLtext2image($string) { // returns changed to echo }

    $teststring 'Which function will be faster?';
    $t time();
    for (
    $i 0$i <= 1000$i++)
    {
        
    text2image($teststring);
    }
    $firsttime = (time() - $t) . ' seconds';

    $t time();
    for (
    $i 0$i <= 1000$i++)
    {
        
    XHTMLtext2image($teststring);
    }
    echo 
    $firsttime '<br />' . (time() - $t) . ' seconds<br />';
    //  On my computer: 9 vs 7, 9 vs 8, 8 vs 7  :D 
    Not that it even really matters in this instance, just defending my honor.

  • #10
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Maybe in other scripts when it already parses, but I'm telling you, I already did it and debugged and it only returned N as a image because it found the slash and did nothing.

    And I see what you mean. I like your Marek's cause well... its good if you want to be lazy and not make all the images like I did. Course I did mine like that back of the bg color, shadowing, and font.

    EDIT:
    Hey now. My is only slower because it uses buffered output instead of echoing every tag as it comes to it. If I change the return behavior to echo:

    PHP Code:

    function text2image($message) { // like above } 
    function XHTMLtext2image($string) { // returns changed to echo } 

    $teststring 'Which function will be faster?'
    $t time(); 
    for (
    $i 0$i <= 1000$i++) 

        
    text2image($teststring); 

    $firsttime = (time() - $t) . ' seconds'

    $t time(); 
    for (
    $i 0$i <= 1000$i++) 

        
    XHTMLtext2image($teststring); 

    echo 
    $firsttime '<br />' . (time() - $t) . ' seconds<br />'
    //  On my computer: 9 vs 7, 9 vs 8, 8 vs 7  :D 
    Not that it even really matters in this instance, just defending my honor.
    '

    Woah, there. It wasn't a diss or anything man. I was just pointing it out cause you said it was faster and for me it wasn't.
    Last edited by Element; 11-27-2005 at 11:06 PM.

  • #11
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Quote Originally Posted by Element
    Maybe in other scripts when it already parses, but I'm telling you, I already did it and debugged and it only returned N as a image because it found the slash and did nothing.
    This is correct behavior for single quoted '\n', maybe that's the issue.

    Quote Originally Posted by Element
    Woah, there. It wasn't a diss or anything man. I was just pointing it out cause you said it was faster and for me it wasn't.
    I know, I didn't mean for it to come off as seriously offended, emoticon notwithstanding.

  • #12
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Element
    And I see what you mean. I like your Marek's cause well... its good if you want to be lazy and not make all the images like I did. Course I did mine like that back of the bg color, shadowing, and font.
    The main idea was to stoer the images with names as numbers. GD was added just to show that. You can easily generate the whole ascii table at once with a loop (thus not having to check if a file exists). After that it should actually be faster than the long if/else statemant especially if someone uses chars that are on the end of the if/else.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •