469,631 Members | 1,296 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

Rendering emoticons onto image - two if statements conflicting

118 100+
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. $grins = array('=D', "=d", ":d", ":D");
  3.         foreach ($grins as $grin) {
  4.             if (preg_match("/$grin/i", $text)) {
  5.                 $_SESSION['grin'] = $grin;
  6.             }
  7.         }
  8.         $smiles = array("=)", ":)");
  9.         foreach ($smiles as $smile) {
  10.             if (preg_match("/$smile/i", $text)) {
  11.                 $_SESSION['smile'] = $smile;
  12.             }
  13.         }
  14.  
  15.  
- that works, just showing you how the sessions are created.

Showimage_a.php - only necessary code shown

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. $grin_im = imagecreatefromgif("icon_grin.gif"); //grinning emote
  4. $blark = ImageColorAllocate($grin_im, 0, 0, 0);
  5.  
  6. $smile_im = imagecreatefromgif("icon_smile.gif"); // smiley emote
  7. $blacks = imagecolorallocate($smile_im, 0, 0, 0);
  8.  
  9. $image = ImageCreateFromGIF("660x240background2.gif"); //main shoutbox image
  10. $blue = ImageColorAllocate($image, 200, 200, 255); // prepare some blueness
  11. $black = ImageColorAllocate($image, 0, 0, 0); // ... and whiteness
  12.  
  13. $cur_line_y = 60; // This stores how far down the image the current line will print
  14. $cur_line_x = 24; // This stores how far across the image the current line will print
  15. $pagecharwidth = 75; // this is the maximum length of the line before it wraps;
  16. $lineheight = 18; // This is how much to move down to print the next line
  17. $pagelinelimit = 1; // This is the maximum number lines of text that can be displayed
  18.  
  19. for ($i = 0; $i < $numberOfLines; $i++) {
  20.     while ($row = mysql_fetch_array($result)) {
  21.  
  22. //code for putting shouts onto image is here - irrelevant for this purpose
  23.  
  24. $cur_line_y += $lineheight;
  25.  
  26.         if (isset($_SESSION['grin'])) {
  27.  
  28.             $grin = $_SESSION['grin'];
  29.             $pos = strpos($line, $grin);
  30.  
  31.             if ($font == "palab") {
  32.                 $post = $pos * 5.6;
  33.             } elseif ($font == "comicsans") {
  34.                 $post = $pos * 6.7;
  35.             } else {
  36.             }
  37.  
  38.             if (preg_match("/$grin/i", $line)) {
  39.  
  40.  
  41.                 imagecopymerge($image, $grin_im, ($cur_line_x + $post), ($cur_line_y - 30), 0, 0,
  42.                     15, 15, 100);
  43.  
  44.             }
  45.  
  46.         }
  47.  
  48.  if (isset($_SESSION['smile'])) { // from here
  49.  
  50.             $smiles = $_SESSION['smile'];
  51.             $smile_pos = strpos($line, $smiles);
  52.  
  53.             if ($font == "palab") {
  54.                 $posts = $smile_pos * 5.6;
  55.             } elseif ($font == "comicsans") { // not mono-spaced fonts
  56.                 $posts = $smile_pos * 6.7;
  57.             } else {
  58.             }
  59.  
  60.  
  61.             if (preg_match("/$smiles/i", $line)) {
  62.  
  63.                 imagecopymerge($image, $smile_im, ($cur_line_x + $posts), ($cur_line_y - 30), 0,
  64.                     0, 15, 15, 100);
  65.             }
  66.         } // to here
  67.  
  68.     } //end while 
  69.  
  70.  
  71. } // end for
  72.  
  73.  
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
May 30 '08 #1
1 1664
pbmods
5,821 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. $grins = array('=d', ':d');
  2. foreach( $grins as $grin )
  3. {
  4.   $pos = stripos($text, $grin);
  5.   if( $pos !== false )
  6.   {
  7.     /** Try using a local, rather than a session, var so that the value doesn't get saved. */
  8.     $posGrin = $pos;
  9.     break;
  10.   }
  11. }
  12.  
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.
Jun 2 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by news | last post: by
2 posts views Thread by Vyas Bharghava | last post: by
reply views Thread by Patrick Olurotimi Ige | last post: by
5 posts views Thread by Dave A | last post: by
5 posts views Thread by Roderik | last post: by
7 posts views Thread by dave | last post: by
omerbutt
6 posts views Thread by omerbutt | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.