473,383 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

getting X, Y values of certain text in string on image

118 100+
Hi folks,

I have a script that pulls user comments from a database, which works fine.

It's an image based script so I'm trying to now add emoticons, for such as =D or =)

To do this I was planning to use list(,,,,,,$left,$top) for the imagettfbox of where =D or =) can be found in the text..

How would I get php to return the location of where the '=D' is in the text? Str_split wouldn't work well enough.

How could I then, from getting the location of it, get it in a imagettfbbox to get the top left X and Y values for it in relation to the text?

Hope that makes sense.

Sam
Apr 17 '08 #1
2 1393
TheServant
1,168 Expert 1GB
Hi folks,

I have a script that pulls user comments from a database, which works fine.

It's an image based script so I'm trying to now add emoticons, for such as =D or =)

To do this I was planning to use list(,,,,,,$left,$top) for the imagettfbox of where =D or =) can be found in the text..

How would I get php to return the location of where the '=D' is in the text? Str_split wouldn't work well enough.

How could I then, from getting the location of it, get it in a imagettfbbox to get the top left X and Y values for it in relation to the text?

Hope that makes sense.

Sam
PHP will only work if you submit the post. ie. if you type :) it will not automatically change to a smiley until you submit it. You can do it, but that would be javascript. If you do still want to use php, please post some code so we can help you with YOUR code, rather than making our own for you. That's what we do here, fix code, not make it!
Apr 17 '08 #2
helraizer1
118 100+
Point taken, here is the code I have.

Explanation: I have a main page for my shoutbox, which contains a form for a user to input a username, message, font(select menu), colour(select), which posts to itself and adds the information they just posted to the database.

In the main page there is an dynamically created image.

showimage.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $user_id = mysql_real_escape_string(htmlspecialchars($_GET['uid']));
  3.  
  4. $sqls = "SELECT username FROM user WHERE user_id='$user_id'";
  5.  
  6. $results = mysql_query($sqls) or die("error in  sqls: " . mysql_error());
  7. $rows = mysql_fetch_array($results);
  8.  
  9. $counts = mysql_num_rows($results);
  10. $font = "palab.ttf";
  11.     $user = $rows['username'];
  12.     $_SESSION['user'] = $user;
  13.  
  14.  
  15. $im = ImageCreate(230, 35);
  16. $black_a = imagecolorallocate($im, 0, 0, 0);
  17. $white_a = imagecolortransparent($im, $black);
  18. $white = ImageColorAllocate($im, 255, 255, 255);
  19. ImageFill($im, 0, 0, $white_a);
  20. Imagettftext($im, 15, 0, 10, 30, $white, $font, $user . "'s Shoutbox");
  21.  
  22. $grin = imagecreatefromgif("icon_lol.gif");
  23. $blark = ImageColorAllocate($grin, 0, 0, 0);
  24.  
  25.  
  26. //$image = ImageCreate(660,240); // create the image canvas
  27. $image = ImageCreateFromGIF("660x240background2.gif");
  28. $blue = ImageColorAllocate($image, 200, 200, 255); // prepare some blueness
  29. $black = ImageColorAllocate($image, 0, 0, 0); // ... and whiteness
  30.  
  31. $cur_line_y = 60; // This stores how far down the image the current line will print
  32. $cur_line_x = 24; // This stores how far across the image the current line will print
  33. $pagecharwidth = 75; // this is the maximum length of the line before it wraps;
  34. $lineheight = 18; // This is how much to move down to print the next line
  35. $pagelinelimit = 1; // This is the maximum number lines of text that can be displayed
  36.  
  37.  
  38. //ImageString($image, 3, 15, $cur_line_y, trim(stripslashes($wordwrapped[0])), $black);
  39.  
  40. $numberOfLines = $pagelinelimit;
  41.  
  42.  
  43. $sql = "SELECT * FROM user INNER JOIN tblcomments ON tblcomments.user_id=user.user_id INNER JOIN tblattributes ON tblcomments.id = tblattributes.id INNER JOIN tblip ON tblattributes.id = tblip.id  WHERE tblcomments.user_id='$user_id' AND tblattributes.user_id='$user_id' AND tblip.user_id='$user_id' ORDER BY tblcomments.`id` DESC LIMIT 0,10";
  44.  
  45. $result = mysql_query($sql) or die("error in  sql: " . mysql_error());
  46.  
  47. for ($i = 0; $i < $numberOfLines; $i++) {
  48.     while ($row = mysql_fetch_array($result)) {
  49.  
  50.  
  51.      $name = "[" . htmlspecialchars_decode($row['username']) . "] ";
  52.         $font = $row['font'];
  53.         $color = $row['color'];
  54.         $line = htmlspecialchars_decode($row['comment']);
  55.  
  56.  
  57.  
  58.         if ($name == "[] ") {
  59.             $name = "";
  60.  
  61.             $line = $name . $line;
  62.         } else {
  63.  
  64.             $line = $name . $line;
  65.         }
  66.  
  67.         if ($color == "white" || $color == "yellow" || $color == "green" || $color ==
  68.             "orange" || $color == "aqua") {
  69.             Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, getColor($color), getfont
  70.                 ($font), trim($line));
  71.  
  72.  
  73.         } elseif ($color != "white" || $color != "yellow" || $color != "green" || $color !=
  74.         "orange" || $color != "aqua") {
  75.             Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, $white, getfont($font),
  76.                 trim($line));
  77.  
  78.  
  79.         } elseif ($font == "fixedsys" || $font == "Courbd" || $font == "arial" || $font ==
  80.         "timesnr" || $font == "calibri" || $font == "comicsans" || $font == "palab") {
  81.             Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, getColor($color), getfont
  82.                 ($font), trim($line));
  83.  
  84.  
  85.         } else {
  86.             Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, $white, "courbd.ttf", trim
  87.                 ($line));
  88.  
  89.  
  90.         }
  91.  
  92.         $cur_line_y += $lineheight;
  93.  
  94.  
  95.     }
  96.  
  97.  
  98. }
  99.  
  100.  
  101. function getColor($color)
  102. {
  103.     global $image;
  104.  
  105.     switch ($color) {
  106.         case "white":
  107.             return ImageColorAllocate($image, 255, 255, 255);
  108.         case "yellow":
  109.             return ImageColorAllocate($image, 255, 255, 0);
  110.         case "green":
  111.             return ImageColorAllocate($image, 0, 255, 0);
  112.         case "orange":
  113.             return ImageColorAllocate($image, 255, 127, 36);
  114.         case "aqua":
  115.             return ImageColorAllocate($image, 0, 255, 255);
  116.         default:
  117.             return ImageColorAllocate($image, 255, 255, 0);
  118.  
  119.     }
  120. }
  121.  
  122.  
  123. function getfont($font)
  124. {
  125.     global $image;
  126.     global $font;
  127.  
  128.     switch ($font) {
  129.         case "fixedsys":
  130.             return "fixedsys.ttf";
  131.         case "Courbd":
  132.             return "courbd.ttf";
  133.         case "arial":
  134.             return "arialbd.ttf";
  135.         case "calibri":
  136.             return "calibrib.ttf";
  137.         case "comicsans":
  138.             return "comicsans.ttf";
  139.         case "palab":
  140.             return "palab.ttf";
  141.         default:
  142.             return "palab.ttf";
  143.     }
  144. }
  145.  
  146. ImageCopyMerge($image, $im, 20, 0, 0, 0, 230, 50, 100);
  147.  
  148. header("Content-Type: image/gif"); // tell the browser what we're gonna give it
  149. imagegif($image); // paint the image in browser
  150. imagegif($image, "user/" . $user . ".gif"); //export as gif file
  151. ImageDestroy($image); // clean up resources
  152.  
  153. ?>
  154.  
