Source for file Gradient.php
Documentation is available at Gradient.php
* @copyright (c) 1995-2008 by Steema Software SL. All Rights Reserved. <info@steema.com>
* @link http://www.steema.com
private $direction = 0; // Vertical
private $startColor; // ='#f00';
private $endColor; //='#000';
private $step= 0; // = intval(abs($step));
private $customTargetPolygon = null;
// properties from teechart
//private Color startColor = Color.GOLD;
//private Color middleColor = Color.GRAY;
//private Color endColor = Color.WHITE;
//private float sigmaFocus = 0.5F;
//private float sigmaScale = 1.0F;
//private boolean wrapTile = false;
//private boolean gammaCorrection;
//private boolean useMiddle;
//private int transparency = 0;
//private double angle = 0D;
//private int radialX = 0;
//private int radialY = 0;
function __get( $property ) {
$method = "get{$property}";
function __set ( $property,$value ) {
$method = "set{$property}";
return $this->$method($value);
$this->startColor = new Color(255,100,100);
$this->endColor = new Color(255,255,255);
/* TODO remove $this->startColor = '#bbb'; //new Color(255,100,100);
$this->endColor = '#fff'; // new Color(255,255,255);
$this->step = intval(abs(0));
* Copies the Gradient parameter properties into the Canvas.Gradient object.
public function assign($value) {
$this->direction = $value->direction;
$this->startColor = $value->startColor;
//middleColor = value.middleColor;
$this->endColor = $value->endColor;
//radialX = value.radialX;
//radialY = value.radialY;
$this->customTargetPolygon = $value->customTargetPolygon;
sigmaFocus = value.sigmaFocus;
sigmaScale = value.sigmaScale;
wrapTile = value.wrapTile;
gammaCorrection = value.gammaCorrection;
//useMiddle = value.useMiddle;
//transparency = value.transparency;
Script Name: GD Gradient Fill
Script URI: http://planetozh.com/blog/my-projects/images-php-gd-gradient-fill/
Description: Creates a gradient fill of any shape (rectangle, ellipse, vertical, horizontal, diamond)
Author URI: http://planetozh.com/
* - width and height : integers, dimesions of your image.
* - direction : string, shape of the gradient.
* Can be : vertical, horizontal, rectangle (or square), ellipse, ellipse2, circle, circle2, diamond.
* - startcolor : string, start color in 3 or 6 digits hexadecimal.
* - endcolor : string, end color in 3 or 6 digits hexadecimal.
* - step : integer, optional, default to 0. Step that breaks the smooth blending effect.
* $image = new Gradient(300,200,'ellipse','#f00','#000',0);
// Attempt to create a blank image in true colors, or a new palette based image if this fails
$this->image = imagecreatetruecolor($this->width,$this->height);
$this->image = imagecreate($this->width,$this->height);
die('Unable to create an image');
$this->fill($this->image,$this->direction,$this->startcolor,$this->endcolor);
// Displays the image with a portable function that works with any file type
// depending on your server software configuration
header("Content-type: image/png");
header("Content-type: image/gif");
header("Content-type: image/jpeg");
header("Content-type: image/vnd.wap.wbmp");
die("Doh ! No graphical functions on this server ?");
// The main function that draws the gradient
function fill($im,$direction= null,$start= null,$end= null) {
if ($direction== null) $direction= $this->direction;
if ($start== null) $start= $this->startColor;
if ($end== null) $end= $this->endColor;
$line_numbers = imagesx($im);
$line_width = imagesy($im);
$line_numbers = imagesy($im);
$line_width = imagesx($im);
$rh= $height> $width? 1: $width/ $height;
$rw= $width> $height? 1: $height/ $width;
$line_numbers = min($width,$height);
$rh= $height> $width? 1: $width/ $height;
$rw= $width> $height? 1: $height/ $width;
$line_numbers = sqrt(pow($width,2)+ pow($height,2));
$line_numbers = sqrt(pow($width,2)+ pow($height,2));
$line_numbers = min($width,$height);
imagefill($im, 0, 0, imagecolorallocate( $im, $r1, $g1, $b1 ));
$line_numbers = max($width,$height)/ 2;
$rh= $height> $width? 1: $width/ $height;
$rw= $width> $height? 1: $height/ $width;
$line_numbers = min($width,$height);
for ( $i = 0; $i < $line_numbers; $i= $i+ 1+ $this->step ) {
$r = ( $r2 - $r1 != 0 ) ? intval( $r1 + ( $r2 - $r1 ) * ( $i / $line_numbers ) ): $r1;
$g = ( $g2 - $g1 != 0 ) ? intval( $g1 + ( $g2 - $g1 ) * ( $i / $line_numbers ) ): $g1;
$b = ( $b2 - $b1 != 0 ) ? intval( $b1 + ( $b2 - $b1 ) * ( $i / $line_numbers ) ): $b1;
// if new values are really new ones, allocate a new color, otherwise reuse previous color.
// There's a "feature" in imagecolorallocate that makes this function
// always returns '-1' after 255 colors have been allocated in an image that was created with
// imagecreate (everything works fine with imagecreatetruecolor)
//review todo if ( "$old_r,$old_g,$old_b" != "$r,$g,$b")
$fill = imagecolorallocate( $im, $r, $g, $b );
imagefilledrectangle($im, 0, $i, $line_width, $i+ $this->step, $fill);
imagefilledrectangle( $im, $i, 0, $i+ $this->step, $line_width, $fill );
imagefilledellipse ($im,$center_x, $center_y, ($line_numbers- $i)* $rh, ($line_numbers- $i)* $rw,$fill);
imagefilledrectangle ($im,$i* $width/ $height,$i* $height/ $width,$width- ($i* $width/ $height), $height- ($i* $height/ $width),$fill);
imagefilledpolygon($im, array (
$width/ 2, $i* $rw- 0.5* $height,
$i* $rh- 0.5* $width, $height/ 2,
$width/ 2,1.5* $height- $i* $rw,
1.5* $width- $i* $rh, $height/ 2 ), 4, $fill);
function fillGradient($im,$x,$y,$w,$h,$direction= null,$start= null,$end= null) {
if ($direction== null) $direction= $this->direction;
if ($start== null) $start= $this->startColor;
if ($end== null) $end= $this->endColor;
$line_numbers = $w; // imagesx($im);
$line_width = $h; // imagesy($im);
$line_numbers = $h; // imagesy($im);
$line_width = $w; // imagesx($im);
$width = $w; // imagesx($im);
$height = $h; // imagesy($im);
$rh= $height> $width? 1: $width/ $height;
$rw= $width> $height? 1: $height/ $width;
$line_numbers = min($width,$height);
$width = $w; //imagesx($im);
$height = $h; // imagesy($im);
$rh= $height> $width? 1: $width/ $height;
$rw= $width> $height? 1: $height/ $width;
$line_numbers = sqrt(pow($width,2)+ pow($height,2));
$width = $w; //imagesx($im);
$height = $h; // imagesy($im);
$line_numbers = sqrt(pow($width,2)+ pow($height,2));
$width = $w; // imagesx($im);
$height = $h; // imagesy($im);
$line_numbers = min($width,$height);
imagefill($im, 0, 0, imagecolorallocate( $im, $r1, $g1, $b1 ));
$width = $w; // imagesx($im);
$height = $h; // imagesy($im);
$line_numbers = max($width,$height)/ 2;
$width = $w; //imagesx($im);
$height = $h; //imagesy($im);
$rh= $height> $width? 1: $width/ $height;
$rw= $width> $height? 1: $height/ $width;
$line_numbers = min($width,$height);
for ( $i = 0; $i < $line_numbers; $i= $i+ 1+ $this->step ) {
$r = ( $r2 - $r1 != 0 ) ? intval( $r1 + ( $r2 - $r1 ) * ( $i / $line_numbers ) ): $r1;
$g = ( $g2 - $g1 != 0 ) ? intval( $g1 + ( $g2 - $g1 ) * ( $i / $line_numbers ) ): $g1;
$b = ( $b2 - $b1 != 0 ) ? intval( $b1 + ( $b2 - $b1 ) * ( $i / $line_numbers ) ): $b1;
// if new values are really new ones, allocate a new color, otherwise reuse previous color.
// There's a "feature" in imagecolorallocate that makes this function
// always returns '-1' after 255 colors have been allocated in an image that was created with
// imagecreate (everything works fine with imagecreatetruecolor)
//review todo if ( "$old_r,$old_g,$old_b" != "$r,$g,$b")
$fill = imagecolorallocate( $im, $r, $g, $b );
imagefilledrectangle($im, 0, $i, $line_width, $i+ $this->step, $fill);
imagefilledrectangle( $im, $i, 0, $i+ $this->step, $line_width, $fill );
imagefilledellipse ($im,$center_x, $center_y, ($line_numbers- $i)* $rh, ($line_numbers- $i)* $rw,$fill);
imagefilledrectangle ($im,$i* $width/ $height,$i* $height/ $width,$width- ($i* $width/ $height), $height- ($i* $height/ $width),$fill);
imagefilledpolygon($im, array (
$width/ 2, $i* $rw- 0.5* $height,
$i* $rh- 0.5* $width, $height/ 2,
$width/ 2,1.5* $height- $i* $rw,
1.5* $width- $i* $rh, $height/ 2 ), 4, $fill);
return Array($color->red,$color->green,$color->blue);
// #ff00ff -> array(255,0,255) or #f0f -> array(255,0,255)
* Determines whether the gradient fill appears on screen.<br>
* Determines whether the gradient fill appears on screen.<br>
* Determines the gradient direction
* Default value: vertical
* Determines the gradient direction
* Default value: vertical
* Determines the gradient startColor
return $this->startColor;
* Determines the gradient startColor as RGB
//return Utils::hex2rgb($this->startColor);
return $this->startColor;
* Determines the gradient startColor
* Determines the gradient startColor as RGB
// $this->startColor = Utils::rgbhex($value->getRed(),$value->getGreen(),$value->getBlue());
$this->startColor = $value;
* Determines the gradient endColor
* Determines the gradient endColor as RGB
* Determines the gradient endColor
* Determines the gradient endColor as RGB
$this->endColor = Utils::rgbhex($value->getRed(),$value->getGreen(),$value->getBlue());
/// Assign a rectangle to this property to define a custom rectangle to a Gradient.
return $this->customTargetPolygon;
$this->customTargetPolygon = $value;
/* function imagecolorgradient($img,$x1,$y1,$height,$width,$colA,$colB) {
$varC1=($colA[1]-$colB[1])/$height;
$varC2=($colA[2]-$colB[2])/$height;
$varC3=($colA[3]-$colB[3])/$height;
for ($i=0;$i<=$height;$i++) {
$col=ImageColorAllocate($img,
$colA[1]-floor($i*$varC1),
$colA[2]-floor($i*$varC2),
$colA[3]-floor($i*$varC3));
ImageLine($img,$x1,$y1+$i,$x1+$width,$y1+$i,$col);
if($f_c[0]> $s_c[0]) $r_range= $f_c[0]- $s_c[0];
else $r_range= $s_c[0]- $f_c[0];
if($f_c[1]> $s_c[1]) $g_range= $f_c[1]- $s_c[1];
else $g_range= $s_c[1]- $f_c[1];
if($f_c[2]> $s_c[2]) $b_range= $f_c[2]- $s_c[2];
else $b_range= $s_c[2]- $f_c[2];
imageline($img,$x1,$y1+ $i,$x2,$y1+ $i,$col);
if($f_c[0]< $s_c[0]) $r+= $r_px;
if($f_c[1]< $s_c[1]) $g+= $g_px;
if($f_c[2]< $s_c[2]) $b+= $b_px;
*Draws a gradient color filled polygon
private function draw_GradientPolygon($ColorHandler) {
for ($vertex= 0;$vertex< count($this->Vertices);$vertex++ ) {
$Vertex= explode(",",$this->Vertices[$vertex]);
for ($i= 0;$i< count($ProperVertices)- 6;$i+= 2) {
$StartPointX= $ProperVertices[$i];
$StartPointY= $ProperVertices[$i+ 1];
$FinishPointX= $ProperVertices[$i+ 2];
$FinishPointY= $ProperVertices[$i+ 3];
$RangeX= $FinishPointX- $StartPointX;
if ($RangeX== 0) $RangeX= 1;
$Ratio= ($FinishPointY- $StartPointY)/ $RangeX;
$YPos= $ProperVertices[count($ProperVertices)- 1];
for ($XPos= $StartPointX;$XPos< $FinishPointX;$XPos++ )
ImageLine($this->Canvas,$XPos,$StartPointY+ $Ratio* ($XPos- $StartPointX),$XPos,$YPos,$ColorHandler[$CurrentColor++ ]);
|