tags:

views:

56

answers:

2

I have a 1 pixel tall by 760 pixel wide image that I use as a repeating vertical background image. The right side of this image is filled with a spot color (the remaining left side of the image is white).

The purpose of this background image, in my css based layout, is that it provides the illusion that the sidebar background color runs all the way down the page (easy to do with tables, but no so much with CSS positioning).

What I need to do is to figure a way to feed a php script (background-image.php) which contains the imagecreatefromgif function, a hex number and have it use that to repaint the spot color of the image to match the spot color that's passed in and save the resulting image onto the server, overwriting the default one.

Ideally, I'd not like to have to call this function everytime the template loads, and onlydo it when the user elects to change the template colors. So once they do that, I'd just like to modify the existing image I've got on the server which will always be called "sidebar_bg.gif"

Any ideas on how to do this are much appreciated.

+1  A: 

Something like this could do it:

$token = md5(serialize(array($red, $green, $blue)));

if (!file_exists('cachedir/'.$token.'.gif')) 
{
    $img = imagecreatefromgif('origfilename.gif');

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

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

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

serveFile($token);

EDIT: Added caching to example code

Franz
To convert a possible hexadecimal GET parameter you could use Saulius' approach to split it up into the RGB values like this: `list($red, $green, $blue) = hexToRGB($_GET['hex']);`
Franz
Thanks Franz! This was exactly what I was looking for.
Scott B
+1  A: 

Just an addition to this post. You can convert HEX color into RGB notation with the folowing function:

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;
}

e.g. try:

var_dump(hexToRGB("FFFFFF"));
Saulius Lukauskas
Thank you. Good one.
Franz
Guys, thanks a ton for the quick input on this. I'm able to create a replica of my "origfilename.gif" but I have not yet played with actually changing the spot color area to the passed in hex. I'm going to play with it a bit.The other thing I'm seeing is that I'm getting an undefined on the function serveFile()Any ideas about that? I'm using localhost with wordpress 2.6.8
Scott B
Well, I think it was not supposed to be defined. You should replace it with the way you show file (serve file) now.
Saulius Lukauskas
A one liner: $rgb = array_map('hexdec', str_split($hex, 2));
Alix Axel