Gradient,Image,php

Patrick64 10/4/2016 0

Gradient Image in PHP

PHP
<?php

/*
  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();

?>
 

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments