Page 1 of 1

argh font colour not selecting right.

Posted: Sat Aug 09, 2003 4:32 pm
by toms100
Sorry if this is a bit long.
basically this script draws the text in the wrong colour. it allways ends up being brown.... (RGB 104,76,52)
the script is passed the text correctly and does everything other than colour it correct!.
the function that needs attention is colourf.
somehow it is going wrong....
i pass "red" or "blue" or "FF00FF" as $colour in this script when constructed and it just will not colour!!!.

Code: Select all

<?php
class sig {
	var $bg;
	var $text;
	var $username;
	var $xOff;
	var $yOff;
	var $font;
	var $fontsize;
	var $fontcolour;
	var $skill;
	var $image;
	
function sig($bg,$text,$username,$xOff,$yOff,$font,$fontsize,$fontcolour,$skill) {
	
	$this->bg = $bg;
	$this->text = $text;
	$this->username = str_replace(' ','_',$username);;
	$this->xOff = $xOff;
	$this->yOff = $yOff;
	$this->font = "/web/sites/296/saundersbc/www.saundersbc.f2s.com/rs/fonts/".$font;
	$this->fontsize = $fontsize;
	$this->skill = $skill;
	if ($this->bg == "") {
	$this->bg = "sig2.jpg";
	}
	$ext = explode('.',$this->bg);
	$th = count($ext);
	//$ext[$th-1] should be the extension..
	if ($ext[$th-1] == 'jpg' || $ext[$th-1] == 'JPG' || $ext[$th-1] == 'jpeg' || $ext[$th-1] == 'JPEG') {
		$this->image = imagecreatefromjpeg($this->bg);
	}
	/*if ($ext[$th-1] == 'gif' || 'GIF') {
		$this->image = imagecreatefromgif($this->bg);
	}*/
	if ($ext[$th-1] == 'png' || $ext[$th-1] == 'PNG') {
		$this->image = imagecreatefrompng($this->bg);
	}
	$this->colourf($fontcolour);
	$this->drawimage();
}

function hex2dec($input) {
	if(strlen($input) < 6 ) return(false);
 	$r = hexdec(substr($input,0,2));
 	$b = hexdec(substr($input,2,2));
 	$g = hexdec(substr($input,4,2));
 	return("$r,$b,$g");
 }

function colourf($colour) {
	switch ($colour) {
		case 'black':
		$col = "000000";
		break;
		case 'white':
		$col = "FFFFFF";
		break;
		case 'blue':
		$col = "FF0000";
		break;
		case 'red':
		$col = "00FF00";
		break;
		case 'green':
		$col = "0000FF";
		break;
		case 'yellow':
		$col = "FFFF00";
		break;
		case 'magenta':
		$col = "FF00FF";
		break;
		case 'cyan':
		$col = "00FFFF";
		break;
		case 'orange':
		$col = "FFA500";
		break;
		case 'redorange':
		$col = "FF4500";
		break;
		case 'violet':
		$col = "EE82EE";
		break;
		case 'purple' :
		$col = "A020F0";
		break;
		case 'greenblue':
		$col = "2E8B57";
		break;
		default:
		$col = $colour;	
		break;
	}
	$rgb = $this->hex2dec($col);
	if ($rgb) {
	$this->fontcolour = imagecolorallocate($this->image, $rgb['r'],$rgb['g'],$rgb['b']);
	} else {
	$this->fontcolour = imagecolorallocate($this->image, 0,0,0);
	}
}

function drawimage() {	
	/// should have done the image now
	/// get the stats
	if ($this->skill == "random") {
	$skill = rand(0,15);
	settype($skill,"int");
	$stats = $this->getstats($this->username,$skill);
	$t = 0;
	while ($stats['lvl'] = "" && $t < 10) {
		$skill = rand(0,15);
		settype($skill,"int");
		$stats = $this->getstats($this->username,$skill);
		$t++;
	}
	}
	else{
		$stats = $this->getstats($this->username,$this->skill);
	}
	if ($stats['xp'] == "") {
	$txt = "xp is NULL";
	}
	else {
	$txt = $this->textdo($this->username,$stats['lvl'],$stats['rank'],$stats['skill'],$stats['xp']);
	}
	$lines = explode('\n',$txt);
	
	
	$this->writetxt($this->xOff,$this->yOff,$lines);
	imagejpeg($this->image);
	
}

function writetxt($x,$y,$lines) {
	$cl = count($lines);
	// $cl is the number of lines to draw.
	//loop thru each line and draw.	
	$y = ($y + $this->fontsize);
	$lin = 0;
	while ($lin < $cl) {
		imagettftext( $this->image, $this->fontsize, 0,$x,$y, $this->fontcolour, $this->font, $lines[$lin]);
		$y = $y + $this->fontsize + 2;
		$lin++;
	}
}

function textdo($username,$lvl,$rank,$skill,$xp) {
	$txt = trim($this->text);
	$txt = str_replace('@USERNAME',$username,$txt);
	$txt = str_replace('@LVL',$lvl,$txt);
	$txt = str_replace('@RANK',$rank,$txt);
	$txt = str_replace('@SKILL',$skill,$txt);
	$txt = str_replace('@XP',$xp,$txt);
	return $txt;
}

function getstats($username,$category=0) { 
$webpage = "http://www.runescape.com/hiscoreuser.cgi?category=".$category."&username=".$username; 
$page = explode(' ', implode('', file($webpage)) ); // split it into words 
$word = count($page); 
$i = 0; 
while ($i <= $word) { 
   $temp = str_replace( '<', '.', $page[$i] ); 
   $temp = str_replace( '>', '.', $temp ); 
   $temp = str_replace( ' ', '', $temp ); 
   $pg = explode('.',$temp); 
   $o = 0; 
   $todo = count($pg); 
   while ( $o <= $todo ) { 
   //echo $pg[$o]."<br>";
      if ( $pg[$o] == "color=yellow" ) { 
	  	
          
         /// i know the next value will be rank 
          	switch ($s) {
			case 1 : 
			$name = str_replace('\r', '',$pg[$o+1] ); 
            $name = str_replace('\n', '',$name ); 
			$s = 2;
			break;
			case 2:
			$lvl = str_replace('\r', '',$pg[$o+1] ); 
            $lvl = str_replace('\n', '',$lvl ); 
			$s = 3;
			break;
			case 3:
			$xp = str_replace('\r', '',$pg[$o+1] ); 
            $xp = str_replace('\n', '',$xp ); 
			break;
			default: 
            $rank = str_replace('\r', '',$pg[$o+1] ); 
            $rank = str_replace('\n', '',$rank );             
            $s = 1; 
			break;
			}
			
			}
        if ($pg[$o] == "width=290" ) {
			$skill = $pg[$o+5];
				}
         /// etc..       
         
      $o++; 
   } 
   $i++;    
} 
$got['rank'] = trim($rank);
$got['xp'] = trim($xp); 
$got['lvl'] = trim($lvl);
$got['skill'] = trim($skill);
return $got;
} 
}
?>
hope someone can help :)

