1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: define('RESOURCEFOLDER',dirname(__FILE__).DIRECTORY_SEPARATOR.'resource');
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27: class Image_Gauge extends Image_Image
28: {
29:
30: private $gauge_needle;
31: private $gauge;
32: private $blank;
33: private $value;
34: private $max;
35: private $min;
36:
37: 38: 39: 40: 41: 42: 43:
44: public function __construct($value,$min=0,$max=100)
45: {
46: $this->value = $value;
47: $this->max = $max;
48: $this->min = $min;
49: $this->gauge_needle = RESOURCEFOLDER.DIRECTORY_SEPARATOR.'gauge_needle.png';
50: $this->gauge = RESOURCEFOLDER.DIRECTORY_SEPARATOR.'gauge_blank.png';
51: $this->blank = RESOURCEFOLDER.DIRECTORY_SEPARATOR.'blank.png';
52: }
53:
54: 55: 56: 57: 58: 59:
60: public function setGauge($jauge)
61: {
62: if(file_exists($jauge))
63: $this->gauge = $jauge;
64: else
65: throw new Image_Exception('Le fichier '.$jauge.' image n\'existe pas');
66: }
67:
68: 69: 70: 71: 72: 73:
74: public function setNeedle($aiguille)
75: {
76: if(file_exists($aiguille))
77: $this->gauge_needle = $aiguille;
78: else
79: throw new Image_Exception('Le fichier '.$aiguille.' image n\'existe pas');
80: }
81:
82: 83: 84: 85:
86: public function draw()
87: {
88: parent::__construct($this->gauge);
89: $this->setType(parent::IMG_PNG);
90:
91: $this->setFont('arial');
92: $this->setColor(231,34,34);
93: $this->setText($this->min,8,44,117);
94: $this->setColor(23,140,4);
95: $this->getValueColor();
96: $this->setText($this->max,8,105,117);
97: $this->setText($this->value.'%',14,$this->computeXposValue(),145);
98:
99: $angle = $this->computeAngle();
100: $needle = new Image_Image($this->gauge_needle);
101: $tailleNeedle = $needle->getInfo();
102: $needle->rotate(-$angle);
103:
104: $fond = $this->getSource();
105:
106: $aiguille = $needle->getSource();
107: $tailleNewNeedleX = imagesx($aiguille);
108: $tailleNewNeedleY = imagesy($aiguille);
109:
110:
111: $gauge = imagecreatefrompng($this->blank);
112: imageAlphaBlending($gauge, true);
113: imageSaveAlpha($gauge, true);
114:
115: imagecopy($gauge,$aiguille,0,0,round(($tailleNewNeedleX-$tailleNeedle['width'])/2)+33,round(($tailleNewNeedleY-$tailleNeedle['height'])/2)+33,$tailleNeedle['width'],$tailleNeedle['height']);
116:
117: imagecopy($fond,$gauge,0,0,0,0,165,165);
118: $this->setSource($fond);
119:
120: 121: 122: 123: 124: 125:
126: }
127:
128: 129: 130: 131: 132: 133:
134: private function computeAngle()
135: {
136: return (($this->value - $this->min)*260)/($this->max - $this->min);
137: }
138:
139: 140: 141: 142: 143:
144: private function getValueColor()
145: {
146: if($this->value<30)
147: $this->setColor(231,34,34);
148: elseif($this->value>70)
149: $this->setColor(23,140,4);
150: else
151: $this->setColor(237,237,9);
152: }
153:
154: 155: 156: 157: 158: 159:
160: private function computeXposValue()
161: {
162: $taille = strlen($this->value);
163: switch($taille)
164: {
165: case 1:
166: $pos = 70;
167: break;
168: case 2:
169: $pos = 70;
170: break;
171: case 3:
172: $pos = 60;
173: break;
174: default :
175: $pos = 70;
176: break;
177: }
178: return $pos;
179: }
180: }
181: ?>