Click to See Complete Forum and Search --> : imagestring allocating different colors to different lines of text not working


BWWebDesigns
02-27-2008, 02:44 PM
Im using the following image code to generate an image on the fly

It usess several lines of text and i want text to be different colors dependant on certain things

However when i try and alocate text color to more than one of the text elements they all go a horrible light brown color and black alternating colors

brown
black
brown
black

etc

Code im using is

$pic = "donation-meter".$imgsrc.".gif";

$im = imagecreatefromgif($pic); /* Attempt to open */
//$bg = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

$goal = number_format ($goal, 2);
$string = "Goal : ".$cursym.$goal;
if($new_value2>=$goal) {
$redgreena = imagecolorallocate($im, 0, 255, 0);
} else {
$redgreena = imagecolorallocate($im, 255, 0, 0);
}
$string2 = "Donated : ".$cursym.$new_value2;
$red = imagecolorallocate($im, 255, 0, 0);
$string3 = "PP Fees : ".$cursym.$fees;
$left = $goal-($new_value-$fees);
$left = number_format ($left, 2);
if(eregi("-",$left)) {
$redgreenb = imagecolorallocate($im, 0, 255, 0);
} else {
$redgreenb = imagecolorallocate($im, 255, 0, 0);
}

$string4 = "Left : ".$cursym.$left;
$string5 = $res."% Of Our Goal";

$px = (imagesx($im) - 18 * strlen($string)) / 2;
imagestring($im, 2, 13, 35, $string, $black);
imagestring($im, 2, 13, 55, $string2, $redgreena);
imagestring($im, 2, 13, 75, $string3, $red);
imagestring($im, 2, 13, 95, $string4, $redgreenb);
imagestring($im, 2, $px, 115, $string5, $textcolor);
imagegif($im);
imagedestroy($im);

Depending on certain situations based on the value of a number to appear within a particular line of text the color of that text will be red or green

The rest of the text should be black

Can anyone offer advice on why it is not doing this

I have read many sites and my code seems to be right

BWWebDesigns
02-28-2008, 04:48 PM
Ok so i modified my code a little to make it better how i wish it to be but still havign problems with teh colors being brown, when i set something as black it works but everything i have set as red green or blue always comes out a light beige brown color

$imgsrc is a number generated based on a value calculated earlier in thes cript

There are 100 images each slightly different representing 0 to 100 % on a meter scale
$pic = "donation-meter".$imgsrc.".gif";

$im = imagecreatefromgif($pic); /* Attempt to open */
//$bg = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 0, 0);
$green = imagecolorallocate($im, 255, 0, 0);
$blue = imagecolorallocate($im, 0, 0, 255);
$goal = number_format ($goal, 2);
$stringa = "Goal : ";
$stringb = $cursym.$goal;
if($new_value2>=$goal) {
$redgreena = imagecolorallocate($im, 0, 255, 0);
} else {
$redgreena = imagecolorallocate($im, 255, 0, 0);
}
$string2a = "Donated : ";
$string2b = $cursym.$new_value2;
$red = imagecolorallocate($im, 255, 0, 0);
$string3a = "PayPal";
$string3b = "Fees";
$string3c = ":";
$string3d = $cursym.$fees;
$left = $goal-($new_value-$fees);
$left = number_format ($left, 2);
if(eregi("-",$left)) {
$redgreenb = imagecolorallocate($im, 0, 255, 0);
} else {
$redgreenb = imagecolorallocate($im, 255, 0, 0);
}
$string4a = "Left : ";
$string4b = $cursym.$left;
$string5a = $res."%";
$string5b = " Of Our Goal";
if($res<100) {
$redgreenc = imagecolorallocate($im, 255, 0, 0);
} else {
$redgreenc = imagecolorallocate($im, 0, 255, 0);
}

if($res<2) {
$string6 = "Donations needed!";
} elseif(($res<10) && ($res>=2)) {
$string6 = "Slowly Getting There!";
} elseif(($res<50) && ($res>=10)) {
$string6 = "Almost Half Way!";
} elseif(($res<70) && ($res>=50)) {
$string6 = "Half Way There!";
} elseif(($res<100) && ($res>=70)) {
$string6 = "So Close Keep Going!";
} elseif($res>=100) {
$string6 = "We Did It YAY!";
}

$px = (imagesx($im) - 21 * strlen($string5b)) / 2;
$px2 = (imagesx($im) - 17 * strlen($string6)) / 2;
imagestring($im, 2, 13, 35, $stringa, $black);
imagestring($im, 2, 55, 35, $stringb, $green);
imagestring($im, 2, 13, 55, $string2a, $black);
imagestring($im, 2, 72, 55, $string2b, $redgreena);
imagestring($im, 2, 13, 75, $string3a, $black);
imagestring($im, 2, 13, 88, $string3b, $black);
imagestring($im, 2, 60, 78, $string3c, $black);
imagestring($im, 2, 72, 78, $string3d, $red);
imagestring($im, 2, 13, 108, $string4a, $black);
imagestring($im, 2, 54, 108, $string4b, $redgreenb);
imagestring($im, 2, $px, 128, $string5a, $redgreenc);
imagestring($im, 2, 44, 128, $string5b, $black);
imagestring($im, 2, $px2, 148, $string6, $blue);
imagegif($im);
imagedestroy($im);

See my testing here

http://www.paypaldonationmeter.com/test.php

I cant for the life of me figure out why when i only use one color that color works fine but when i try and use more than one only black seems to work and the rest turn a nasty light brown

BWWebDesigns
02-28-2008, 05:42 PM
think i figured it out, only seems to work when i use imges that are png and change all the code to be png instead of gif

Now it works

MrCoder
02-29-2008, 03:43 AM
Why not just use CSS?