1: <?php
2: /**
3: * Pry Framework
4: *
5: * LICENSE
6: *
7: * This source file is subject to the new BSD license that is bundled
8: * with this package in the file LICENSE.txt.
9: *
10: * @version $Revision: 276 $
11: */
12:
13: /**
14: * Créer un dégradé de couleur à partir d'une couleur de début et une de fin.
15: *
16: * <code>
17: * $image = new DegradeCouleur(110,110);
18: * $image->startColor = $image->hexToRgb('28abe2');
19: * $image->endColor = $image->hexToRgb('2d3192');
20: * print_r($image->startColor);
21: * $image->direction = DegradeCouleur::DEGRADE_HORIZONTAL;
22: * $image->degrade();
23: * $image->setType(DegradeCouleur::IMG_JPG);
24: * $image->save('test.jpg');
25: * </code>
26: *
27: * @category Pry
28: * @package Image
29: * @version 1.2.0
30: * @author Olivier ROGER <oroger.fr>
31: *
32: */
33: class Image_DegradeCouleur extends Image_Image
34: {
35: const DEGRADE_HORIZONTAL=0;
36: const DEGRADE_VERTICAL=1;
37: const DEGRADE_DIAGONAL=2;
38:
39: /**
40: * Direction du dégradé
41: * @access public
42: * @var int
43: */
44: public $direction;
45:
46: /**
47: * Couleur de début
48: * @access public
49: * @var array
50: */
51: public $startColor;
52:
53: /**
54: * Couleur de fin
55: * @access public
56: * @var array
57: */
58: public $endColor;
59:
60: /**
61: * Largeur de l'image
62: * @access private
63: * @var int
64: */
65: private $largeur;
66:
67: /**
68: * Hauteur de l'image
69: * @access private
70: * @var int
71: */
72: private $hauteur;
73:
74: /**
75: * Constructeur
76: *
77: * @param int $width
78: * @param int $height
79: */
80: public function __construct($width,$height)
81: {
82: parent::__construct(null,$width,$height);
83: $this->largeur = $width;
84: $this->hauteur = $height;
85: if($width<=0 || $height<=0)
86: throw new RangeException('Une image doit avoir des dimension valides');
87: }
88:
89: /**
90: * Lance la construction du dégradé
91: *
92: */
93: public function degrade()
94: {
95: $this->setColor(255,255,255);
96:
97: // On défini le nombre de lignes à tracer en fonction du type de dégradé
98: if($this->direction == self::DEGRADE_HORIZONTAL)
99: {
100: $nbLine = $this->largeur;
101: }
102: elseif($this->direction == self::DEGRADE_VERTICAL)
103: {
104: $nbLine = $this->hauteur;
105: }
106: else
107: {
108: $nbLine = $this->largeur+$this->hauteur;
109: }
110:
111: for($i=0;$i<$nbLine;$i++)
112: {
113: //Calcul de chaque composante en fonction du pas défini par :
114: // Ligne*(Composante de fin - Composante de début )/ Nombre de ligne
115: $red = $this->startColor[0]+$i*($this->endColor[0]-$this->startColor[0])/$nbLine;
116: $green = $this->startColor[1]+$i*($this->endColor[1]-$this->startColor[1])/$nbLine;
117: $blue = $this->startColor[2]+$i*($this->endColor[2]-$this->startColor[2])/$nbLine;
118:
119: //Attribution de la nouvelle couleur
120: $this->setColor($red,$green,$blue);
121:
122: //Dessin de la ligne
123: if($this->direction == self::DEGRADE_HORIZONTAL)
124: {
125: //Dégradé Horizontal => dessin de ligne vertical
126: imageline($this->source,$i,0,$i,$this->hauteur,$this->couleur);
127: }
128: elseif($this->direction == self::DEGRADE_VERTICAL)
129: {
130: imageline($this->source,0,$i,$this->largeur,$i,$this->couleur);
131: }
132: else
133: {
134: imageline($this->source,max(0,($i-$this->hauteur)),min($i,$this->hauteur),min($i,$this->largeur),max(0,($i-$this->largeur)),$this->couleur);
135: }
136: }
137: }
138: }
139: ?>