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
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.