Rendering emoticons onto image - two if statements conflicting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • helraizer1
    New Member
    • Mar 2007
    • 118

    Rendering emoticons onto image - two if statements conflicting

    Hi folks,

    I have an image based shoutbox which I am currently implementing emoticons onto. I have a way now for the emoticons to appear where the :D or =) etc. is on the image, but for some reason they seem to be conflicting. I shall try to explain.


    chatbox.php - only necessary code shown
    [code=php]

    $grins = array('=D', "=d", ":d", ":D");
    foreach ($grins as $grin) {
    if (preg_match("/$grin/i", $text)) {
    $_SESSION['grin'] = $grin;
    }
    }
    $smiles = array("=)", ":)");
    foreach ($smiles as $smile) {
    if (preg_match("/$smile/i", $text)) {
    $_SESSION['smile'] = $smile;
    }
    }

    [/code] - that works, just showing you how the sessions are created.

    Showimage_a.php - only necessary code shown

    [code=php]


    $grin_im = imagecreatefrom gif("icon_grin. gif"); //grinning emote
    $blark = ImageColorAlloc ate($grin_im, 0, 0, 0);

    $smile_im = imagecreatefrom gif("icon_smile .gif"); // smiley emote
    $blacks = imagecoloralloc ate($smile_im, 0, 0, 0);

    $image = ImageCreateFrom GIF("660x240bac kground2.gif"); //main shoutbox image
    $blue = ImageColorAlloc ate($image, 200, 200, 255); // prepare some blueness
    $black = ImageColorAlloc ate($image, 0, 0, 0); // ... and whiteness

    $cur_line_y = 60; // This stores how far down the image the current line will print
    $cur_line_x = 24; // This stores how far across the image the current line will print
    $pagecharwidth = 75; // this is the maximum length of the line before it wraps;
    $lineheight = 18; // This is how much to move down to print the next line
    $pagelinelimit = 1; // This is the maximum number lines of text that can be displayed

    for ($i = 0; $i < $numberOfLines; $i++) {
    while ($row = mysql_fetch_arr ay($result)) {

    //code for putting shouts onto image is here - irrelevant for this purpose

    $cur_line_y += $lineheight;

    if (isset($_SESSIO N['grin'])) {

    $grin = $_SESSION['grin'];
    $pos = strpos($line, $grin);

    if ($font == "palab") {
    $post = $pos * 5.6;
    } elseif ($font == "comicsans" ) {
    $post = $pos * 6.7;
    } else {
    }

    if (preg_match("/$grin/i", $line)) {


    imagecopymerge( $image, $grin_im, ($cur_line_x + $post), ($cur_line_y - 30), 0, 0,
    15, 15, 100);

    }

    }

    if (isset($_SESSIO N['smile'])) { // from here

    $smiles = $_SESSION['smile'];
    $smile_pos = strpos($line, $smiles);

    if ($font == "palab") {
    $posts = $smile_pos * 5.6;
    } elseif ($font == "comicsans" ) { // not mono-spaced fonts
    $posts = $smile_pos * 6.7;
    } else {
    }


    if (preg_match("/$smiles/i", $line)) {

    imagecopymerge( $image, $smile_im, ($cur_line_x + $posts), ($cur_line_y - 30), 0,
    0, 15, 15, 100);
    }
    } // to here

    } //end while


    } // end for

    [/code]

    So what it's supposed to do is if ':D' '=D' ':d' '=d' (emoticon string) are present in the string then it adds the image 'icon_grin.gif' to the main image over where that emoticon string is found. It should also add 'icon_smile.gif ' to the main image where :) or =) is found in the string. The thing is that if I comment out the bits inbetween 'from here' and 'to here' in that code, then the icon_grin is added to where :D =D :d or =d is found but if I uncomment it then icon_smile is added to where :D =D :d or =d are, instead of :) =).

    Can you see where the problem is arising?
    I have given all relevant code and can't see why they are conflicting like that..

    Thanks,
    Sam

    code=php tags don't appear to be working. =\ - Sorry
    Last edited by helraizer1; May 30 '08, 02:42 PM. Reason: changed title
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Sam.

    I noticed that you are using preg_match() so that you can do a case-insensitive search - but then you put both cases in your pattern array.

    You might find this to be more efficient:
    [code=php]
    $grins = array('=d', ':d');
    foreach( $grins as $grin )
    {
    $pos = stripos($text, $grin);
    if( $pos !== false )
    {
    /** Try using a local, rather than a session, var so that the value doesn't get saved. */
    $posGrin = $pos;
    break;
    }
    }
    [/code]

    Note also the break, since your script only processes one smiley per type, you can stop searching once you've found a smiley.

    I'm curious, as noted in the comment above, whether the fact that you're using a session variable has anything to do with the issue you're experiencing.

    Comment

    Working...