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 plan 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

Ok, more information about the problem.

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, the image then takes this information and makes an image from it.

In the main page there is an dynamically created image.

showimage.php

<?php
$user_id = mysql_real_escape_string(htmlspecialchars($_GET['uid']));

$sqls = "SELECT username FROM user WHERE user_id='$user_id'";

$results = mysql_query($sqls) or die("error in  sqls: " . mysql_error());
$rows = mysql_fetch_array($results);

$counts = mysql_num_rows($results);
$font = "palab.ttf";
    $user = $rows['username'];
    $_SESSION['user'] = $user;


$im = ImageCreate(230, 35);
$black_a = imagecolorallocate($im, 0, 0, 0);
$white_a = imagecolortransparent($im, $black);
$white = ImageColorAllocate($im, 255, 255, 255);
ImageFill($im, 0, 0, $white_a);
Imagettftext($im, 15, 0, 10, 30, $white, $font, $user . "'s Shoutbox");

$grin = imagecreatefromgif("icon_lol.gif");
$blark = ImageColorAllocate($grin, 0, 0, 0);


//$image = ImageCreate(660,240); // create the image canvas
$image = ImageCreateFromGIF("660x240background2.gif");
$blue = ImageColorAllocate($image, 200, 200, 255); // prepare some blueness
$black = ImageColorAllocate($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


//ImageString($image, 3, 15, $cur_line_y, trim(stripslashes($wordwrapped[0])), $black);

$numberOfLines = $pagelinelimit;


$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";

$result = mysql_query($sql) or die("error in  sql: " . mysql_error());

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


	 $name = "[" . htmlspecialchars_decode($row['username']) . "] ";
        $font = $row['font'];
        $color = $row['color'];
        $line = htmlspecialchars_decode($row['comment']);
	


        if ($name == "[] ") {
            $name = "";

            $line = $name . $line;
        } else {

            $line = $name . $line;
        }

        if ($color == "white" || $color == "yellow" || $color == "green" || $color ==
            "orange" || $color == "aqua") {
            Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, getColor($color), getfont
                ($font), trim($line));


        } elseif ($color != "white" || $color != "yellow" || $color != "green" || $color !=
        "orange" || $color != "aqua") {
            Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, $white, getfont($font),
                trim($line));


        } elseif ($font == "fixedsys" || $font == "Courbd" || $font == "arial" || $font ==
        "timesnr" || $font == "calibri" || $font == "comicsans" || $font == "palab") {
            Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, getColor($color), getfont
                ($font), trim($line));


        } else {
            Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, $white, "courbd.ttf", trim
                ($line));


        }

        $cur_line_y += $lineheight;


    }


}


function getColor($color)
{
    global $image;

    switch ($color) {
        case "white":
            return ImageColorAllocate($image, 255, 255, 255);
        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, 0);

    }
}


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 "calibri":
            return "calibrib.ttf";
        case "comicsans":
            return "comicsans.ttf";
        case "palab":
            return "palab.ttf";
        default:
            return "palab.ttf";
    }
}

ImageCopyMerge($image, $im, 20, 0, 0, 0, 230, 50, 100);

header("Content-Type: image/gif"); // tell the browser what we're gonna give it
imagegif($image); // paint the image in browser
imagegif($image, "user/" . $user . ".gif"); //export as gif file
ImageDestroy($image); // clean up resources

?>

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;

For this in shoutbox.php:

<?php // tag only for highlighting
$smiles = array('=D', "=d", ":d", ":D");
 foreach($smiles as $smile) {
if (preg_match("/$smile/i", $text)) {
	$_SESSION['smile'] = $smile;
}
}
?>

I attempted to use, in showimage.php

<?php
if (isset($_SESSION['smile'])) {

    $smile = $_SESSION['smile'];

    if (preg_match("/$smile/i", $line)) {
        list(, , , , , , ,$left, $top) = imagettfbbox(10, 0, getfont($font), $smile);

        imagecopymerge($image, $grin, $left, $top, 0, 0, 15, 15, 100);
    }

}
?>

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

This article has been dead for over six months. Start a new discussion instead.