helraizer Posted December 4, 2007 Share Posted December 4, 2007 Hi folks, I have a dynamic image made in php with the GD2 library.. the system allows users to write/send a message which then appears on the image. ^as above. How (if at all possible) could I calculate the position of the a certain word in the text on the image? Like, find/return the X and Y values for that word? Hope that made sense, Sam Quote Link to comment https://forums.phpfreaks.com/topic/80110-calcualte-position-of-text-on-image/ Share on other sites More sharing options...
BenInBlack Posted December 4, 2007 Share Posted December 4, 2007 ???-- stupid question alert -- ??? Why on earth are you doing it in image format? ???-- stupid question alert -- ??? Man that must take forever, and creating and image is one thing, trying to read data out of it, i doubt you could, due to the randomness of the background, I mean, if it was all white back ground and you used mono spaced font, you might be able to create a library of each letter, and then take that letter image and compare it to the big image you create in gd. Why dont you just use a background with text on it Quote Link to comment https://forums.phpfreaks.com/topic/80110-calcualte-position-of-text-on-image/#findComment-406024 Share on other sites More sharing options...
helraizer Posted December 4, 2007 Author Share Posted December 4, 2007 ???-- stupid question alert -- ??? Why on earth are you doing it in image format? ???-- stupid question alert -- ??? Why dont you just use a background with text on it The idea behind it was so that I could use it as a forum signature, so I can only link it with '[ img ]' tags if it is an image. Therefore if it wasn't a picture it would be useless for the purpose it was created Sam Quote Link to comment https://forums.phpfreaks.com/topic/80110-calcualte-position-of-text-on-image/#findComment-406045 Share on other sites More sharing options...
GingerRobot Posted December 4, 2007 Share Posted December 4, 2007 Why would you want to? Perhaps you could do something prior to the the text being written on the image? Quote Link to comment https://forums.phpfreaks.com/topic/80110-calcualte-position-of-text-on-image/#findComment-406048 Share on other sites More sharing options...
BenInBlack Posted December 4, 2007 Share Posted December 4, 2007 I guess im, lost, because what you are describing is creating the image to use as a forum signature, so why do you need to read the data out of the image? Quote Link to comment https://forums.phpfreaks.com/topic/80110-calcualte-position-of-text-on-image/#findComment-406056 Share on other sites More sharing options...
helraizer Posted December 4, 2007 Author Share Posted December 4, 2007 Why would you want to? Perhaps you could do something prior to the the text being written on the image? I got the inspiration from those dynamic images that say "Your IP address is xxx.xxx.xxx.xxx" - which are generated by some random site, and seem quite pointless. So this is almost like a shoutbox to use as signature, that updates everytime someone posts a message. So it isn't entirely pointless. Try it: myChatbox One problem is for the fonts, each font has a different width.. so I can easily cut that function, so the messages are all in the same font. Is there a function, or anything to decipher the x & y values of a certain word (specified) within the string? Or am I just being hopeful? @ BenInBlack: The idea was/is to find the x&y positions of certain parts of the string, like on a forum to use emoticons, and use ImageCopyMerge to add the emoticon; for instance if the user types '=)' or '' it will ImageCopyMerge an image (an emoticon) at that position where the text is '=)' or ''. I know I may be aiming a bit too high Hope that makes sense, Sam Quote Link to comment https://forums.phpfreaks.com/topic/80110-calcualte-position-of-text-on-image/#findComment-406065 Share on other sites More sharing options...
GingerRobot Posted December 4, 2007 Share Posted December 4, 2007 I think what you're after is imagettfbbox(). You specify the text, font, and angle, and it returns an array of points showing the dimensions. Here's a snippet from the code that generates my signiture using the above function: $size = imagettfbbox(8,0,$font,$v); $width = $size[2] - $size[0]; while($width > 250){ $v = substr($v,0,strlen($v)-4).'...'; $size = imagettfbbox(8,0,$font,$v); $width = $size[2] - $size[0]; } The idea is to keep shortening the text until it fits into the space for it. Quote Link to comment https://forums.phpfreaks.com/topic/80110-calcualte-position-of-text-on-image/#findComment-406112 Share on other sites More sharing options...
helraizer Posted December 6, 2007 Author Share Posted December 6, 2007 I think what you're after is imagettfbbox(). You specify the text, font, and angle, and it returns an array of points showing the dimensions. Here's a snippet from the code that generates my signiture using the above function: $size = imagettfbbox(8,0,$font,$v); $width = $size[2] - $size[0]; while($width > 250){ $v = substr($v,0,strlen($v)-4).'...'; $size = imagettfbbox(8,0,$font,$v); $width = $size[2] - $size[0]; } The idea is to keep shortening the text until it fits into the space for it. Thanks GingerRobot; this (below) is my code for the image; how would I impliment the imagettfbox within this? <?php include("linesfile.php5"); $linesDataFile = new DataFile("data.line"); //$image = ImageCreate(660,240); // create the image canvas $image = ImageCreateFromPNG("background.png"); $blue = ImageColorAllocate($image, 200, 200, 255); // prepare some blueness $black = ImageColorAllocate($image, 0, 0, 0); // ... and whiteness $cur_line_y = 63; // 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 = 15; // This is how much to move down to print the next line $pagelinelimit = 12; // This is the maximum number lines of text that can be displayed ImageFill($image, 0, 0, $blue); // fill the canvas //ImageString($image, 3, 15, $cur_line_y, trim(stripslashes($wordwrapped[0])), $black); $numberOfLines = $pagelinelimit; for($i=0;$i<$numberOfLines;$i++) { $data = $linesDataFile->getReverseIterate(); if (count($data)==0) continue; $name = "[" . $data[0] . "] "; $color = $data[1]; $font = $data[2]; $line = $data[3]; $line = $name . $line; //ImageString($image, 2, $cur_line_x, $cur_line_y, trim($line), getColor($color)); imagettftext($image,10,0,$cur_line_x,$cur_line_y,getColor($color),getfont($font),trim($line)); $cur_line_y += $lineheight; } function getColor($color) { global $image; switch($color) { case "black" : return ImageColorAllocate($image, 0, 0, 0); case "white" : return ImageColorAllocate($image, 255, 255, 255); case "blue" : return ImageColorAllocate($image, 0, 0, 205); case "red" : return ImageColorAllocate($image, 255, 0, 0); case "yellow" : return ImageColorAllocate($image, 255, 255, 0); case "green" : return ImageColorAllocate($image, 0, 255, 0); case "orange" : return ImageColorAllocate($image, 255, 127, 36); case "aqua" : return ImageColorAllocate($image, 0, 255, 255); default: return ImageColorAllocate($image, 255, 255, 255); } } function getfont($font) { global $image; global $font; switch($font) { case "fixedsys" : return "fixedsys.ttf"; case "Courbd" : return "courbd.ttf"; case "arial" : return "arialbd.ttf"; case "timesnr" : return "timesbd.ttf"; case "calibri" : return "calibrib.ttf"; case "comicsans" : return "comicsans.ttf"; case "palab" : return "palab.ttf"; default: return "courbd.ttf"; } } header("Content-Type: image/png"); // tell the browser what we're gonna give it ImagePng($image); // paint the image in browser ImagePng($image, "./chatbox.png"); //export as png file ImageDestroy($image); // clean up resources ?> Sam Quote Link to comment https://forums.phpfreaks.com/topic/80110-calcualte-position-of-text-on-image/#findComment-407894 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.