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