Jump to content

function to output function


shocker-z

Recommended Posts

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..
[code]
<?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');
?>
[/code]
i have also tryed with echo and return
[code]
<?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');
?>
[/code]

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
Link to comment
https://forums.phpfreaks.com/topic/9470-function-to-output-function/
Share on other sites

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:
[code]<?php
function hexrgb ($hexstr)
{
  $int = hexdec($hexstr);

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


Or you can put the ImageColorAllocate() function in your function and return the value returned by ImageColorAllocate().
[code]<?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));
}?>[/code]
To use:
[code]<? $im = hexrgb('ffeeaa',$img_handle); ?>[/code]

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.