This is the code for the image, on the main page if the posted comment contains =D, :D, :d or =d a session is started $_SESSION['smile'];

For this in shoutbox.php:
Expand|Select|Wrap|Line Numbers
  1. $smiles = array('=D', "=d", ":d", ":D");
  2.  foreach($smiles as $smile) {
  3. if (preg_match("/$smile/i", $text)) {
  4.     $_SESSION['smile'] = $smile;
  5. }
  6. }
  7.  
I attempted to use, in showimage.php

Expand|Select|Wrap|Line Numbers
  1. if (isset($_SESSION['smile'])) {
  2.  
  3.     $smile = $_SESSION['smile'];
  4.  
  5.     if (preg_match("/$smile/i", $line)) {
  6.         list(, , , , , , ,$left, $top) = imagettfbbox(10, 0, getfont($font), $smile);
  7.  
  8.         imagecopymerge($image, $grin, $left, $top, 0, 0, 15, 15, 100);
  9.     }
  10.  
  11. }
  12.  
but to no avail, it didn't work; I even changed $left and $top in the imagecopymerge to 350 and 100, respectively. This would have shown if the image was being rendered ontop of the main image, but for some reason it didn't show up at all.. any ideas?

Also, will that imagettfbbox(10, 0, getfont($font), $smile) return the coordinates for the '=D' in relation to its place in the line of text? If not, how would I get that to work?

Hope that makes more sense,
Sam
Apr 17 '08 #3

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

Similar topics

21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
8
by: Alan Silver | last post by:
Hello, I have a repeater that has code like this... <ItemTemplate> <asp:CheckBox ID="chkDelete" Text="" RunAt="server"/> .... other stuff goes here </ItemTemplate> There is a button below...
29
by: Chris Riesbeck | last post by:
I have an image with a class and the class defines a clip rectangle. In Firefox 2 and 3, and Opera 9, I can access the rectangle with document.defaultView.getComputedStyle(). But that doesn't...
2
by: hgarg | last post by:
The onpaint() is getting called before calculating the required parameters by listBox1_SelectedIndexChanged function. I tried calling it at the end of this function. But of no use. Due to this issue...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.