PHPBuilder - Gradient Image



RSS Twitter
Snippets Graphics

Gradient Image

by: Warlord
|
May 25, 2003

Version: 1.3

Type: Function

Category: Graphics

License: GNU General Public License

Description: This script takes a height, width start color and end color (In hex) and generates a vertical gradient jpeg using GD. The script is written so it can be called from an image tag like this: <IMG SRC='gradienti.php?h=400&w=600&sc=FF0000&ec=FFFF00'>



<?php

/* Version 1.3 by Warlord */

/*
  Appears to be a glitch in images greater than 250px high - never reaches final colour?
*/

class grad {

  var $height = 100;
  var $width = 100;
  var $startcol = '000000';
  var $endcol = 'ffffff';

  function draw() {
    $im = imagecreate ($this->width, $this->height);
     
    $b = hexdec($this->startcol);
    $c = hexdec($this->endcol);

    $sr = ($b & 0xFF0000) >> 16;
    $sg = ($b & 0xFF00) >> 8;
    $sb = ($b & 0xFF);

    $er = ($c & 0xFF0000) >> 16;
    $eg = ($c & 0xFF00) >> 8;
    $eb = ($c & 0xFF);

    $r = $er - $sr;
    $g = $eg - $sg;
    $b = $eb - $sb;

    for ($line = 0; $line < $this->height; $line++)
    {
      $cRed = (($sr += ($r / $this->height)) < 0) ? (int)0: (int)$sr;
      $cGreen = (($sg += ($g / $this->height)) < 0) ? (int)0: (int)$sg;
      $cBlue = (($sb += ($b / $this->height)) < 0) ? (int)0: (int)$sb;
      $clr[$line] = imagecolorallocate($im, $cRed, $cGreen, $cBlue);
      imageline($im, 0, $line, ($this->width - 1), $line, $clr[$line]);
    }    
    imagejpeg($im);
  }
}

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

$img = new grad;

if (isset($height)) $img->height = $height;
if (isset($width)) $img->width = $width;
if (isset($startcol)) $img->startcol = $startcol;
if (isset($endcol)) $img->endcol = $endcol;

$img->draw();

?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Warlord

Comment:



Comment:

(Maximum characters: 1200). You have characters left.