Random image w/ gradient.

Creating and modifying plugins.
Post Reply
Tys
Regular
Posts: 36
Joined: Thu Feb 24, 2005 5:18 am

Random image w/ gradient.

Post by Tys »

Not really a plugin, but others may be interested. I whipped up a little script that takes a side (left or right) and chooses a random image from a directory with a matching name then overlays it with a gradient fading to the color of your choosing. Great for title bars and lot easier then manually throwing a gradient on every image. All that is required is cropping to the right size and uploading to the correct name. Feel free to use it/modify it however you want.

ie (refreshing the images a couple of times should show a new one):

http://tvg.ca/templates/dozqar/gradient.php?side=left
or
http://tvg.ca/templates/dozqar/gradient.php?side=right

In page example at: http://tvg.ca/

Sample directory listing:
header-left-feet.jpg
header-left-light.jpg
header-left-skitracks.jpg
header-left-sun.jpg
header-left-sunshine.jpg
header-right-cameras.jpg
header-right-paintingdasies.jpg
header-right-tyshead.jpg

Links to orginal images:
http://tvg.ca/templates/dozqar/img/header-left-feet.jpg
http://tvg.ca/templates/dozqar/img/head ... -light.jpg
http://tvg.ca/templates/dozqar/img/head ... tracks.jpg
http://tvg.ca/templates/dozqar/img/header-left-sun.jpg
http://tvg.ca/templates/dozqar/img/head ... nshine.jpg
http://tvg.ca/templates/dozqar/img/head ... ameras.jpg
http://tvg.ca/templates/dozqar/img/head ... dasies.jpg
http://tvg.ca/templates/dozqar/img/head ... yshead.jpg

Requires the GD library to compiled with PHP, see more here:
http://ca.php.net/gd

Code: Select all

<?

/* config settings */
define('FADE_RED',138);
define('FADE_GREEN',153);
define('FADE_BLUE',188);
define('IMAGE_DIR','img');
define('RIGHT_PREFIX','header-right');
define('LEFT_PREFIX','header-left');
/* end config settings */


header("Content-type: image/jpg");


if ($_REQUEST['side'] == "right")
    loadimg(RIGHT_PREFIX,.75,false);
else
    loadimg(LEFT_PREFIX,.5,true);
    
function loadimg($side,$start = 1,$fillRight = false)
{
    $images = array();
    $d = @opendir(IMAGE_DIR);
    if ($d)
    {
        while (($f = readdir($d)) !== false)
        {
            if (!strstr($f,$side))
                continue;
            $images[]= $f;
        }
    }
    
    $img  = imagecreatefromjpeg(IMAGE_DIR ."/". $images[array_rand($images)]);

    $gcol = array(0,FADE_RED,FADE_GREEN,FADE_BLUE);
    imagecolorgradient($img,$gcol,$start,$fillRight);
    
    imagejpeg($img);
    imagedestroy($img);
}

function imagecolorgradient($img,$gcol,$start = 1,$fillRight = false)
{
    $width = imagesx($img);
    $height = imagesy($img);
    
    // for every pixel
    for ($w=0; $w<=$width; $w++)
    {
        if ($fillRight)
            $pixelX = $width - $w;
        else
            $pixelX = $w;       

        $ww = $w * $start;
        $iw = $width - $ww;
        
        for ($h=0; $h<=$height; $h++)
        {
            $rgb = imagecolorat($img,$pixelX,$h);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            
            $r = ($ww*$r + $iw*$gcol[1]) / $width;
            $g = ($ww*$g + $iw*$gcol[2]) / $width;
            $b = ($ww*$b + $iw*$gcol[3]) / $width;
            
            $col=ImageColorAllocate($img,$r,$g,$b);
            imagesetpixel($img,$pixelX,$h,$col);
        }
    }
}
?>
Post Reply