Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • Captcha
  • Converter
  • DegradeCouleur
  • Font
  • Gauge
  • Image
  • Traitement

Exceptions

  • Exception
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  */
 12: 
 13: namespace Pry\Image;
 14: 
 15: define('RESOURCEFOLDER', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'resource');
 16: 
 17: /**
 18:  * Dessine une jauge type jauge à essence.
 19:  * Inspiré de Stephen Powis PHPDialGauge
 20:  * <code>
 21:  * $gauge = new Image_gauge(50);
 22:  * $gauge->draw();
 23:  * $gauge->display();
 24:  * </code>
 25:  * @category Pry
 26:  * @package Image
 27:  * @version 1.1.0 
 28:  * @author Olivier ROGER <oroger.fr>
 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:      * Constructeur. Définition des images de base
 42:      *
 43:      * @param int $value Valeur à afficher
 44:      * @param int $min Valeur minimale
 45:      * @param int $max Valeur maximale
 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:      * Défini une image de jauge
 59:      *
 60:      * @param string $jauge chemin vers l image
 61:      * @access public
 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:      * Défini l image de l'aiguille
 73:      *
 74:      * @param string $aiguille Chemin vers l'image de l'aiguille
 75:      * @access public
 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:      * Dessine la jauge
 87:      * @access public
 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:         //Crop de l aiguille
118:         imagecopy($gauge, $aiguille, 0, 0, round(($tailleNewNeedleX - $tailleNeedle['width']) / 2) + 33, round(($tailleNewNeedleY - $tailleNeedle['height']) / 2) + 33, $tailleNeedle['width'], $tailleNeedle['height']);
119:         //position au centre
120:         imagecopy($fond, $gauge, 0, 0, 0, 0, 165, 165);
121:         $this->setSource($fond);
122:     }
123: 
124:     /**
125:      * Calcul l'angle de rotation
126:      *
127:      * @return int
128:      * @access private
129:      */
130:     private function computeAngle()
131:     {
132:         return (($this->value - $this->min) * 260) / ($this->max - $this->min);
133:     }
134: 
135:     /**
136:      * Attribue une couleur au texte de la valeur
137:      * @access private;
138:      *
139:      */
140:     private function getValueColor()
141:     {
142:         if ($this->value < 30)
143:             $this->setColor(231, 34, 34); // ROUGE
144:         elseif ($this->value > 70)
145:             $this->setColor(23, 140, 4); //VERT
146:         else
147:             $this->setColor(237, 237, 9); //Jaune
148:     }
149: 
150:     /**
151:      * Défini une postion pour le texte de la valeur en fonction de sa taille
152:      *
153:      * @return int
154:      * @access private
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: ?>
Pry API documentation generated by ApiGen 2.8.0