By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
461,998 Members | 488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 461,998 IT Pros & Developers. It's quick & easy.

Finding X & Y co-ords of text on an image

100+
P: 118
Hi all,

I have a dynamic image that picks out data from a dynamically created .line file.

showimage.php(5)
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("linesfile.php5");
  3. $linesDataFile = new DataFile("data.line");
  4.  
  5. //$image = ImageCreate(660,240); // create the image canvas
  6. $image = ImageCreateFromPNG("background.png");
  7. $blue = ImageColorAllocate($image, 200, 200, 255); // prepare some blueness
  8. $black = ImageColorAllocate($image, 0, 0, 0); // ... and whiteness
  9.  
  10. $cur_line_y = 63;  // This stores how far down the image the current line will print
  11. $cur_line_x = 24; // This stores how far across the image the current line will print
  12. $pagecharwidth = 75; // this is the maximum length of the line before it wraps;
  13. $lineheight = 15; // This is how much to move down to print the next line
  14. $pagelinelimit = 12; // This is the maximum number lines of text that can be displayed
  15.  
  16. ImageFill($image, 0, 0, $blue); // fill the canvas
  17.  
  18. //ImageString($image, 3, 15, $cur_line_y, trim(stripslashes($wordwrapped[0])), $black);
  19.  
  20. $numberOfLines = $pagelinelimit;
  21.  
  22.  
  23.  
  24. for($i=0;$i<$numberOfLines;$i++) {
  25.     $data = $linesDataFile->getReverseIterate();
  26.     if (count($data)==0) continue;
  27.     $name = "[" . $data[0] . "] ";
  28.     $color = $data[1];
  29.     $font = $data[2];
  30.     $line = $data[3];
  31.  
  32.     $line = $name . $line;
  33.  
  34.  
  35.  
  36. //ImageString($image, 2, $cur_line_x, $cur_line_y, trim($line), getColor($color));
  37.  
  38.  
  39.             imagettftext($image,10,0,$cur_line_x,$cur_line_y,getColor($color),getfont($font),trim($line));
  40.  
  41.     $cur_line_y += $lineheight;
  42.  
  43. }
  44.  
  45. function getColor($color) {
  46.     global $image;
  47.  
  48.     switch($color) {
  49.         case "black" :
  50.             return ImageColorAllocate($image, 0, 0, 0); 
  51.         case "white" :
  52.             return ImageColorAllocate($image, 255, 255, 255); 
  53.         case "blue" :
  54.             return ImageColorAllocate($image, 0, 0, 205); 
  55.         case "red" :      
  56.             return ImageColorAllocate($image, 255, 0, 0); 
  57.         case "yellow" :
  58.             return ImageColorAllocate($image, 255, 255, 0); 
  59.         case "green" :
  60.             return ImageColorAllocate($image, 0, 255, 0); 
  61.                 case "orange" :
  62.                         return ImageColorAllocate($image, 255, 127, 36);
  63.                 case "aqua" :
  64.                         return ImageColorAllocate($image, 0, 255, 255);
  65.         default: 
  66.             return ImageColorAllocate($image, 255, 255, 255); 
  67.  
  68.     }
  69. }
  70.  
  71. function getfont($font) {
  72.     global $image;
  73.     global $font;
  74.  
  75.     switch($font) {
  76.         case "fixedsys" :
  77.             return "fixedsys.ttf";
  78.         case "Courbd" :
  79.             return "courbd.ttf";
  80.         case "arial" :
  81.             return "arialbd.ttf";
  82.         case "timesnr" :
  83.             return "timesbd.ttf";
  84.         case "calibri" :
  85.             return "calibrib.ttf";
  86.         case "comicsans" :
  87.             return "comicsans.ttf";
  88.         case "palab" :
  89.             return "palab.ttf";
  90.         default:
  91.             return "courbd.ttf";
  92.     }
  93. }
  94.  
  95.  
  96. header("Content-Type: image/png"); // tell the browser what we're gonna give it
  97. ImagePng($image); // paint the image in browser
  98. ImagePng($image, "./chatbox.png"); //export as png file
  99. ImageDestroy($image); // clean up resources
  100. ?>
the php in Index.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.             include("linesfile.php5");
  3.             $filename = "data.line";
  4.             set_magic_quotes_runtime(0);
  5.             if ($_POST['submit']) {
  6.             // grab the inputted text
  7.             $text = stripcslashes($_POST['input'] . "\n");
  8.             $username = stripslashes($_POST['username']);
  9.             $color = $_POST['color'];
  10.             $font = $_POST['font'];
  11.             $ip = $_SERVER['REMOTE_ADDR'] . "\n";
  12.             $_SESSION['username'] = $username;
  13.             $_SESSION['color'] = $color;
  14.  
  15.  
  16.                     $dirty = array('many', 'bad', 'words', 'in', 'here'); // I took out the real array just to keep the thread 'clean'.
  17.  
  18.                         foreach($dirty AS $bad_word){
  19.                         $text = preg_replace("/$bad_word/i","****", $text);
  20.                       }
  21.             $data[] ="\n".$username;
  22.             $data[] =trim($color);
  23.             $data[] =trim($font);
  24.             $data[] =trim($text);
  25.  
  26.  
  27.             $datafile = new DataFile($filename);
  28.             if(!$datafile->writeNewLine($data)) die("Error writing to file");
  29. }
  30.           ?>
my plan/idea is to impliment smilies into the image. If the user enters data such as 'Yay, it works!! =D' it will replace '=D' with a .gif image.

To do this I believe I will use Imagecopymerge(). but how would I go about getting the X & Y co-ords for the text that needs changing, in this case '=D'? Baring in mind that everytime a new post is submitted the previous line is moved down.

How would I do this?

Thanks,
Sam

myChatbox - the site, just so you get a feel for the image and input
Nov 1 '07 #1
Share this Question
Share on Google+
1 Reply

pbmods
Expert 5K+
P: 5,821
Heya, Sam.

No good way to do it that I know of. I think you're hitting the limit of what a graphical chatbox can do.
Nov 27 '07 #2

Post your reply

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