<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* Class for creating several gradients
*
* @author Uku-Kaarel Jõesaar, http://ukj.pri.ee
*/
class imagegradient {
/**
* GD Image.
* @var resource $im
*/
var $im;
/**
* The constructor.
*
* @param resource $im GD Image
* @return none
*/
function imagegradient( & $im ) {
$this->im = & $im;
//imagealphablending($this->im, true);
//imagesavealpha($this->im, false);
} //imagegradient()
/**
* Return an array that contain the gradient.
*
* @param array $b start color
* @param array $c final color
* @param integer $n steps
* @param bool $shade if TRUE [ $b ---> $c <--- $b
* @return array colors; ex.: array( array(r0,g0,b0),array(r1,g1,b1),array(r2,g2,b2) )
*/
function color_transition($b, $c, $n,$shade=FALSE) {
//echo print_r($b,1) .", ". print_r($c,1).", ".print_r($n,1)." ;";
if( $shade===true ) $n = floor($n/2);
if(isset($b[3],$c[3])) $isAlpha = TRUE; else $isAlpha = FALSE;
$red_start = $b[0]; $green_start = $b[1]; $blue_start = $b[2];
$red_end = $c[0]; $green_end = $c[1]; $blue_end = $c[2];
if($isAlpha) {
$alpha_start = $b[3];
$alpha_end = $c[3];
if( $b[3] > 127 ) $b[3] = 127; elseif( $b[3] < 0 ) $b[3] = 0;
if( $c[3] > 127 ) $c[3] = 127; elseif( $c[3] < 0 ) $c[3] = 0;
if($b[3]>=$c[3]) $rgbd[3] = ($b[3]-$c[3]); else $rgbd[3] = ($c[3]-$b[3]);
$step_alpha = ( $rgbd[3] / $n);
}
// difference between start and end
if($b[0]>=$c[0]) $rgbd[0] = ($b[0]-$c[0]); else $rgbd[0] = ($c[0]-$b[0]);
if($b[1]>=$c[1]) $rgbd[1] = ($b[1]-$c[1]); else $rgbd[1] = ($c[1]-$b[1]);
if($b[2]>=$c[2]) $rgbd[2] = ($b[2]-$c[2]); else $rgbd[2] = ($c[2]-$b[2]);
$colorDif = $rgbd;
// width of one color step
$step_red = ( $rgbd[0] / $n) ;
$step_green = ( $rgbd[1] / $n) ;
$step_blue = ( $rgbd[2] / $n) ;
//echo "$step_red , $step_green, $step_blue";
for ($i = 0 ; $i <= $n ; $i++) {
if( $red_start>$red_end ) $red = $red_start - ($step_red * $i);
else $red = $red_start + ($step_red * $i);
if( $blue_start>$blue_end ) $blue = $blue_start - ($step_blue * $i);
else $blue = $blue_start + ($step_blue * $i);
if( $green_start>$green_end ) $green = $green_start - ($step_green * $i);
else $green = $green_start + ($step_green * $i);
if( $isAlpha ) {
if( $alpha_start>$alpha_end ) $alpha = $alpha_start - ($step_alpha * $i);
else $alpha = $alpha_start + ($step_alpha * $i);
$GradientColor[] = array(round($red) ,round($green) ,round($blue), round($alpha));
} else
$GradientColor[] = array(round($red) ,round($green) ,round($blue));
}
if( $shade===TRUE ) {
$ShadeColorRev = Array_reverse($GradientColor);
array_pop ($GradientColor);
$shade = Array_merge($GradientColor ,$ShadeColorRev );
unset($ShadeColorRev);
unset($ShadeColor);
return $shade;
}
//print_r ( $GradientColor );
return $GradientColor;
}// color_transition()
/**
* Color transition on vertical axis, top to bottom.
*
* <code>
* -------- |
* ........ |
* ======== V
* </code>
*
* @param mixed $startcol
* @param mixed $endcol
* @param int $gx top-left corner x
* @param int $gy top-left corner y
* @param int $width
* @param int $height
* @param bool $shade
* @return none
*/
function gradient_vertical($startcol=array(0,0,0,0),$endcol=array(255,255,255,127),$gx=0,$gy=0,$width=100,$height=10,$shade=false) {
$a = $this->color_transition($startcol , $endcol, $height ,$shade);
if( function_exists('imagecolorallocatealpha') )
$esc = "\$color = imagecolorallocatealpha(\$this->im,\$a[\$pos][0],\$a[\$pos][1],\$a[\$pos][2],\$a[\$pos][3]);";
else
$esc = "\$color = ImageColorAllocate(\$this->im,\$a[\$pos][0],\$a[\$pos][1],\$a[\$pos][2]);";
//PREPARE
$evalExec="
for (\$pos=0; \$pos<$height;\$pos++) {
$esc
imageline( \$this->im, $gx, $gy+\$pos, $gx+$width, $gy+\$pos,\$color);
}
";
// EXECUTE
eval($evalExec);
}
/**
* Color transition on horizontal axis, left to right.
*
* <code>
* ------->
* I II III
* </code>
*
* @param mixed $startcol
* @param mixed $endcol
* @param int $gx top-left corner x
* @param int $gy top-left corner y
* @param int $width
* @param int $height
* @param bool $shade
* @return none
*/
function gradient_horizontal($startcol=array(0,0,0,0),$endcol=array(255,255,255,127),$gx=0,$gy=0,$width=100,$height=10,$shade=false) {
$a = $this->color_transition($startcol , $endcol, $width ,$shade);
if( function_exists('imagecolorallocatealpha') )
$esc = "\$color = imagecolorallocatealpha(\$this->im,\$a[\$pos][0],\$a[\$pos][1],\$a[\$pos][2],\$a[\$pos][3]);";
else
$esc = "\$color = ImageColorAllocate(\$this->im,\$a[\$pos][0],\$a[\$pos][1],\$a[\$pos][2]);";
$width=$width+$gx-1;
//PREPARE
$evalExec="
for (\$pos=0; \$pos<$width;\$pos++) {
$esc
imageline( \$this->im, $gx+\$pos,$gy, $gx+\$pos,$gy+\$height,\$color);
}
";
// EXECUTE
eval($evalExec);
}
/**
* Apply gradient on specified area
*
* radial quarter [|_] [_|] [|-] [7]
*
* @param string $startcol #rrggbb
* @param string $endcol #rrggbb
* @param int $gx top-left corner x
* @param int $gy top-left corner y
* @param int $len
* @param string $dir direction
* @return none
*/
function gradient_cor($startcol=array(0,0,0),$endcol=array(255,255,255),$gx=0,$gy=0,$len=50,$dir='tl' ) {
$lenc=(2*$len)-1;
// there is gradient array // array( 0=>r,1=>g,2=>b);
$gcol_a = $this->color_transition($startcol , $endcol, $lenc,false);
//echo $len.', '; //print_r( $startcol ) ; print_r ($endcol ); //print_r( $gcol_a );
$gxw= (int)($gx+$len);
$gyh= (int)($gy+$len);
// PREPARE DIRECTION TYPE
if($dir=='tl'){
$evalExec_dirtype = " imagefilledarc(\$this->im, $gxw,$gyh, \$pos, \$pos, 180, 270, \$color, IMG_ARC_PIE); "; } //TL
elseif($dir=='tr'){
$evalExec_dirtype = " imagefilledarc(\$this->im, $gx,$gyh, \$pos, \$pos, 270, 0, \$color, IMG_ARC_PIE); "; } //TR
elseif($dir=='bl'){
$evalExec_dirtype = " imagefilledarc(\$this->im, $gxw,$gy, \$pos, \$pos, 90, 180, \$color, IMG_ARC_PIE); ";} //BL
elseif($dir=='br'){
$evalExec_dirtype = " imagefilledarc(\$this->im, $gx,$gy, \$pos, \$pos, 0, 90, \$color, IMG_ARC_PIE); "; } //BR
// EXECUTE
imagefilledrectangle ( $this->im, $gx,$gy, $gxw,$gyh, imagecolorresolve($this->im, $endcol[0], $endcol[1], $endcol[2]) );
if( function_exists('imagecolorallocatealpha') )
$esc = "\$color = imagecolorallocatealpha(\$this->im,\$gcol_a[\$pos][0],\$gcol_a[\$pos][1],\$gcol_a[\$pos][2],\$gcol_a[\$pos][3]);";
else
$esc = "\$color = ImageColorAllocate(\$this->im,\$gcol_a[\$pos][0],\$gcol_a[\$pos][1],\$gcol_a[\$pos][2]);";
$evalExec="
for (\$pos=$lenc; \$pos>0;\$pos-- ) {
$esc
$evalExec_dirtype
}
";
eval($evalExec);
imagesetthickness($this->im, 1);
} // end method gradient
} //end class
?>