1

The function below is designed to take an input hex (with or without the "#" prefix) and apply the color update to the portion of the image between startPixel and endPixel.

I can get the function to work fine on localhost tests when (1) supplying red, green, blue and (2) running the file directly as a stand alone (ie, just saving the contents of the function to a file and executing it).

However, I have two problems I can't seem to resolve. (1) I need to pass in a hex and get the function to work without requiring rgb hard codes and (2) I need the function to work inside my functions.php file in wordpress while saving my theme options. I'm getting a "failed to open stream" error each time I try to call the function on save.

` function:

 function set_theme_color($hex)
    {
    //hexToRGB($hex); DOES NOT WORK. ALWAYS RETURNS BLACK
    $token = "images/sidebar-bg";

    $red = 0;
    $green = 0;
    $blue = 202;

    $startPixel = 601;
    $endPixel = 760;

   $img = imagecreatefromgif('images/sidebar-bg.gif');

    $color = imagecolorallocate($img, $red, $green, $blue);

    for ($i = $startPixel-1; $i < $endPixel; $i++)
    {
        imagesetpixel($img, $i, 0, $color);
    }

    imagegif($img, $token.'.gif');
    }

    function hexToRGB ($hexColor)
    {
    $output = array();
    $output['red']   = hexdec($hexColor[0].$hexColor[1]);
    $output['green'] = hexdec($hexColor[2].$hexColor[3]);
    $output['blue']  = hexdec($hexColor[4].$hexColor[5]);

    return $output;
    }

    set_theme_color('#cccccc');

`

Scott B
  • 38,833
  • 65
  • 160
  • 266

1 Answers1

2

Your hexToRGB function is not taking the possibility of the # sign into account. For parsing the color codes, I'd use a regex:

function hexToRGB ($hexColor)
{
    if( preg_match( '/^#?([a-h0-9]{2})([a-h0-9]{2})([a-h0-9]{2})$/i', $hexColor, $matches ) )
    {
        return array(
            'red' => hexdec( $matches[ 1 ] ),
            'green' => hexdec( $matches[ 2 ] ),
            'blue' => hexdec( $matches[ 3 ] )
        );
    }
    else
    {
        return array( 0, 0, 0 );
    }
}

Your cannot open stream error is most likely due to file permissions. Make sure to grant the permissions mode 777 on the file you're trying to write to.

Jon Benedicto
  • 10,492
  • 3
  • 28
  • 30
  • Thanks Jon, I'm still getting black from the hex conversion. How can I echo out the returned values from the function? – Scott B Dec 10 '09 at 15:14
  • How are you calling hexToRGB? – Jon Benedicto Dec 10 '09 at 15:31
  • OK, when I use var_dump(hexToRGB("cccccc")) it returns what appears to be the proper result set... array(3) { ["red"]=> int(204) ["green"]=> int(204) ["blue"]=> int(204) } However, I'm still getting black from the spot color conversion. Not sure my set_image_color function is executing the rgb conversion properly. Can you have a look back at the set_theme_color function and let me know what I'm missing? – Scott B Dec 10 '09 at 15:34
  • How are you calling hexToRGB? – Jon Benedicto 2 mins ago Answer: inside the function above, I'm uncommenting the hexToRGB($hex) I will be calling the set_theme_color($hexColor) function from functions.php and passing in a hex value. – Scott B Dec 10 '09 at 15:36
  • Your set_theme_color function doesn't appear to be taking the result from hexToRGB and passing it to the image_color_allocate method. – Jon Benedicto Dec 10 '09 at 16:08
  • OK, this works! However, I'm still unable to execute it when I place the function inside functions.php. It only works as a standalone file... function set_theme_color($hex) { $info = hexToRGB($hex); $token = "images/sidebar-bg2"; $startPixel = 601; $endPixel = 760; $img = imagecreatefromgif('images/sidebar-bg2.gif'); $color = imagecolorallocate($img, $info["red"], $info["green"], $info["blue"]); for ($i = $startPixel-1; $i < $endPixel; $i++) { imagesetpixel($img, $i, 0, $color); } imagegif($img, $token.'.gif'); } – Scott B Dec 10 '09 at 16:13