Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100 off


PHP Tutorials and Scripts   




Title: GD+ Gradients    Marked Cool    (Review this resource)
Author: aurellius
Posted On: 2004-12-10
Category: Home > PHP Classes

Popularity: 1 points out of 10    

Description: GD extension class for creating gradients to use in your php-generated images. Very user friendly and flexible! Oop structured.

Total Hits: 1234     Total Votes: 0     Total Points: 0 (0 reviews)        [ Download ]   

Page Navigation:  [1]


GD+ Gradients




Code Information
GD+ Gradients is the beginnings of a larger set of PHP GD coding extensions, hence, GD+. GD+ Gradients creates a gradient to be used in images, based on information provided, such as the position of certain colors, up to 100, like that in Photoshop. In this first beta class, there is no opacity level capabilities although there will be later in the game.

Below is the list of functions structured below the class with explanations and usages, and an example code piece to create a yellow-orange gradient. If you find any bugs, please let me know using this contact email.

Sample Code
<?php
include('gdplus/gradients.php'); $gradient = new gdplus_gradients; $gradient->addcolor(230,220,10,0); $gradient->addcolor(240,170,20,50); $gradient->addcolor(220,120,30,100); $display = $gradient->buildgradient(200,200); header('content-type: image/jpeg'); imagejpeg($display);
?>

function addcolor(int $red, int $green, int $blue, int $position)
Adds a new slider to the gradient being built; returns true upon success and false if a slider is already specified for the assigned position.

function buildgradient()
Returns the gradient image resource. Returns true if image creation is successful; false otherwise.

function clearcache()
Clears the gradient cache to start over and create a new gradient. Clears all information variables for use again. Returns nothing.

class gdplus_gradients
<?php
class gdplus_gradients 
{
   var $red = array();
   var $green = array();
   var $blue = array();
   var $position = array();
   var $rotation = 0;
   function addcolor($red,$green,$blue,$position)
   {
      if(@in_array((int)$position,$this->position)) return false;
      $this->red[sizeof($this->red)] = (int)$red;
      $this->green[sizeof($this->green)] = (int)$green;
      $this->blue[sizeof($this->blue)] = (int)$blue;
      $this->position[sizeof($this->position)] = (int)$position;
      return true;
   }
   function buildgradient($width,$height)
   {
      if($im = @imagecreate((int)$width,(int)$height))
      {
         asort($this->position);
         $percentage = (int)$width / 100;
         foreach($this->position as $id => $value)
         {
            if(is_numeric($last)){
               $c['red'] = $this->red[$last];
               $c['green'] = $this->green[$last];
               $c['blue'] = $this->blue[$last];
               $diff = $this->position[$last] - $value;
               $p['red'] = ($this->red[$last] -  $this->red[$id]) / $diff;
               $p['green'] = ($this->green[$last] - $this->green[$id]) / $diff;
               $p['blue'] = ($this->blue[$last] - $this->blue[$id]) / $diff;
               for($i=$this->position[$last];$i<$value;$i++)
               {
                  $w = floor($i * $percentage);
                  $color = @imagecolorallocate($im,floor($c['red']),
                             floor($c['green']),floor($c['blue']));
                  @imagefilledrectangle($im,$w,0,floor($w + $percentage),
                             (int)$height,$color);
                  $c['red'] += $p['red'];                   $c['green'] += $p['green'];                   $c['blue'] += $p['blue'];                }             }             $last = $id;          }          return $im;       }else return false;    }    function clearcache()    {       $this->red = array();       $this->green = array();       $this->blue = array();       $this->position = array();       $this->rotation = 0;       return true;    } } ?>


Page Navigation:  [1]



© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.