Accueil > > > [PHP5] PHPDRAW - LE PHOTOSHOP DE PHP (OU PRESQUE...)
[PHP5] PHPDRAW - LE PHOTOSHOP DE PHP (OU PRESQUE...)
Information sur la source
Description
Hello, bon, ce code ne sert pas à grand chose, c'est un amusement. Etant donné que je ne parviens pas à uploader la 3ème partie de mon tuto sur les design patterns, je me suis amusé à approfondir le dernier exemple de ce tuto sur le design pattern INTERPRETER. Ce code est une petite interface en Ajax permettant de générer une image grâce à des commandes simples : right 50 tracera un trait de 50 pixels vers la droite. On peut les enchaîner: right 50 down 20 right 60 up 50 Les possibilités sont : right, left, up, down suivis chacun d'un entier en pixel pour tracer. pixel suivi d'un entier en pixel pour l'épaisseur du trait. color suivi d'un entier à 9 chiffres (rrrgggbbb) pour la couleur du trait. x, y suivis chacun d'un entier en pixel pour la position x ou y. file suivi d'un nom de fichier pour sauvegarder votre oeuvre d'art ;-) Une doc est incluse. Ce code n'a aucune prétention, c'est juste pour le fun. Mais il a des vertus pédagogiques certaines...! NB: Ce code utilise la librairie js protoype pour faciliter le code js. Ce code n'ea été testé QUE sous Firefox, et je doute qu'il tourne sous IE à cause de la récupération des textNode du flux xml (pas eu le temps de tester sous IE pour être franc).
Source
- <?php
- /**
- * Notre INTERFACE EXPRESSION
- *
- */
- interface expression {
- public static function interpret(interpreter $subject, $mParam);
- }
-
- /**
- * Une EXPRESSION CONCRETE pour les mouvements vers la droite
- *
- */
- class right implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(!is_numeric($mParam)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid distance '.$mParam);
- }
- $mParam = (int)$mParam;
- if (false === imageline($subject->IMG, $subject->X, $subject->Y, $subject->X+$mParam, $subject->Y, $subject->RCOLOR)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create line');
- }
- $subject->X += $mParam;
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour les mouvements vers la gauche
- *
- */
- class left implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(!is_numeric($mParam)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid distance '.$mParam);
- }
- $mParam = (int)$mParam;
- if (false === imageline($subject->IMG, $subject->X, $subject->Y, $subject->X-$mParam, $subject->Y, $subject->RCOLOR)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create line');
- }
- $subject->X -= $mParam;
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour les mouvements vers le haut
- *
- */
- class up implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(!is_numeric($mParam)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid distance '.$mParam);
- }
- $mParam = (int)$mParam;
- if (false === imageline($subject->IMG, $subject->X, $subject->Y, $subject->X, $subject->Y-$mParam, $subject->RCOLOR)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create line');
- }
- $subject->Y -= $mParam;
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour les mouvements vers le bas
- *
- */
- class down implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(!is_numeric($mParam)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid distance '.$mParam);
- }
- $mParam = (int)$mParam;
- if (false === imageline($subject->IMG, $subject->X, $subject->Y, $subject->X, $subject->Y+$mParam, $subject->RCOLOR)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create line');
- }
- $subject->Y += $mParam;
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour la création d'une ellipse
- *
- */
- class ellipse implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(false === strpos($mParam, '-')) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid parameter '.$mParam);
- }
- $aParam = explode('-', $mParam);
- $iWidth = (int)$aParam[0];
- $iHeight = (int)$aParam[1];
- if (false === imageellipse($subject->IMG, $subject->X, $subject->Y, $iWidth, $iHeight, $subject->RCOLOR)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create ellipse');
- }
- $subject->Y += $mParam;
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour la création d'une ellipse remplie
- *
- */
- class filledellipse implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(false === strpos($mParam, '-')) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid parameter '.$mParam);
- }
- $aParam = explode('-', $mParam);
- $iWidth = (int)$aParam[0];
- $iHeight = (int)$aParam[1];
- if (false === imagefilledellipse($subject->IMG, $subject->X, $subject->Y, $iWidth, $iHeight, $subject->RCOLOR)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create filled ellipse');
- }
- $subject->Y += $mParam;
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour la création d'un arc remplis
- *
- */
- class filledarc implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(false === strpos($mParam, '-')) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid parameter '.$mParam);
- }
- $aParam = explode('-', $mParam);
- $iWidth = (int)$aParam[0];
- $iHeight = (int)$aParam[1];
- $iStart = (int)$aParam[2];
- $iEnd = (int)$aParam[3];
- if (false === imagefilledarc($subject->IMG, $subject->X, $subject->Y, $iWidth, $iHeight, $iStart, $iEnd, $subject->RCOLOR, IMG_ARC_PIE)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create filled arc');
- }
- $subject->Y += $mParam;
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour la création d'un rectangle
- *
- */
- class rectangle implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(false === strpos($mParam, '-')) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid parameter '.$mParam);
- }
- $aParam = explode('-', $mParam);
- $iX = (int)$aParam[0];
- $iY = (int)$aParam[1];
- if (false === imagerectangle($subject->IMG, $subject->X, $subject->Y, $iX, $iY, $subject->RCOLOR)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create rectangle');
- }
- $subject->Y += $mParam;
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour la création d'un polygone
- *
- */
- class polygon implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(false === strpos($mParam, '-')) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid parameter '.$mParam);
- }
- $aParam = array_map ('intval', explode('-', $mParam));
- $iCount = count($aParam);
- if(0 !== $iCount%2) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : total points must be even : '.$iCount);
- }
- $iCount /= 2;
- if (false === imagepolygon($subject->IMG, $aParam, $iCount, $subject->RCOLOR)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create polygon');
- }
- $subject->Y += $mParam;
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour la couleur du trait
- *
- */
- class color implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(!is_numeric($mParam) || 9 !== strlen($mParam)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid color '.$mParam);
- }
- $iRed = (int)substr($mParam, 0, 3);
- $iGreen = (int)substr($mParam, 3, 3);
- $iBlue = (int)substr($mParam, 6, 3);
- $subject->RCOLOR = imagecolorallocate($subject->IMG, $iRed, $iGreen, $iBlue);
- $subject->SCOLOR = $mParam;
- if(false === $subject->RCOLOR) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to allocate color '.$mParam);
- }
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour la position X
- *
- */
- class x implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(!is_numeric($mParam)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid position '.$mParam);
- }
- $mParam = (int)$mParam;
- $subject->X = $mParam;
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour la position Y
- *
- */
- class y implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(!is_numeric($mParam)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid position '.$mParam);
- }
- $mParam = (int)$mParam;
- $subject->Y = $mParam;
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour l'épaisseur du trait
- *
- */
- class pixel implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(!is_numeric($mParam)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid position '.$mParam);
- }
- $mParam = (int)$mParam;
- if (false === imagesetthickness($subject->IMG, $mParam)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to set thickness '.$mParam);
- }
- $subject->SPIXEL = $mParam;
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour initialiser la taille de la police de caractère (1-5)
- *
- */
- class fontsize implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(!is_numeric($mParam) || $mParam < 1 || $mParam > 5) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid font size '.$mParam);
- }
- $mParam = (int)$mParam;
- $subject->FONTSIZE = $mParam;
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour l'écriture d'un texte
- *
- */
- class text implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(0 !== strpos($mParam, '"') || (strlen($mParam) - 1) !== strrpos($mParam, '"')) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid text value '.$mParam);
- }
- $mParam = trim($mParam, '"');
- if (false === imagestring($subject->IMG, $subject->FONTSIZE, $subject->X, $subject->Y, $mParam, $subject->RCOLOR)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to write text '.$mParam);
- }
- }
- }
-
- /**
- * Une EXPRESSION CONCRETE pour la sauvegarde de l'image sous un nom particulier
- *
- */
- class file implements expression {
- public static function interpret(interpreter $subject, $mParam) {
- if(!is_string($mParam)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid filename '.$mParam);
- }
- if (false === imagepng($subject->IMG, $mParam)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to ssave image to file '.$mParam);
- }
- }
- }
-
- /**
- * Notre INTERPRETER
- *
- */
- class interpreter {
- private $iX = 200;
- private $iY = 200;
- private $sColor;
- private $iWidth = 500;
- private $iHeight = 500;
- private $iThickNess = 1;
- private $iFontSize = 1;
- private $aDefaultDim = array(500,500);
- private $oExpressionStack;
- private $imh;
- private $colorh;
-
- private $aCor = array(
- 'X' => array('cor' => 'iX', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
- 'Y' => array('cor' => 'iY', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
- 'COLOR' => array('cor' => 'sColor', 'type' => 'is_string', 'gettable' => true, 'settable' => true),
- 'SCOLOR' => array('cor' => 'sColor', 'type' => 'is_string', 'gettable' => true, 'settable' => true),
- 'RCOLOR' => array('cor' => 'colorh', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
- 'WIDTH' => array('cor' => 'iWidth', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
- 'HEIGHT' => array('cor' => 'iHeight', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
- 'PIXEL' => array('cor' => 'iThickNess', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
- 'FONTSIZE' => array('cor' => 'iFontSize', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
- 'SPIXEL' => array('cor' => 'iThickNess', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
- 'IMG' => array('cor' => 'imh', 'type' => 'is_resource', 'gettable' => true, 'settable' => false)
- );
-
- public function __construct($sFile = null) {
- if(!is_null($sFile)) {
- $this->imh = @imagecreatefrompng($sFile);
- if(!$this->imh) {
- throw new functionnalException(__CLASS__.'::'.__FUNCTION__.'() : '.$sFile.' has not been found');
- }
- } else {
- $this->imh = imagecreatetruecolor($this->iWidth, $this->iHeight);
- imagecolorallocate($this->imh, 0, 0, 0);
- }
- }
-
- public function interpret ($sChaine) {
- if(!is_string($sChaine)) {
- throw new functionnalException(__CLASS__.'::'.__FUNCTION__.'() : Parameter must be a string');
- }
- $this->oExpressionStack = new ArrayIterator(preg_split('`("[^"]+")|[\s]+`', $sChaine, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE));
- $this->evaluate();
- }
-
- private function evaluate() {
- while($this->oExpressionStack->valid()) {
- $sToken = $this->oExpressionStack->current();
- if(!class_exists($sToken)) {
- throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid expression '.$sToken);
- }
- $this->oExpressionStack->next();
- $mParam = $this->oExpressionStack->current();
- $interpreter = new ReflectionMethod($sToken, 'interpret');
- $interpreter->invoke(null, $this, $mParam);
- $this->oExpressionStack->next();
- }
- }
-
- public function getMove() {
- if(true === headers_sent()) {
- throw new functionnalException(__CLASS__.'::'.__FUNCTION__.'() : Unable to display image, headers already sent');
- }
- header('Content-type: image/png');
- imagepng($this->imh);
- }
-
- public function getSavedMove($sFile) {
- imagepng($this->imh, $sFile);
- }
-
- public function __get($sProp) {
- if(!array_key_exists($sProp, $this->aCor) || !isset($this->aCor[$sProp]['gettable']) || false === $this->aCor[$sProp]['gettable']) {
- throw new functionnalException(__CLASS__.'::'.__FUNCTION__.'() : Property '.$sProp.' is not gettable');
- }
- $sThisProp = $this->aCor[$sProp]['cor'];
- return $this->$sThisProp;
- }
-
- public function __set($sProp, $mVal) {
- if(!array_key_exists($sProp, $this->aCor) || !isset($this->aCor[$sProp]['settable']) || false === $this->aCor[$sProp]['settable'] || !isset($this->aCor[$sProp]['type'])) {
- throw new functionnalException(__CLASS__.'::'.__FUNCTION__.'() : Property '.$sProp.' is not settable');
- }
- $sFunc = $this->aCor[$sProp]['type'];
- if(!$sFunc($mVal)) {
- throw new functionnalException(__CLASS__.'::'.__FUNCTION__.'() : Value '.$mVal.' is not a valid value for '.$sProp.'; waited : '.substr($sFunc, 3));
- }
- if($sProp === 'COLOR') {
- color::interpret($this, $mVal);
- $this->sColor = $mVal;
- } elseif($sProp === 'PIXEL') {
- pixel::interpret($this, $mVal);
- $this->iThickNess = (int)$mVal;
- } elseif($sProp === 'FONTSIZE') {
- fontsize::interpret($this, $mVal);
- $this->iFontSize = (int)$mVal;
- } else {
- $sThisProp = $this->aCor[$sProp]['cor'];
- $this->$sThisProp = $mVal;
- }
- }
- }
- ?>
<?php
/**
* Notre INTERFACE EXPRESSION
*
*/
interface expression {
public static function interpret(interpreter $subject, $mParam);
}
/**
* Une EXPRESSION CONCRETE pour les mouvements vers la droite
*
*/
class right implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(!is_numeric($mParam)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid distance '.$mParam);
}
$mParam = (int)$mParam;
if (false === imageline($subject->IMG, $subject->X, $subject->Y, $subject->X+$mParam, $subject->Y, $subject->RCOLOR)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create line');
}
$subject->X += $mParam;
}
}
/**
* Une EXPRESSION CONCRETE pour les mouvements vers la gauche
*
*/
class left implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(!is_numeric($mParam)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid distance '.$mParam);
}
$mParam = (int)$mParam;
if (false === imageline($subject->IMG, $subject->X, $subject->Y, $subject->X-$mParam, $subject->Y, $subject->RCOLOR)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create line');
}
$subject->X -= $mParam;
}
}
/**
* Une EXPRESSION CONCRETE pour les mouvements vers le haut
*
*/
class up implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(!is_numeric($mParam)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid distance '.$mParam);
}
$mParam = (int)$mParam;
if (false === imageline($subject->IMG, $subject->X, $subject->Y, $subject->X, $subject->Y-$mParam, $subject->RCOLOR)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create line');
}
$subject->Y -= $mParam;
}
}
/**
* Une EXPRESSION CONCRETE pour les mouvements vers le bas
*
*/
class down implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(!is_numeric($mParam)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid distance '.$mParam);
}
$mParam = (int)$mParam;
if (false === imageline($subject->IMG, $subject->X, $subject->Y, $subject->X, $subject->Y+$mParam, $subject->RCOLOR)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create line');
}
$subject->Y += $mParam;
}
}
/**
* Une EXPRESSION CONCRETE pour la création d'une ellipse
*
*/
class ellipse implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(false === strpos($mParam, '-')) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid parameter '.$mParam);
}
$aParam = explode('-', $mParam);
$iWidth = (int)$aParam[0];
$iHeight = (int)$aParam[1];
if (false === imageellipse($subject->IMG, $subject->X, $subject->Y, $iWidth, $iHeight, $subject->RCOLOR)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create ellipse');
}
$subject->Y += $mParam;
}
}
/**
* Une EXPRESSION CONCRETE pour la création d'une ellipse remplie
*
*/
class filledellipse implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(false === strpos($mParam, '-')) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid parameter '.$mParam);
}
$aParam = explode('-', $mParam);
$iWidth = (int)$aParam[0];
$iHeight = (int)$aParam[1];
if (false === imagefilledellipse($subject->IMG, $subject->X, $subject->Y, $iWidth, $iHeight, $subject->RCOLOR)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create filled ellipse');
}
$subject->Y += $mParam;
}
}
/**
* Une EXPRESSION CONCRETE pour la création d'un arc remplis
*
*/
class filledarc implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(false === strpos($mParam, '-')) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid parameter '.$mParam);
}
$aParam = explode('-', $mParam);
$iWidth = (int)$aParam[0];
$iHeight = (int)$aParam[1];
$iStart = (int)$aParam[2];
$iEnd = (int)$aParam[3];
if (false === imagefilledarc($subject->IMG, $subject->X, $subject->Y, $iWidth, $iHeight, $iStart, $iEnd, $subject->RCOLOR, IMG_ARC_PIE)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create filled arc');
}
$subject->Y += $mParam;
}
}
/**
* Une EXPRESSION CONCRETE pour la création d'un rectangle
*
*/
class rectangle implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(false === strpos($mParam, '-')) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid parameter '.$mParam);
}
$aParam = explode('-', $mParam);
$iX = (int)$aParam[0];
$iY = (int)$aParam[1];
if (false === imagerectangle($subject->IMG, $subject->X, $subject->Y, $iX, $iY, $subject->RCOLOR)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create rectangle');
}
$subject->Y += $mParam;
}
}
/**
* Une EXPRESSION CONCRETE pour la création d'un polygone
*
*/
class polygon implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(false === strpos($mParam, '-')) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid parameter '.$mParam);
}
$aParam = array_map ('intval', explode('-', $mParam));
$iCount = count($aParam);
if(0 !== $iCount%2) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : total points must be even : '.$iCount);
}
$iCount /= 2;
if (false === imagepolygon($subject->IMG, $aParam, $iCount, $subject->RCOLOR)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to create polygon');
}
$subject->Y += $mParam;
}
}
/**
* Une EXPRESSION CONCRETE pour la couleur du trait
*
*/
class color implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(!is_numeric($mParam) || 9 !== strlen($mParam)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid color '.$mParam);
}
$iRed = (int)substr($mParam, 0, 3);
$iGreen = (int)substr($mParam, 3, 3);
$iBlue = (int)substr($mParam, 6, 3);
$subject->RCOLOR = imagecolorallocate($subject->IMG, $iRed, $iGreen, $iBlue);
$subject->SCOLOR = $mParam;
if(false === $subject->RCOLOR) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to allocate color '.$mParam);
}
}
}
/**
* Une EXPRESSION CONCRETE pour la position X
*
*/
class x implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(!is_numeric($mParam)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid position '.$mParam);
}
$mParam = (int)$mParam;
$subject->X = $mParam;
}
}
/**
* Une EXPRESSION CONCRETE pour la position Y
*
*/
class y implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(!is_numeric($mParam)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid position '.$mParam);
}
$mParam = (int)$mParam;
$subject->Y = $mParam;
}
}
/**
* Une EXPRESSION CONCRETE pour l'épaisseur du trait
*
*/
class pixel implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(!is_numeric($mParam)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid position '.$mParam);
}
$mParam = (int)$mParam;
if (false === imagesetthickness($subject->IMG, $mParam)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to set thickness '.$mParam);
}
$subject->SPIXEL = $mParam;
}
}
/**
* Une EXPRESSION CONCRETE pour initialiser la taille de la police de caractère (1-5)
*
*/
class fontsize implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(!is_numeric($mParam) || $mParam < 1 || $mParam > 5) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid font size '.$mParam);
}
$mParam = (int)$mParam;
$subject->FONTSIZE = $mParam;
}
}
/**
* Une EXPRESSION CONCRETE pour l'écriture d'un texte
*
*/
class text implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(0 !== strpos($mParam, '"') || (strlen($mParam) - 1) !== strrpos($mParam, '"')) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid text value '.$mParam);
}
$mParam = trim($mParam, '"');
if (false === imagestring($subject->IMG, $subject->FONTSIZE, $subject->X, $subject->Y, $mParam, $subject->RCOLOR)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to write text '.$mParam);
}
}
}
/**
* Une EXPRESSION CONCRETE pour la sauvegarde de l'image sous un nom particulier
*
*/
class file implements expression {
public static function interpret(interpreter $subject, $mParam) {
if(!is_string($mParam)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid filename '.$mParam);
}
if (false === imagepng($subject->IMG, $mParam)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : Failed to ssave image to file '.$mParam);
}
}
}
/**
* Notre INTERPRETER
*
*/
class interpreter {
private $iX = 200;
private $iY = 200;
private $sColor;
private $iWidth = 500;
private $iHeight = 500;
private $iThickNess = 1;
private $iFontSize = 1;
private $aDefaultDim = array(500,500);
private $oExpressionStack;
private $imh;
private $colorh;
private $aCor = array(
'X' => array('cor' => 'iX', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
'Y' => array('cor' => 'iY', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
'COLOR' => array('cor' => 'sColor', 'type' => 'is_string', 'gettable' => true, 'settable' => true),
'SCOLOR' => array('cor' => 'sColor', 'type' => 'is_string', 'gettable' => true, 'settable' => true),
'RCOLOR' => array('cor' => 'colorh', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
'WIDTH' => array('cor' => 'iWidth', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
'HEIGHT' => array('cor' => 'iHeight', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
'PIXEL' => array('cor' => 'iThickNess', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
'FONTSIZE' => array('cor' => 'iFontSize', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
'SPIXEL' => array('cor' => 'iThickNess', 'type' => 'is_int', 'gettable' => true, 'settable' => true),
'IMG' => array('cor' => 'imh', 'type' => 'is_resource', 'gettable' => true, 'settable' => false)
);
public function __construct($sFile = null) {
if(!is_null($sFile)) {
$this->imh = @imagecreatefrompng($sFile);
if(!$this->imh) {
throw new functionnalException(__CLASS__.'::'.__FUNCTION__.'() : '.$sFile.' has not been found');
}
} else {
$this->imh = imagecreatetruecolor($this->iWidth, $this->iHeight);
imagecolorallocate($this->imh, 0, 0, 0);
}
}
public function interpret ($sChaine) {
if(!is_string($sChaine)) {
throw new functionnalException(__CLASS__.'::'.__FUNCTION__.'() : Parameter must be a string');
}
$this->oExpressionStack = new ArrayIterator(preg_split('`("[^"]+")|[\s]+`', $sChaine, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE));
$this->evaluate();
}
private function evaluate() {
while($this->oExpressionStack->valid()) {
$sToken = $this->oExpressionStack->current();
if(!class_exists($sToken)) {
throw new languageException(__CLASS__.'::'.__FUNCTION__.'() : invalid expression '.$sToken);
}
$this->oExpressionStack->next();
$mParam = $this->oExpressionStack->current();
$interpreter = new ReflectionMethod($sToken, 'interpret');
$interpreter->invoke(null, $this, $mParam);
$this->oExpressionStack->next();
}
}
public function getMove() {
if(true === headers_sent()) {
throw new functionnalException(__CLASS__.'::'.__FUNCTION__.'() : Unable to display image, headers already sent');
}
header('Content-type: image/png');
imagepng($this->imh);
}
public function getSavedMove($sFile) {
imagepng($this->imh, $sFile);
}
public function __get($sProp) {
if(!array_key_exists($sProp, $this->aCor) || !isset($this->aCor[$sProp]['gettable']) || false === $this->aCor[$sProp]['gettable']) {
throw new functionnalException(__CLASS__.'::'.__FUNCTION__.'() : Property '.$sProp.' is not gettable');
}
$sThisProp = $this->aCor[$sProp]['cor'];
return $this->$sThisProp;
}
public function __set($sProp, $mVal) {
if(!array_key_exists($sProp, $this->aCor) || !isset($this->aCor[$sProp]['settable']) || false === $this->aCor[$sProp]['settable'] || !isset($this->aCor[$sProp]['type'])) {
throw new functionnalException(__CLASS__.'::'.__FUNCTION__.'() : Property '.$sProp.' is not settable');
}
$sFunc = $this->aCor[$sProp]['type'];
if(!$sFunc($mVal)) {
throw new functionnalException(__CLASS__.'::'.__FUNCTION__.'() : Value '.$mVal.' is not a valid value for '.$sProp.'; waited : '.substr($sFunc, 3));
}
if($sProp === 'COLOR') {
color::interpret($this, $mVal);
$this->sColor = $mVal;
} elseif($sProp === 'PIXEL') {
pixel::interpret($this, $mVal);
$this->iThickNess = (int)$mVal;
} elseif($sProp === 'FONTSIZE') {
fontsize::interpret($this, $mVal);
$this->iFontSize = (int)$mVal;
} else {
$sThisProp = $this->aCor[$sProp]['cor'];
$this->$sThisProp = $mVal;
}
}
}
?>
Conclusion
Juste pour dire parce que c'est toujours agréable : ce code a terminé 6ème des Innovation Awards de novembre 2007 sur phpclasses.org :-)
Historique
- 07 octobre 2007 16:02:52 :
- Ajout de 3 commandes :
ellipse
rectangle
polygon
- 07 octobre 2007 16:03:30 :
- oublié le code...
- 08 octobre 2007 22:46:11 :
- Tite modif
- 08 octobre 2007 23:07:25 :
- Compatibilité IE... (IE7 en tous cas).
- 01 novembre 2007 10:33:20 :
- Rajout des modifications apportées par _KLESK : filledellipse et filledarc
Rajout de 2 autres commandes :
fontsize suivi d'un entier compris entre 1 et 5 fixe la taille de la police de caractère
text suivi d'une chaîne délimitée par des guillemets (text "Hello World" par exemple) pour écrire ce texte
- 01 novembre 2007 10:33:43 :
- Rajout des modifications apportées par _KLESK : filledellipse et filledarc
Rajout de 2 autres commandes :
fontsize suivi d'un entier compris entre 1 et 5 fixe la taille de la police de caractère
text suivi d'une chaîne délimitée par des guillemets (text "Hello World" par exemple) pour écrire ce texte
- 01 novembre 2007 10:34:35 :
- Oubli du code :-)
- 01 janvier 2008 10:16:34 :
- RAS
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
aperçu avant impression, ajouter une image [ par cciiia ]
BONJOUR,Voilà mon souci j'aimerai ajouter le logo de mon entreprise juste à l'inpression.Car le logo n'est pas sur ma page php.Je pense qu'i
Chemin de l'image dans mysql [ par fabienfsf ]
Bonjour,Je voudrais faire en faite j'ai cunçu une petite radio ou on peu ecouter une radio que l'on veut sur le net alord pour sa j'ai fait uneta
affichage d'une image [ par refkaben ]
Bonjour,Je developpe un site ou les utilisateurs ont un formulaire, dans lequel il va donner le chemin d'une image, cette image sera upploadée, p
php->pdf->probleme image [ par younes371 ]
Bonjour, J'ai trouvé un code ds phpcs qui m a été très utile pour mon application,j'ai pu afficher les resultats de la requette dans le tableau(Numero
lier le formulaire upload avec un autre formulaire [ par maxwellcs ]
Bonsoir a tous!! Déja une petite question est-il possible de rassembler dans un meme formulaire , un upload et des donnees a saisir?? Si la r&
Debutant en Ajax [ par TheGorgo ]
Bonjour, Je cherche de l'aide pour un script en ajax. J'ai cherché des tutoriaux un peu partout, mais je ne comprends toujours pas. Je pense que ce q
image avec FPDF [ par sebalex ]
Bonsoir à toutes et à tous, Voici un bout de mon code. Avec ce code (FPDF), je crée un document PDF. A présent, je souhaite qu'en fonction de la base
actualiser une image clicable [ par craso ]
Bonjour,j'ai posté cette question dans la partie ajax car je crois que je vais devoir utiliser ajax pour faire ce que je veux.Je souhaite actualiser u
mon script crée mes fichiers dans un endroit inattendu [ par angelimad ]
bonjour tout le monde. mon problème c'est que j'ai un site php hébergé sur un serveur Linux. mon script upload le fichier via le formulaire du client
Upload d'images : prévisualisation avant upload avec php+ajax+javascript [ par amewole ]
Bonjour à vous tous, Je suis à la recherche d'un script php+ajax+javascript permettant de faire un preview des images avant le upload c'est à dire qu
|
Derniers Blogs
GESTION D'EXCEPTION AVEC LES TASKSGESTION D'EXCEPTION AVEC LES TASKS par richardc
Nous avons vu dans un précédent article comment utiliser Task pour effectuer des opérations dans un autre thread.
Malheureusement, comme tout le monde n'est pas parfait, il se peut que cette exécution se passe mal et qu'une exception se produise.
La...
Cliquez pour lire la suite de l'article par richardc DéMARRONS AVEC LES TASKSDéMARRONS AVEC LES TASKS par richardc
Que vous le vouliez ou non, le développement multi-tâche est maintenant une obligation pour toute nouvelle application. Il est donc vital d'en comprendre les mécanismes et de s'y mettre le plus tôt possible.
En attendant le .NET Framework 4.5 avec le...
Cliquez pour lire la suite de l'article par richardc SLIDE & DéMO TECHDAYS 2012 - FAST & FURIOUS XAML APPSSLIDE & DéMO TECHDAYS 2012 - FAST & FURIOUS XAML APPS par Vko
Retrouvez les slides et les démo de ma session Fast & Furious XAML Apps. A ceux qui se posent la question : "est-ce que le code de la DataGrid est disponible?", je vous répondrais "pas encore". Je vais mettre en place un projet codeplex pour part...
Cliquez pour lire la suite de l'article par Vko XNA IS DEAD!XNA IS DEAD! par richardc
Depuis la semaine dernière (et grâce aux TechDays 2012), je me penche activement sur la nouvelle version de Windows, aka Windows 8. Vous me direz, il était temps puisque la première preview date de Septembre dernier.
OK. Remarquez, on n'en est qu'aux...
Cliquez pour lire la suite de l'article par richardc TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 !TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 ! par ROMELARD Fabrice
Speakers: Fabrice Meillon et Stanislas Quastana Cette session est basée entièrement sur celle donnée lors de la BUILD cet hiver. Il n'y a pas d'ajout d'information en rapport avec cet évènement passé. Windows 8 Server sera intégralem...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
SYSTEME D'AMISYSTEME D'AMI par moza2409
Cliquez pour lire la suite par moza2409
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|