Jump to content


Photo

function to output function


  • Please log in to reply
2 replies to this topic

#1 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 10 May 2006 - 03:11 PM

What i'm doing is trying to implament a way to use ImageColorAllocate with hex colors, i've tryed a few ways and none of them work my image doesn't render...
here is my current function which outputs the correct rgb colors if called with hexrgb('ffffff'); it will return 255, 255, 255 so thought it would work but when i use ImageColorAllocate($img_handle, hexrgb('ffffff')); my image doesn't render..
<?php
function hexrgb ($hexstr)
{
  $int = hexdec($hexstr);

  $hex=array("red" => 0xFF & ($int >> 0x10),
               "green" => 0xFF & ($int >> 0x8),
               "blue" => 0xFF & $int);
  foreach ($hex as $i) {
  if ($ii) {
   $color=$color.', '.$i;
  } else {
   $color=$i;
  $ii=1;
  }
  }
  echo($color);
}
$hex=hexrgb('ffffff');
?>
i have also tryed with echo and return
<?php
function hexrgb ($hexstr)
{
  $int = hexdec($hexstr);

  $hex=array("red" => 0xFF & ($int >> 0x10),
               "green" => 0xFF & ($int >> 0x8),
               "blue" => 0xFF & $int);
  foreach ($hex as $i) {
  if ($ii) {
   $color=$color.', '.$i;
  } else {
   $color=$i;
  $ii=1;
  }
  }
  echo("ImageColorAllocate(".'$img_handle'.", $color)");
}
hexrgb('000000');
?>

When i use the last function i get a blank image nothing else..


Any help or diffrent functions/ways to do the same job would be great!

The reason i need this is so then when pulling data froma dabase i will also be able to let a user choose the BG and text colours :)

Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 May 2006 - 04:04 PM

The ImageColorAllocate() function expects that the last 3 arguments are integers, you are trying to use a string.

Modify your routine to return an array containing the values for red, green, and blue:
<?php
function hexrgb ($hexstr)
{
  $int = hexdec($hexstr);

  $hex=array(0xFF & ($int >> 0x10),
               0xFF & ($int >> 0x8),
               0xFF & $int);
  return ($hex);
}?>
To use:
<?php
list ($red, $green, $blue) = hexrgb('ffffff');
ImageColorAllocate($img_handle, $red, $green, $blue);
?>


Or you can put the ImageColorAllocate() function in your function and return the value returned by ImageColorAllocate().
<?php
unction hexrgb ($hexstr,$img_handle)
{
  $int = hexdec($hexstr);

  list($r, $g, $b) = array(0xFF & ($int >> 0x10),
               0xFF & ($int >> 0x8),
               0xFF & $int);
  return (ImageColorAllocate($img_handle, $r, $g, $b));
}?>
To use:
<? $im = hexrgb('ffeeaa',$img_handle); ?>

Ken

#3 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 10 May 2006 - 05:18 PM

Thats great mate thanx alot :D
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users