Tom

Posted: Sat Aug 09, 2003 7:36 pm
by m3rajk
umm... is it trying to convert your hex to an int base 10?

tell it to treat it as a string and have the browser treat it otherwise

Posted: Sun Aug 10, 2003 2:43 am
by toms100
this generates an image so i need it in RGB.
initially when i had all the RGB values it still ended up that brown colour..

Posted: Sun Aug 10, 2003 4:07 am
by pootergeist
your hexdec creates vars $r $g $b whereas you try to access an array as throughput

$return_var = array(
'r' => hexdec(substr($input,0,2)),
'b' => hexdec(substr($input,2,2)),
'g' => hexdec(substr($input,4,2)));
return $return_var;

Posted: Sun Aug 10, 2003 7:00 am
by toms100
i changed that but it still does it the greeny browny colour :(
also ive noticed that the colour changed depending on the background image....
which led me to look at php.net and an interesting comment was posted on the imagettftext page:
I've recently had a problem with a image script that overlays text on top of a truecolour image, using the imagettftext function. The problem is the any text on the image just simply refuses to appear, regardless of specifying the full path of the font file or not.

The script used to work fine, until version 4.3.0, and the same problem seems to exist in version 4.3.1 and 4.3.2

For those who are stuck with this problem, here is my work around.

You need to create a 2nd overlay image which is the same size as the using the imagecreate function. Then you set the background colour to a transparent colour.

Once this is done, use the imagettftext functions to draw the text onto the overlay image.

When you have completed drawing onto the overlay image, the image needs to be overlayed onto the truecolour image. Just use the imagecopymerge function with a PCT value of 100.

Then continue to use the imageJPEG and imagePNG functions as normal, hopefully with your text on it!
i tried that but all i got was a black screen.....
this was my code for it:

Code: Select all

<?php
<?php
class sig {
	var $bg;
	var $text;
	var $username;
	var $xOff;
	var $yOff;
	var $font;
	var $fontsize;
	var $fontcolour;
	var $skill;
	var $image;
	var $im2;
function workaround() {
	$size = getimagesize($this->bg);
	$this->image = imagecreate($size[0],$size[1]);
	//make it transparent
	$black = imagecolorallocate($this->image, 0, 0, 0);		
	imagefill($this->image,0,0,$black);
	imagecolortransparent($this->image,$black);
	
	
	//transparent
	//return
}

function workaround2() {
	/// copys it back
	$size = getimagesize($this->bg);
	imagecopymerge( $this->image, $this->image,0, 0,0,0, $size[0],$size[1],100);
}

	
function sig($bg,$text,$username,$xOff,$yOff,$font,$fontsize,$fontcolour,$skill) {
	
	$this->bg = $bg;
	$this->text = $text;
	$this->username = str_replace(' ','_',$username);;
	$this->xOff = $xOff;
	$this->yOff = $yOff;
	$this->font = "/web/sites/296/saundersbc/www.saundersbc.f2s.com/rs/fonts/".$font;
	$this->fontsize = $fontsize;
	$this->skill = $skill;
	
	
	///
	if ($this->bg == "") {
	$this->bg = "sig2.jpg";
	}
	/// work around
	$this->workaround();
	$ext = explode('.',$this->bg);
	$th = count($ext);
	/////
	//$ext[$th-1] should be the extension..
	if ($ext[$th-1] == 'jpg' || $ext[$th-1] == 'JPG' || $ext[$th-1] == 'jpeg' || $ext[$th-1] == 'JPEG') {
		$this->im2 = imagecreatefromjpeg($this->bg);
	}
	/*if ($ext[$th-1] == 'gif' || 'GIF') {
		$this->im2 = imagecreatefromgif($this->bg);
	}*/
	if ($ext[$th-1] == 'png' || $ext[$th-1] == 'PNG') {
		$this->im2 = imagecreatefrompng($this->bg);
	}
	$this->colourf($fontcolour);
	$this->drawimage();
}

function hex2dec($input) {
	if(strlen($input) < 6 ) return(false);
 	$return_var = array( 
	'r' => hexdec(substr($input,0,2)), 
	'b' => hexdec(substr($input,2,2)), 
	'g' => hexdec(substr($input,4,2))); 
	return $return_var;
 }

function colourf($colour) {
	switch ($colour) {
		case 'black':
		$col = "000000";
		break;
		case 'white':
		$col = "FFFFFF";
		break;
		case 'blue':
		$col = "FF0000";
		break;
		case 'red':
		$col = "00FF00";
		break;
		case 'green':
		$col = "0000FF";
		break;
		case 'yellow':
		$col = "FFFF00";
		break;
		case 'magenta':
		$col = "FF00FF";
		break;
		case 'cyan':
		$col = "00FFFF";
		break;
		case 'orange':
		$col = "FFA500";
		break;
		case 'redorange':
		$col = "FF4500";
		break;
		case 'violet':
		$col = "EE82EE";
		break;
		case 'purple' :
		$col = "A020F0";
		break;
		case 'greenblue':
		$col = "2E8B57";
		break;
		default:
		$col = $colour;	
		break;
	}
	$rgb = $this->hex2dec($col);
	if ($rgb) {
	$this->fontcolour = imagecolorallocate($this->image, $rgb['r'],$rgb['g'],$rgb['b']);
	} else {
	$this->fontcolour = imagecolorallocate($this->image, 0,0,0);
	}
}

function drawimage() {	
	/// should have done the image now
	/// get the stats
	if ($this->skill == "random") {
	$skill = rand(0,15);
	settype($skill,"int");
	$stats = $this->getstats($this->username,$skill);
	$t = 0;
	while ($stats['lvl'] = "" && $t < 10) {
		$skill = rand(0,15);
		settype($skill,"int");
		$stats = $this->getstats($this->username,$skill);
		$t++;
	}
	}
	else{
		$stats = $this->getstats($this->username,$this->skill);
	}
	if ($stats['xp'] == "") {
	$txt = "xp is NULL";
	}
	else {
	$txt = $this->textdo($this->username,$stats['lvl'],$stats['rank'],$stats['skill'],$stats['xp']);
	}
	$lines = explode('\n',$txt);
	
	
	$this->writetxt($this->xOff,$this->yOff,$lines);
	/// workaround
	$this->workaround2();
	/// workaround^^
	imagejpeg($this->im2);
	
}

function writetxt($x,$y,$lines) {
	$cl = count($lines);
	// $cl is the number of lines to draw.
	//loop thru each line and draw.	
	$y = ($y + $this->fontsize);
	$lin = 0;
	while ($lin < $cl) {
		imagettftext( $this->image, $this->fontsize, 0,$x,$y, $this->fontcolour, $this->font, $lines[$lin]);
		$y = $y + $this->fontsize + 2;
		$lin++;
	}
}

function textdo($username,$lvl,$rank,$skill,$xp) {
	$txt = trim($this->text);
	$txt = str_replace('@USERNAME',$username,$txt);
	$txt = str_replace('@LVL',$lvl,$txt);
	$txt = str_replace('@RANK',$rank,$txt);
	$txt = str_replace('@SKILL',$skill,$txt);
	$txt = str_replace('@XP',$xp,$txt);
	return $txt;
}

function getstats($username,$category=0) { 
$webpage = "http://www.runescape.com/hiscoreuser.cgi?category=".$category."&username=".$username; 
$page = explode(' ', implode('', file($webpage)) ); // split it into words 
$word = count($page); 
$i = 0; 
while ($i <= $word) { 
   $temp = str_replace( '<', '.', $page[$i] ); 
   $temp = str_replace( '>', '.', $temp ); 
   $temp = str_replace( ' ', '', $temp ); 
   $pg = explode('.',$temp); 
   $o = 0; 
   $todo = count($pg); 
   while ( $o <= $todo ) { 
   //echo $pg[$o]."<br>";
      if ( $pg[$o] == "color=yellow" ) { 
	  	
          
         /// i know the next value will be rank 
          	switch ($s) {
			case 1 : 
			$name = str_replace('\r', '',$pg[$o+1] ); 
            $name = str_replace('\n', '',$name ); 
			$s = 2;
			break;
			case 2:
			$lvl = str_replace('\r', '',$pg[$o+1] ); 
            $lvl = str_replace('\n', '',$lvl ); 
			$s = 3;
			break;
			case 3:
			$xp = str_replace('\r', '',$pg[$o+1] ); 
            $xp = str_replace('\n', '',$xp ); 
			break;
			default: 
            $rank = str_replace('\r', '',$pg[$o+1] ); 
            $rank = str_replace('\n', '',$rank );             
            $s = 1; 
			break;
			}
			
			}
        if ($pg[$o] == "width=290" ) {
			$skill = $pg[$o+5];
				}
         /// etc..       
         
      $o++; 
   } 
   $i++;    
} 
$got['rank'] = trim($rank);
$got['xp'] = trim($xp); 
$got['lvl'] = trim($lvl);
$got['skill'] = trim($skill);
return $got;
} 
}


?>

Posted: Sun Aug 10, 2003 3:38 pm
by toms100
guess ill ahve to wait for the bug to be fixed :(