GD intermittent color problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • helraizer1
    New Member
    • Mar 2007
    • 118

    GD intermittent color problem

    Hey folks, yet again.

    [php]


    if ($back == "m") //$back is a value pulled from a db; that works perfectly.
    {
    $wid = $rowing['width'];
    $hei = $rowing['height'];
    createthumb("66 0x240background 2.gif", "./user/" . $user . "_back.gif" , $wid, $hei);

    $image = imagecreatefrom gif("./user/" . $user . "_back.gif" );
    $blue = ImageColorAlloc ate($image, 200, 200, 255); // prepare some blueness
    $black = ImageColorAlloc ate($image, 0, 0, 0); // ... and whiteness
    $im = ImageCreate(300 , 35);
    $black_a = imagecoloralloc ate($im, 0, 0, 0);
    $white_a = imagecolortrans parent($im, $black_a);
    $white = ImageColorAlloc ate($im, 255, 255, 255);
    ImageFill($im, 0, 0, $white_a);
    Imagettftext($i m, 15, 0, 10, 30, $white, $font, $user . "'s Shoutbox");

    $cur_line_y = 65; // This stores how far down the image the current line will print

    $postperpage = 11;
    }
    elseif ($back == "u")
    {
    $wid = $rowing['width'];
    $hei = $rowing['height'];



    // createthumb("./user/".$user."_img.g if", "./user/" . $user . "_backim.gi f", $wid, $hei);

    imagecreatefrom gif("./user/" . $user . "_img.gif") ;



    $images = imagecreate($wi d, $hei);

    $imge = imagecreatefrom gif("./user/" . $user . "_backim.gi f");

    imagecopymerge( $images, $imge, 0, 0, 0, 0, $wid, $hei, 20);

    imagegif($image s, "./user/" . $user . "_back_trans.gi f");

    $image = imagecreatefrom gif("./user/" . $user . "_back_trans.gi f");

    $im = ImageCreate(300 , 35);
    $black_a = imagecoloralloc ate($im, 0, 0, 0);
    $white_a = imagecolortrans parent($im, $black_a);
    $white = ImageColorAlloc ate($im, 255, 255, 255);
    ImageFill($im, 0, 0, $white_a);
    Imagettftext($i m, 15, 0, 10, 30, $white, $font, $user . "'s Shoutbox");

    $cur_line_y = 74; // This stores how far down the image the current line will print
    $postperpage = 10;
    }

    $sql = ""; // no need for actuall SQL statement - it works.

    $result = mysql_query($sq l) or die("error in sqllls: " . mysql_error());


    $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; // How many of each comment appears per page.


    //ImageString($im age, 3, 15, $cur_line_y, trim(stripslash es($wordwrapped[0])), $black);

    $numberOfLines = $pagelinelimit;

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

    $name = "[" . htmlspecialchar s_decode($row['username']) . "] "; //it works - returns username
    $font = $row['font']; //it works - renders the colour
    $color = $row['color']; //it works - changes the font
    $line = htmlspecialchar s_decode($row['comment']); //it works, posts the shout

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

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

    }

    //code for emoticons.

    //add shouts to image!
    if ($back == "m" )
    {
    if ($color == "white" || $color == "yellow" || $color == "green" || $color ==
    "orange" || $color == "aqua")
    {
    Imagettftext($i mage, 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($i mage, 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($i mage, 10, 0, $cur_line_x, $cur_line_y, getColor($color ), getfont
    ($font), trim($line));


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


    }

    }
    elseif ($back == "w" )
    {

    $blac = imagecoloralloc ate($image, 0, 0, 0);

    if ($color == "black" || $color == "purple" || $color == "blue" || $color ==
    "emerald" || $color == "red")
    {
    Imagettftext($i mage, 10, 0, $cur_line_x, $cur_line_y, getColor($color ), getfont
    ($font), trim($line));


    }
    elseif ($color != "black" || $color != "purple" || $color != "blue" || $color !=
    "emerald" || $color != "red")
    {
    Imagettftext($i mage, 10, 0, $cur_line_x, $cur_line_y, $blac, getfont($font),
    trim($line));


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


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


    }

    }


    [/php]

    Depending on the values of $back, the background of the image changes and the font colours also change. This code works so far.

    Problem code

    [php]
    elseif($back == "u")
    {
    if ($color == "white" || $color == "yellow" || $color == "green" || $color ==
    "orange" || $color == "aqua")
    {
    Imagettftext($i mage, 10, 0, $cur_line_x, $cur_line_y, getColor($color ), getfont
    ($font), trim($line));


    }elseif ($color != "black" || $color != "purple" || $color != "blue" || $color !=
    "emerald" || $color != "red")
    {
    Imagettftext($i mage, 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($i mage, 10, 0, $cur_line_x, $cur_line_y, getColor($color ), getfont
    ($font), trim($line));


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


    }
    }

    $cur_line_y += $lineheight;


    }
    }
    [/php]

    The problem is that when $back == "u", for the first two comments the colours are normal then after that the older comments turn black; the newest comment is in colour but it also changes the colour of the $im that says "User's Shoutbox", which is created as a seperate image.

    I can't, for the life of me, see the problem.. can you?

    An example: $back == "u" because it's an uploaded background. The oldest comment was white, then I added the Yellow comment, the white one turned black.




    Hope that makes sense; if you need more code, just ask. =)
    Sam
  • helraizer1
    New Member
    • Mar 2007
    • 118

    #2
    *bump*

    Any one got any ideas?

    The process of the background image is that when they upload it as either jpg, gif or png; it is converted to and saved a gif file.

    So I uploaded this image



    which then is pulled into the code where $back == "u" the first time. It is the saved to the user's specified dimensions. So creates this (700x290, in this case).



    It is the merged onto a gd made image at the same dimensions, this time at 20% transparency. Which gives this:



    That final, darkened image is what the shouts are written onto. Is there any way this process could be affecting the colours? I can't imagine it can because this process happens seperately each time, before the shouts are written on.

    Sam


    EDIT: Fixed it!

    Used imagecreatetrue color($wid, $hei); instead of imagecreate(). It works perfectly now.

    Comment

    • helraizer1
      New Member
      • Mar 2007
      • 118

      #3
      The imagecreatetrue colour only solved it for a little while, allowing one more comment of colour than before, now the comments do the same thing.

      Any ideas why when a new comment is posted, the imagecoloralloc ate fails and the comments turn black?

      supposed to be like:

      http://www.helraizer.co.uk/mychatbox/user/helraizer.gif - plain background, colours work!

      but ends up like:

      http://www.helraizer.co.uk/mychatbox/user/lucifer.gif - detailed background, colours only work part of the time.

      Please reply if you have any theories.

      Sam

      Comment

      • helraizer1
        New Member
        • Mar 2007
        • 118

        #4
        Fixed it again. xD

        Used the same technique but this time I've made it as png. That way it doesn't run out of colours on the pallete and gives the same result.

        Sam

        Comment

        Working...