├── CaptchaAction.php
├── README.md
├── composer.json
├── equations
├── AddSub.php
├── Division.php
├── Fraction.php
├── Integrate1.php
├── LimitFnt.php
├── LimitIfnt1.php
├── LimitIfnt2.php
├── Multiply.php
├── Polynom2.php
└── classes.php
├── fonts
├── COPYING
├── FreeSerif.ttf
├── cmex10.ttf
├── cmmi10.ttf
├── cmr10.ttf
├── index.php
└── msam10.ttf
└── mathpublisher.php
/CaptchaAction.php:
--------------------------------------------------------------------------------
1 |
14 | * @since 1.0
15 | */
16 | class CaptchaAction extends \yii\captcha\CaptchaAction
17 | {
18 | const JPEG_FORMAT = 'jpeg';
19 | const PNG_FORMAT = 'png';
20 |
21 | /**
22 | * Avaliable value are 'jpeg' or 'png'
23 | * @var string
24 | */
25 | public $imageFormat = self::JPEG_FORMAT;
26 | /**
27 | * Dificully level
28 | * @var int
29 | */
30 | public $level;
31 | /**
32 | * Font size.
33 | * @var int
34 | */
35 | public $size = 14;
36 | /**
37 | * Allow decimal
38 | * @var boolean
39 | */
40 | public $allowDecimal = false;
41 | /**
42 | * Registered equation class
43 | * @var array
44 | */
45 | public static $classes;
46 |
47 | /**
48 | * @inheritdoc
49 | */
50 | public function init()
51 | {
52 | if ($this->level === null) {
53 | $this->level = ArrayHelper::getValue(\Yii::$app->params, 'mdm.captcha.level', 1);
54 | }
55 | }
56 |
57 | /**
58 | * @inheritdoc
59 | */
60 | public function run()
61 | {
62 | if (Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR) !== null) {
63 | // AJAX request for regenerating code
64 | $code = $this->getVerifyCode(true);
65 | Yii::$app->response->format = Response::FORMAT_JSON;
66 | return [
67 | 'hash1' => $this->generateValidationHash($code),
68 | 'hash2' => $this->generateValidationHash(strtolower($code)),
69 | // we add a random 'v' parameter so that FireFox can refresh the image
70 | // when src attribute of image tag is changed
71 | 'url' => Url::to([$this->id, 'v' => uniqid()]),
72 | ];
73 | } else {
74 | $this->setHttpHeaders();
75 | Yii::$app->response->format = Response::FORMAT_RAW;
76 | return $this->renderImage($this->getVerifyCode(false, true));
77 | }
78 | }
79 |
80 | /**
81 | * @inheritdoc
82 | */
83 | public function getVerifyCode($regenerate = false, $code = false)
84 | {
85 | $session = Yii::$app->getSession();
86 | $session->open();
87 | $name = $this->getSessionKey();
88 | if ($session[$name] === null || $regenerate) {
89 | $session[$name . 'code'] = $this->generateVerifyCode();
90 | $session[$name] = $this->getValue($session[$name . 'code']);
91 | $session[$name . 'count'] = 1;
92 | }
93 |
94 | return $code ? $session[$name . 'code'] : $session[$name];
95 | }
96 |
97 | /**
98 | * @inheritdoc
99 | */
100 | public function validate($input, $caseSensitive)
101 | {
102 | $code = $this->getVerifyCode(false, true);
103 | $value = $this->getValue($code);
104 | if ($this->allowDecimal) {
105 | $valid = abs(round($input, 2) - round($value, 2)) <= 0.02;
106 | } else {
107 | $valid = $input == $value;
108 | }
109 |
110 | $session = Yii::$app->getSession();
111 | $session->open();
112 | $name = $this->getSessionKey() . 'count';
113 | $session[$name] = $session[$name] + 1;
114 | if ($valid || $session[$name] > $this->testLimit && $this->testLimit > 0) {
115 | $this->getVerifyCode(true);
116 | }
117 |
118 | return $valid;
119 | }
120 |
121 | /**
122 | * @inheritdoc
123 | */
124 | protected function generateVerifyCode()
125 | {
126 | mt_srand(time());
127 | $code = [mt_rand(0, 100)];
128 | for ($i = 1; $i <= 5; $i++) {
129 | $code[$i] = mt_rand(0, 10);
130 | }
131 |
132 | return $code;
133 | }
134 |
135 | /**
136 | * @inheritdoc
137 | */
138 | protected function renderImage($code)
139 | {
140 | require __DIR__ . '/mathpublisher.php';
141 |
142 | $formula = new \expression_math(tableau_expression(trim($this->getExpresion($code))));
143 | $formula->dessine($this->size);
144 |
145 | ob_start();
146 | switch ($this->imageFormat) {
147 | case self::JPEG_FORMAT:
148 | imagejpeg($formula->image);
149 | break;
150 | case self::PNG_FORMAT:
151 | imagepng($formula->image);
152 | break;
153 | }
154 | imagedestroy($formula->image);
155 |
156 | return ob_get_clean();
157 | }
158 |
159 | /**
160 | * Sets the HTTP headers needed by image response.
161 | */
162 | protected function setHttpHeaders()
163 | {
164 | Yii::$app->getResponse()->getHeaders()
165 | ->set('Pragma', 'public')
166 | ->set('Expires', '0')
167 | ->set('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
168 | ->set('Content-Transfer-Encoding', 'binary')
169 | ->set('Content-type', "image/{$this->imageFormat}");
170 | }
171 |
172 | /**
173 | * Get expresion formula .
174 | * @param array $code
175 | * @return string
176 | */
177 | protected function getExpresion($code)
178 | {
179 | if ($this->fixedVerifyCode !== null) {
180 | return $this->fixedVerifyCode;
181 | }
182 | $class = static::$classes[$this->level][$code[0] % count(static::$classes[$this->level])];
183 | return $class::getExpresion($code, $this->allowDecimal);
184 | }
185 |
186 | /**
187 | * Get value of formula
188 | * @param array $code
189 | * @return int|float
190 | */
191 | protected function getValue($code)
192 | {
193 | if ($this->fixedVerifyCode !== null) {
194 | return $this->fixedVerifyCode;
195 | }
196 | $class = static::$classes[$this->level][$code[0] % count(static::$classes[$this->level])];
197 | return $class::getValue($code, $this->allowDecimal);
198 | }
199 | }
200 |
201 | //
202 | CaptchaAction::$classes = require(__DIR__ . '/equations/classes.php');
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Captcha With Math Equation
2 | ==========================
3 |
4 | Installation
5 | ------------
6 |
7 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
8 |
9 | Either run
10 |
11 | ```
12 | php composer.phar require --prefer-dist mdmsoft/yii2-captcha "~1.0"
13 | ```
14 |
15 | or add
16 |
17 | ```
18 | "mdmsoft/yii2-captcha": "~1.0"
19 | ```
20 |
21 | to the require section of your `composer.json` file.
22 |
23 |
24 | Usage
25 | -----
26 |
27 | Once the extension is installed, simply modify your controler, add or change methode `actions()`:
28 |
29 | ```php
30 | public function actions()
31 | {
32 | return [
33 | ...
34 | 'captcha' => [
35 | 'class' => 'mdm\captcha\CaptchaAction',
36 | 'level' => 3, // avaliable level are 1,2,3 :D
37 | ],
38 | ];
39 | }
40 | ```
41 |
42 | In view
43 | ```php
44 | =
45 | $form->field($model, 'verifyCode')->widget(Captcha::className(), [
46 | 'template' => '
',
47 | ])
48 | ?>
49 |
50 | ```
51 | 
52 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mdmsoft/yii2-captcha",
3 | "description": "Captcha with math equation",
4 | "keywords": ["yii2", "captcha", "math"],
5 | "type": "yii2-extension",
6 | "license": "BSD-3-Clause",
7 | "support": {
8 | "issues": "https://github.com/mdmsoft/yii2-captcha/issues",
9 | "source": "https://github.com/mdmsoft/yii2-captcha"
10 | },
11 | "authors": [
12 | {
13 | "name": "Misbahul D Munir",
14 | "email": "misbahuldmunir@gmail.com"
15 | }
16 | ],
17 | "require": {
18 | "yiisoft/yii2": "~2.0"
19 | },
20 | "autoload": {
21 | "psr-4": {
22 | "mdm\\captcha\\": ""
23 | }
24 | },
25 | "extra": {
26 | "branch-alias": {
27 | "dev-master": "1.x-dev"
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/equations/AddSub.php:
--------------------------------------------------------------------------------
1 |
10 | * @since 1.0
11 | */
12 | class AddSub
13 | {
14 |
15 | protected static function format($code)
16 | {
17 | $a = $code[1] + $code[2] + $code[3] * $code[5];
18 | $b = $code[3] + $code[4] + 12;
19 | $c = $code[2] + $code[4] + $code[5] + 1;
20 |
21 | return [$a, $b, $c];
22 | }
23 |
24 | public static function getExpresion($code)
25 | {
26 | list($a, $b, $c) = static::format($code);
27 | return "{$a}+{$b}-{$c}";
28 | }
29 |
30 | public static function getValue($code)
31 | {
32 | list($a, $b, $c) = static::format($code);
33 | return $a + $b - $c;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/equations/Division.php:
--------------------------------------------------------------------------------
1 |
9 | * @since 1.0
10 | */
11 | class Division
12 | {
13 |
14 | protected static function format($code)
15 | {
16 | $a = $code[1] + $code[2] + $code[3] * $code[5];
17 | $b = $code[3] + $code[4] + 12;
18 | $c = $code[2] + $code[4] + $code[5] + 1;
19 |
20 | return [$a + $b - $c, $c];
21 | }
22 |
23 | public static function getExpresion($code, $decimal = false)
24 | {
25 | list($a, $b) = static::format($code);
26 | $a = $decimal ? $a : $a * $b;
27 | return "{$a} / {$b}";
28 | }
29 |
30 | public static function getValue($code, $decimal = false)
31 | {
32 | list($a, $b) = static::format($code);
33 | return $decimal ? 1.0 * $a / $b : $a;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/equations/Fraction.php:
--------------------------------------------------------------------------------
1 |
13 | * @since 1.0
14 | */
15 | class Fraction
16 | {
17 |
18 | protected static function format($code)
19 | {
20 | $a = $code[1] + $code[2] + 1;
21 | $b = $code[3] + $code[4] + 1;
22 | $c = $code[4] + $code[5] + 1;
23 |
24 | return [$a, $b, $c];
25 | }
26 |
27 | public static function getExpresion($code, $decimal = false)
28 | {
29 | list($a, $b, $c) = static::format($code);
30 | $a += $b;
31 |
32 | $c2 = $c + 2;
33 | $midle = $c2 > $b ? '+ ' . ($c2 - $b) . "*{$a}" : ($c2 < $b ? '- ' . ($b - $c2) . "*{$a}" : '');
34 | $bc = $b * $c2;
35 |
36 | if ($decimal) {
37 | $c += 3;
38 | }
39 | $a2 = $a + 2;
40 | return "{{$a}^2 {$midle} - {$bc}} / {{$a2} + {$c}}";
41 | }
42 |
43 | public static function getValue($code, $decimal = false)
44 | {
45 | list($a, $b, $c) = static::format($code);
46 | if ($decimal) {
47 | $a += $b;
48 | return 1.0 * ($a - $b) * ($a + 2 + $c) / ($a + $c + 5);
49 | } else {
50 | return $a;
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/equations/Integrate1.php:
--------------------------------------------------------------------------------
1 |
15 | * @since 1.0
16 | */
17 | class Integrate1
18 | {
19 |
20 | public static function format($code)
21 | {
22 | $a = $code[1] + 1;
23 | $b = $code[2] + 1;
24 | $c = $code[4] + 1;
25 |
26 | return [$a, $b, $c];
27 | }
28 |
29 | public static function getExpresion($code, $decimal = false)
30 | {
31 | list($a, $b, $c) = static::format($code);
32 | $b *= 2;
33 | $f = $decimal ? '' : '3';
34 | return "int{0}{{$a}}{({$f}x^2 + {$b}x + {$c}) dx}";
35 | }
36 |
37 | public static function getValue($code, $decimal = false)
38 | {
39 | list($a, $b, $c) = static::format($code);
40 | $f = $decimal ? 1.0 / 3 : 1;
41 | return ($a * $a * $a) * $f + $b * $a * $a + $c * $a;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/equations/LimitFnt.php:
--------------------------------------------------------------------------------
1 | a, (x^2 + (c-b)x - b*c)/(x - b)]
13 | *
14 | * @author Misbahul D Munir
15 | * @since 1.0
16 | */
17 | class LimitFnt
18 | {
19 |
20 | protected static function format($code)
21 | {
22 | $a = $code[1] + $code[2] + 1;
23 | $b = $code[3] + $code[4] + 1;
24 | $c = $code[4] + $code[5] + 1;
25 |
26 | return [$a, $b, $c];
27 | }
28 |
29 | public static function getExpresion($code)
30 | {
31 | list($a, $b, $c) = static::format($code);
32 | $midle = $c > $b ? '+ ' . ($c - $b) . 'x' : ($c < $b ? '- ' . ($b - $c) . 'x' : '');
33 | $bc = $b * $c;
34 |
35 | return "lim{x right {$a}}{{x^2 {$midle} - {$bc}}/{x - {$b}}}";
36 | }
37 |
38 | public static function getValue($code)
39 | {
40 | list($a,, $c) = static::format($code);
41 | return $a + $c;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/equations/LimitIfnt1.php:
--------------------------------------------------------------------------------
1 | ~,V(x^2 + ax + c) - V(x^2 + bx + d)]
9 | *
10 | * (a - b)/2
11 | *
12 | * @author Misbahul D Munir
13 | * @since 1.0
14 | */
15 | class LimitIfnt1
16 | {
17 |
18 | protected static function format($code)
19 | {
20 | $a = $code[1] + $code[2] + 10;
21 | $b = $code[3] + $code[4] - 10;
22 | $c = $code[4] + $code[5] + 1;
23 | $d = $code[1] + $code[5] + 1;
24 |
25 | return [$a, $b, $c, $d];
26 | }
27 |
28 | public static function getExpresion($code, $decimal = false)
29 | {
30 | list($a, $b, $c, $d) = static::format($code);
31 | if (!$decimal) {
32 | $a *= 2;
33 | }
34 | $a += $b;
35 | $sb = $b > 0 ? "+ {$b}x" : ($b < 0 ? '- ' . abs($b) . 'x' : '');
36 | $sg1 = $c > 10 ? '+' : '-';
37 | $sg2 = $d > 10 ? '+' : '-';
38 | return "lim{x right infty}{sqrt{x^2 + {$a}x {$sg1} {$c}}-sqrt{x^2 {$sb} {$sg2} {$d}}}";
39 | }
40 |
41 | public static function getValue($code, $decimal = false)
42 | {
43 | list($a,,, ) = static::format($code);
44 | return $decimal ? $a / 2 : $a;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/equations/LimitIfnt2.php:
--------------------------------------------------------------------------------
1 | ~,V(ax^2 + cx + c) / V(x^2 + dx + d)]
9 | *
10 | * Va
11 | *
12 | * @author Misbahul D Munir
13 | * @since 1.0
14 | */
15 | class LimitIfnt2
16 | {
17 |
18 | protected static function format($code)
19 | {
20 | $a = $code[1] + $code[2] + 1;
21 | $b = $code[2] + $code[3] + $code[4] + 1;
22 | $c = $code[1] + $code[4] + $code[5] + 1;
23 | $d = $code[1] + $code[5] + 5;
24 |
25 | return [$a, $b, $c, $d];
26 | }
27 |
28 | public static function getExpresion($code, $decimal = false)
29 | {
30 | list($a, $b, $c, $d) = static::format($code);
31 | if (!$decimal) {
32 | $a = $a * $a;
33 | }
34 | $midle1 = (($b > 15) ? '+' : '-') . " {$b}x + " . ($d - 4);
35 | $midle2 = (($c > 15) ? '+' : '-') . " {$c}x - " . ($d + 4);
36 |
37 | return "lim{x right infty}{{sqrt{{$a}x^2 {$midle1}}}/{sqrt{x^2 {$midle2}}}}";
38 | }
39 |
40 | public static function getValue($code, $decimal = false)
41 | {
42 | list($a,,, ) = static::format($code);
43 | return $decimal ? sqrt($a) : $a;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/equations/Multiply.php:
--------------------------------------------------------------------------------
1 |
11 | * @since 1.0
12 | */
13 | class Multiply
14 | {
15 |
16 | protected static function format($code)
17 | {
18 | $a = $code[1] + $code[2] + $code[3] * $code[5];
19 | $b = $code[3] + $code[4] + 12;
20 | $c = $code[2] + $code[4] + $code[5] + 1;
21 |
22 | return [$a + $b - $c, $c];
23 | }
24 |
25 | public static function getExpresion($code)
26 | {
27 | list($a, $b) = static::format($code);
28 | return "{$a} * {$b}";
29 | }
30 |
31 | public static function getValue($code)
32 | {
33 | list($a, $b) = static::format($code);
34 | return $a * $b;
35 | }
36 | }
--------------------------------------------------------------------------------
/equations/Polynom2.php:
--------------------------------------------------------------------------------
1 |
11 | * @since 1.0
12 | */
13 | class Polynom2
14 | {
15 |
16 | protected static function format($code)
17 | {
18 | $a = $code[1] + $code[2] + 1;
19 | $b = $code[3] + $code[4] + 3;
20 | $c = $code[3] + $code[4] + $code[5] + 1;
21 |
22 | return [$a, $b, $c];
23 | }
24 |
25 | public static function getExpresion($code)
26 | {
27 | list($a, $b, $c) = static::format($code);
28 | $b2 = $b - 2;
29 | return "{$a}^2 + {$b}*{$c} - {$b2}";
30 | }
31 |
32 | public static function getValue($code)
33 | {
34 | list($a, $b, $c) = static::format($code);
35 | return $a * $a + $b * $c - ($b - 2);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/equations/classes.php:
--------------------------------------------------------------------------------
1 | [
6 | 'mdm\captcha\equations\AddSub',
7 | 'mdm\captcha\equations\Multiply',
8 | 'mdm\captcha\equations\Division',
9 | ],
10 | // level 2
11 | '2'=>[
12 | 'mdm\captcha\equations\Polynom2',
13 | 'mdm\captcha\equations\Fraction',
14 | ],
15 | // level 3
16 | '3'=>[
17 | 'mdm\captcha\equations\LimitFnt',
18 | 'mdm\captcha\equations\LimitIfnt1',
19 | 'mdm\captcha\equations\LimitIfnt2',
20 | 'mdm\captcha\equations\Integrate1',
21 | ],
22 | ];
23 |
--------------------------------------------------------------------------------
/fonts/COPYING:
--------------------------------------------------------------------------------
1 | cmex10.ttf, msam10.ttf, cmr10.ttf, cmmi10.ttf : Copyright (C) 1997 American Mathematical Society
2 | FreeSerif.ttf : Copyleft 2002, 2003 Free Software Foundation.
--------------------------------------------------------------------------------
/fonts/FreeSerif.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mdmsoft/yii2-captcha/8fe34320e5c2017be8f1b19fe5454e9877f78e78/fonts/FreeSerif.ttf
--------------------------------------------------------------------------------
/fonts/cmex10.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mdmsoft/yii2-captcha/8fe34320e5c2017be8f1b19fe5454e9877f78e78/fonts/cmex10.ttf
--------------------------------------------------------------------------------
/fonts/cmmi10.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mdmsoft/yii2-captcha/8fe34320e5c2017be8f1b19fe5454e9877f78e78/fonts/cmmi10.ttf
--------------------------------------------------------------------------------
/fonts/cmr10.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mdmsoft/yii2-captcha/8fe34320e5c2017be8f1b19fe5454e9877f78e78/fonts/cmr10.ttf
--------------------------------------------------------------------------------
/fonts/index.php:
--------------------------------------------------------------------------------
1 | header("location:../index.html"); ?>
2 |
--------------------------------------------------------------------------------
/fonts/msam10.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mdmsoft/yii2-captcha/8fe34320e5c2017be8f1b19fe5454e9877f78e78/fonts/msam10.ttf
--------------------------------------------------------------------------------
/mathpublisher.php:
--------------------------------------------------------------------------------
1 | ... tag).
21 | $size is the size of the police used for the formulas.
22 | $pathtoimg is the relative path between the html pages and the images directory.
23 | With a simple "echo mathfilter($text,$size,$pathtoimg);", you can display text with mathematical formulas.
24 | The mathfilter function will replace all the math tags (formula) in $text by
.
25 | Example :
26 | mathfilter("A math formula : f(x)=sqrt{x},12,"img/") will return :
27 | "A math formula :
"
28 | The image corresponding to a formula is created only once. Then the image is stocked into the image directories.
29 | The first time that mathfilter is called, the images corresponding to the formulas are created, but the next times mathfilter will only return the html code.
30 |
31 | NOTE : if the free latex fonts furnished with this script don't work well (very tiny formulas - that's could happened with some GD configurations), you should try to use the bakoma versions of these fonts (downloadable here : http://www.ctan.org/tex-archive/fonts/cm/ps-type1/bakoma/ttf/ )
32 | * ***************************************************************** */
33 |
34 | //********* PARAMETERS TO MODIFY *********************************
35 | // The four global variables. Uncomment the line if you need it.
36 | //global $dirfonts,$dirimg,$symboles,$fontesmath;
37 | // choose the type of the declaration according to your server settings (some servers don't accept the dirname(__FILE__) command for security reasons).
38 | // NEW in 0.3 version : no more / at the end of $dirfonts and $dirimg
39 | // absolute path to the fonts directory
40 | class GlobalVar
41 | {
42 | public static $dirfonts;
43 | public static $dirimg;
44 | public static $symboles;
45 | public static $fontesmath;
46 | }
47 | GlobalVar::$dirfonts = __DIR__ . "/fonts";
48 |
49 | // absolute path to the images directory
50 | GlobalVar::$dirimg = __DIR__ . "/img";
51 |
52 |
53 |
54 | //******************************************************************
55 | GlobalVar::$symboles = array(
56 | '~' => ' ',
57 | 'alpha' => '®',
58 | 'beta' => '¯',
59 | 'gamma' => '°',
60 | 'delta' => '±',
61 | 'epsilon' => '²',
62 | 'varepsilon' => '"',
63 | 'zeta' => '³',
64 | 'eta' => '´',
65 | 'theta' => 'µ',
66 | 'vartheta' => '#',
67 | 'iota' => '¶',
68 | 'kappa' => '·',
69 | 'lambda' => '¸',
70 | 'mu' => '¹',
71 | 'nu' => 'º',
72 | 'xi' => '»',
73 | 'pi' => '¼',
74 | 'varpi' => '$',
75 | 'rho' => '½',
76 | 'varrho' => '%',
77 | 'sigma' => '¾',
78 | 'varsigma' => '&',
79 | 'tau' => '¿',
80 | 'upsilon' => 'À',
81 | 'phi' => 'Á',
82 | 'varphi' => ''',
83 | 'chi' => 'Â',
84 | 'psi' => 'Ã',
85 | 'omega' => '!',
86 | 'Gamma' => '¡',
87 | 'Lambda' => '¤',
88 | 'Sigma' => '§',
89 | 'Psi' => 'ª',
90 | 'Delta' => '¢',
91 | 'Xi' => '¥',
92 | 'Upsilon' => '¨',
93 | 'Omega' => '',
94 | 'Theta' => '£',
95 | 'Pi' => '¦',
96 | 'Phi' => '©',
97 | 'infty' => '∞',
98 | 'ne' => '≠',
99 | '*' => '×',
100 | 'in' => '∈',
101 | 'notin' => '∉',
102 | 'forall' => '∀',
103 | 'exists' => '∃',
104 | 'notexists' => '∄',
105 | 'partial' => '∂',
106 | 'approx' => '≈',
107 | 'left' => '←',
108 | 'right' => '→',
109 | 'leftright' => '↔',
110 | 'doubleleft' => '⇐',
111 | 'doubleright' => '⇒',
112 | 'doubleleftright' => '⇔',
113 | 'nearrow' => '↗',
114 | 'searrow' => '↙',
115 | 'pm' => '±',
116 | 'bbR' => 'ℝ',
117 | 'bbN' => 'ℕ',
118 | 'bbZ' => 'ℤ',
119 | 'bbC' => 'ℂ',
120 | 'inter' => '⋂',
121 | 'union' => '⋃',
122 | 'ortho' => '⊥',
123 | 'parallel' => '∥',
124 | 'backslash' => '\',
125 | 'prime' => ''',
126 | 'wedge' => '⋀',
127 | 'vert' => '∥',
128 | 'subset' => '⊂',
129 | 'notsubset' => '⊄',
130 | 'circ' => '∘',
131 | 'varnothing' => 'ø',
132 | 'cdots' => '⋯',
133 | 'vdots' => '⋮',
134 | 'ddots' => '⋱',
135 | //operateurs
136 | 'le' => '6',
137 | 'ge' => '>',
138 | '<' => '<',
139 | '>' => '>',
140 | //parentheses
141 | '(' => '³',
142 | ')' => '´',
143 | '[' => 'h',
144 | ']' => 'i',
145 | 'lbrace' => '(',
146 | 'rbrace' => ')',
147 | //autres
148 | '_hat' => 'c',
149 | '_racine' => 'q',
150 | '_integrale' => 'R',
151 | '_dintegrale' => '∬',
152 | '_tintegrale' => '∭',
153 | '_ointegrale' => 'H',
154 | '_produit' => 'Q',
155 | '_somme' => 'P',
156 | '_intersection' => 'T',
157 | '_reunion' => 'S',
158 | '_lim' => 'lim',
159 | //fonctions
160 | 'arccos' => 'arccos',
161 | 'ker' => 'ker',
162 | 'arcsin' => 'arcsin',
163 | 'lg' => 'lg',
164 | 'arctan' => 'arctan',
165 | 'arg' => 'arg',
166 | 'cos' => 'cos',
167 | 'cosh' => 'cosh',
168 | 'ln' => 'ln',
169 | 'cot' => 'cot',
170 | 'log' => 'log',
171 | 'coth' => 'coth',
172 | 'max' => 'max',
173 | 'csc' => 'csc',
174 | 'min' => 'min',
175 | 'deg' => 'deg',
176 | 'det' => 'det',
177 | 'sec' => 'sec',
178 | 'dim' => 'dim',
179 | 'sin' => 'sin',
180 | 'exp' => 'exp',
181 | 'sinh' => 'sinh',
182 | 'gcd' => 'gcd',
183 | 'sup' => 'sup',
184 | 'hom' => 'hom',
185 | 'tan' => 'tan',
186 | 'inf' => 'inf',
187 | 'tanh' => 'tanh'
188 | );
189 | GlobalVar::$fontesmath = array(
190 | '~' => 'FreeSerif',
191 | 'alpha' => 'cmmi10',
192 | 'beta' => 'cmmi10',
193 | 'gamma' => 'cmmi10',
194 | 'delta' => 'cmmi10',
195 | 'epsilon' => 'cmmi10',
196 | 'varepsilon' => 'cmmi10',
197 | 'zeta' => 'cmmi10',
198 | 'eta' => 'cmmi10',
199 | 'theta' => 'cmmi10',
200 | 'vartheta' => 'cmmi10',
201 | 'iota' => 'cmmi10',
202 | 'kappa' => 'cmmi10',
203 | 'lambda' => 'cmmi10',
204 | 'mu' => 'cmmi10',
205 | 'nu' => 'cmmi10',
206 | 'xi' => 'cmmi10',
207 | 'pi' => 'cmmi10',
208 | 'varpi' => 'cmmi10',
209 | 'rho' => 'cmmi10',
210 | 'varrho' => 'cmmi10',
211 | 'sigma' => 'cmmi10',
212 | 'varsigma' => 'cmmi10',
213 | 'tau' => 'cmmi10',
214 | 'upsilon' => 'cmmi10',
215 | 'phi' => 'cmmi10',
216 | 'varphi' => 'cmmi10',
217 | 'chi' => 'cmmi10',
218 | 'psi' => 'cmmi10',
219 | 'omega' => 'cmmi10',
220 | 'Gamma' => 'cmr10',
221 | 'Lambda' => 'cmr10',
222 | 'Sigma' => 'cmr10',
223 | 'Psi' => 'cmr10',
224 | 'Delta' => 'cmr10',
225 | 'Xi' => 'cmr10',
226 | 'Upsilon' => 'cmr10',
227 | 'Omega' => 'cmr10',
228 | 'Theta' => 'cmr10',
229 | 'Pi' => 'cmr10',
230 | 'Phi' => 'cmr10',
231 | 'infty' => 'FreeSerif',
232 | 'ne' => 'FreeSerif',
233 | '*' => 'FreeSerif',
234 | 'in' => 'FreeSerif',
235 | 'notin' => 'FreeSerif',
236 | 'forall' => 'FreeSerif',
237 | 'exists' => 'FreeSerif',
238 | 'notexists' => 'FreeSerif',
239 | 'partial' => 'FreeSerif',
240 | 'approx' => 'FreeSerif',
241 | 'left' => 'FreeSerif',
242 | 'right' => 'FreeSerif',
243 | 'leftright' => 'FreeSerif',
244 | 'doubleleft' => 'FreeSerif',
245 | 'doubleright' => 'FreeSerif',
246 | 'doubleleftright' => 'FreeSerif',
247 | 'nearrow' => 'FreeSerif',
248 | 'searrow' => 'FreeSerif',
249 | 'pm' => 'FreeSerif',
250 | 'bbR' => 'FreeSerif',
251 | 'bbN' => 'FreeSerif',
252 | 'bbZ' => 'FreeSerif',
253 | 'bbC' => 'FreeSerif',
254 | 'inter' => 'FreeSerif',
255 | 'union' => 'FreeSerif',
256 | 'ortho' => 'FreeSerif',
257 | 'parallel' => 'FreeSerif',
258 | 'backslash' => 'FreeSerif',
259 | 'prime' => 'FreeSerif',
260 | 'wedge' => 'FreeSerif',
261 | 'vert' => 'FreeSerif',
262 | 'subset' => 'FreeSerif',
263 | 'notsubset' => 'FreeSerif',
264 | 'circ' => 'FreeSerif',
265 | 'varnothing' => 'FreeSerif',
266 | 'cdots' => 'FreeSerif',
267 | 'vdots' => 'FreeSerif',
268 | 'ddots' => 'FreeSerif',
269 | //operateurs
270 | 'le' => 'msam10',
271 | 'ge' => 'msam10',
272 | '<' => 'cmmi10',
273 | '>' => 'cmmi10',
274 | //parentheses
275 | '(' => 'cmex10',
276 | ')' => 'cmex10',
277 | '[' => 'cmex10',
278 | ']' => 'cmex10',
279 | 'lbrace' => 'cmex10',
280 | 'rbrace' => 'cmex10',
281 | //autres
282 | '_hat' => 'cmex10',
283 | '_racine' => 'cmex10',
284 | '_integrale' => 'cmex10',
285 | '_dintegrale' => 'FreeSerif',
286 | '_tintegrale' => 'FreeSerif',
287 | '_ointegrale' => 'cmex10',
288 | '_produit' => 'cmex10',
289 | '_somme' => 'cmex10',
290 | '_intersection' => 'cmex10',
291 | '_reunion' => 'cmex10',
292 | '_lim' => 'cmr10',
293 | //fonctions
294 | 'arccos' => 'cmr10',
295 | 'ker' => 'cmr10',
296 | 'arcsin' => 'cmr10',
297 | 'lg' => 'cmr10',
298 | 'arctan' => 'cmr10',
299 | 'arg' => 'cmr10',
300 | 'cos' => 'cmr10',
301 | 'cosh' => 'cmr10',
302 | 'ln' => 'cmr10',
303 | 'cot' => 'cmr10',
304 | 'log' => 'cmr10',
305 | 'coth' => 'cmr10',
306 | 'max' => 'cmr10',
307 | 'csc' => 'cmr10',
308 | 'min' => 'cmr10',
309 | 'deg' => 'cmr10',
310 | 'det' => 'cmr10',
311 | 'sec' => 'cmr10',
312 | 'dim' => 'cmr10',
313 | 'sin' => 'cmr10',
314 | 'exp' => 'cmr10',
315 | 'sinh' => 'cmr10',
316 | 'gcd' => 'cmr10',
317 | 'sup' => 'cmr10',
318 | 'hom' => 'cmr10',
319 | 'tan' => 'cmr10',
320 | 'inf' => 'cmr10',
321 | 'tanh' => 'cmr10'
322 | );
323 |
324 | function est_nombre($str)
325 | {
326 | return preg_match("/^[0-9]/", $str);
327 | }
328 |
329 | function tableau_expression($expression)
330 | {
331 | $e = str_replace('_', ' _ ', $expression);
332 | $e = str_replace('{(}', '{ }', $e);
333 | $e = str_replace('{)}', '{ }', $e);
334 | $t = token_get_all("");
335 | $extraits = array();
336 | $result = array();
337 | //stupid code but token_get_all bug in some php versions
338 | $d = 0;
339 | for ($i = 0; $i < count($t); $i++) {
340 | if (is_array($t[$i])) {
341 | $t[$i] = $t[$i][1];
342 | }
343 | if (preg_match("/formula/", $t[$i])) {
344 | $d = $i + 2;
345 | break;
346 | }
347 | }
348 | for ($i = $d; $i < count($t) - 1; $i++) {
349 | if (is_array($t[$i])) {
350 | $t[$i] = $t[$i][1];
351 | }
352 | if ($t[$i] == '<=') {
353 | $t[$i] = 'le';
354 | } elseif ($t[$i] == '!=') {
355 | $t[$i] = 'ne';
356 | } elseif ($t[$i] == '<>') {
357 | $t[$i] = 'ne';
358 | } elseif ($t[$i] == '>=') {
359 | $t[$i] = 'ge';
360 | } elseif ($t[$i] == '--') {
361 | $t[$i] = '-';
362 | $t[$i + 1] = '-' . $t[$i + 1];
363 | } elseif ($t[$i] == '++') {
364 | $t[$i] = '+';
365 | } elseif ($t[$i] == '-') {
366 | if ($t[$i - 1] == '^' || $t[$i - 1] == '_' || $t[$i - 1] == '*' || $t[$i - 1] == '/' || $t[$i - 1] == '+' || $t[$i - 1] == '(') {
367 | $t[$i] = '';
368 | if (is_array($t[$i + 1])) {
369 | $t[$i + 1][1] = '-' . $t[$i + 1][1];
370 | } else {
371 | $t[$i + 1] = '-' . $t[$i + 1];
372 | }
373 | }
374 | }
375 | if (trim($t[$i]) != '') {
376 | $extraits[] = $t[$i];
377 | }
378 | }
379 | for ($i = 0; $i < count($extraits); $i++) {
380 | $result[] = new expression_texte($extraits[$i]);
381 | }
382 | return $result;
383 | }
384 |
385 | // ugly hack, but GD is not very good with truetype fonts (especially with latex fonts)
386 | function affiche_symbol($texte, $haut)
387 | {
388 | $dirfonts = GlobalVar::$dirfonts;
389 | $symboles = GlobalVar::$symboles;
390 | $fontesmath = GlobalVar::$fontesmath;
391 | $texte = trim(stripslashes($texte));
392 | switch ($texte) {
393 | case '':
394 | $img = ImageCreate(1, max($haut, 1));
395 | $blanc = ImageColorAllocate($img, 255, 255, 255);
396 | $blanc = imagecolortransparent($img, $blanc);
397 | ImageFilledRectangle($img, 0, 0, 1, $haut, $blanc);
398 | break;
399 | case '~':
400 | $img = ImageCreate(1, max($haut, 1));
401 | $blanc = ImageColorAllocate($img, 255, 255, 255);
402 | $blanc = imagecolortransparent($img, $blanc);
403 | ImageFilledRectangle($img, 0, 0, 1, $haut, $blanc);
404 | break;
405 | case 'vert':
406 | $img = ImageCreate(6, max($haut, 1));
407 | $blanc = ImageColorAllocate($img, 255, 255, 255);
408 | $blanc = imagecolortransparent($img, $blanc);
409 | $noir = ImageColorAllocate($img, 0, 0, 0);
410 | ImageFilledRectangle($img, 0, 0, 6, $haut, $blanc);
411 | ImageFilledRectangle($img, 2, 0, 2, $haut, $noir);
412 | ImageFilledRectangle($img, 4, 0, 4, $haut, $noir);
413 | break;
414 | case '|':
415 | $img = ImageCreate(5, max($haut, 1));
416 | $blanc = ImageColorAllocate($img, 255, 255, 255);
417 | $blanc = imagecolortransparent($img, $blanc);
418 | $noir = ImageColorAllocate($img, 0, 0, 0);
419 | ImageFilledRectangle($img, 0, 0, 5, $haut, $blanc);
420 | ImageFilledRectangle($img, 2, 0, 2, $haut, $noir);
421 | break;
422 | case 'right':
423 | $font = $dirfonts . "/" . $fontesmath[$texte] . ".ttf";
424 | $t = 16;
425 | $texte = $symboles[$texte];
426 | $tmp_dim = ImageTTFBBox($t, 0, $font, $texte);
427 | $tmp_largeur = abs($tmp_dim[2] - $tmp_dim[0]) + 2;
428 | $tmp_hauteur = abs($tmp_dim[3] - $tmp_dim[5]) + 2;
429 | $tmp_img = ImageCreate(max($tmp_largeur, 1), max($tmp_hauteur, 1));
430 | $tmp_noir = ImageColorAllocate($tmp_img, 0, 0, 0);
431 | $tmp_blanc = ImageColorAllocate($tmp_img, 255, 255, 255);
432 | $tmp_blanc = imagecolortransparent($tmp_img, $tmp_blanc);
433 | ImageFilledRectangle($tmp_img, 0, 0, $tmp_largeur, $tmp_hauteur, $tmp_blanc);
434 | ImageTTFText($tmp_img, $t, 0, 0, $tmp_hauteur, $tmp_noir, $font, $texte);
435 | $toutblanc = true;
436 | $sx = $sy = $ex = $ey = -1;
437 | for ($y = 0; $y < $tmp_hauteur; $y++) {
438 | for ($x = 0; $x < $tmp_largeur; $x++) {
439 | $rgb = ImageColorAt($tmp_img, $x, $y);
440 | if ($rgb != $tmp_blanc) {
441 | $toutblanc = false;
442 | if ($sy == -1) {
443 | $sy = $y;
444 | } else {
445 | $ey = $y;
446 | }
447 |
448 | if ($sx == -1) {
449 | $sx = $x;
450 | } else {
451 | if ($x < $sx) {
452 | $sx = $x;
453 | } else if ($x > $ex) {
454 | $ex = $x;
455 | }
456 | }
457 | }
458 | }
459 | }
460 | $nx = abs($ex - $sx);
461 | $ny = abs($ey - $sy);
462 | $img = ImageCreate(max($nx + 4, 1), max($ny + 4, 1));
463 | $blanc = ImageColorAllocate($img, 255, 255, 255);
464 | $blanc = imagecolortransparent($img, $blanc);
465 | ImageFilledRectangle($img, 0, 0, $nx + 4, $ny + 4, $blanc);
466 | ImageCopy($img, $tmp_img, 2, 2, $sx, $sy, min($nx + 2, $tmp_largeur - $sx), min($ny + 2, $tmp_hauteur - $sy));
467 | break;
468 | case '_hat':
469 | $font = $dirfonts . "/" . $fontesmath[$texte] . ".ttf";
470 | $t = $haut;
471 | $texte = $symboles[$texte];
472 | $tmp_dim = ImageTTFBBox($t, 0, $font, $texte);
473 | $tmp_largeur = abs($tmp_dim[2] - $tmp_dim[0]);
474 | $tmp_hauteur = abs($tmp_dim[3] - $tmp_dim[5]) * 4;
475 | $tmp_img = ImageCreate(max($tmp_largeur, 1), max($tmp_hauteur, 1));
476 | $tmp_noir = ImageColorAllocate($tmp_img, 0, 0, 0);
477 | $tmp_blanc = ImageColorAllocate($tmp_img, 255, 255, 255);
478 | $tmp_blanc = imagecolortransparent($tmp_img, $tmp_blanc);
479 | ImageFilledRectangle($tmp_img, 0, 0, $tmp_largeur, $tmp_hauteur, $tmp_blanc);
480 | ImageTTFText($tmp_img, $t, 0, 0, $tmp_hauteur, $tmp_noir, $font, $texte);
481 | $toutblanc = true;
482 | $img = $tmp_img;
483 | $sx = $sy = $ex = $ey = -1;
484 | for ($y = 0; $y < $tmp_hauteur; $y++) {
485 | for ($x = 0; $x < $tmp_largeur; $x++) {
486 | $rgb = ImageColorAt($tmp_img, $x, $y);
487 | if ($rgb != $tmp_blanc) {
488 | $toutblanc = false;
489 | if ($sy == -1) {
490 | $sy = $y;
491 | } else {
492 | $ey = $y;
493 | }
494 |
495 | if ($sx == -1) {
496 | $sx = $x;
497 | } else {
498 | if ($x < $sx) {
499 | $sx = $x;
500 | } else if ($x > $ex) {
501 | $ex = $x;
502 | }
503 | }
504 | }
505 | }
506 | }
507 | $nx = abs($ex - $sx);
508 | $ny = abs($ey - $sy);
509 | $img = ImageCreate(max($nx + 4, 1), max($ny + 4, 1));
510 | $blanc = ImageColorAllocate($img, 255, 255, 255);
511 | $blanc = imagecolortransparent($img, $blanc);
512 | ImageFilledRectangle($img, 0, 0, $nx + 4, $ny + 4, $blanc);
513 | ImageCopy($img, $tmp_img, 2, 2, $sx, $sy, min($nx + 2, $tmp_largeur - $sx), min($ny + 2, $tmp_hauteur - $sy));
514 | break;
515 | case '_dintegrale':
516 | case '_tintegrale':
517 | if (isset($fontesmath[$texte])) {
518 | $font = $dirfonts . "/" . $fontesmath[$texte] . ".ttf";
519 | } elseif (est_nombre($texte)) {
520 | $font = $dirfonts . "/cmr10.ttf";
521 | } else {
522 | $font = $dirfonts . "/cmmi10.ttf";
523 | }
524 | $t = 6;
525 | if (isset($symboles[$texte])) {
526 | $texte = $symboles[$texte];
527 | }
528 | do {
529 | $tmp_dim = ImageTTFBBox($t, 0, $font, $texte);
530 | $t+=1;
531 | } while ((abs($tmp_dim[3] - $tmp_dim[5]) < 1.2 * $haut));
532 | $tmp_largeur = abs($tmp_dim[2] - $tmp_dim[0]) * 2;
533 | $tmp_hauteur = abs($tmp_dim[3] - $tmp_dim[5]) * 2;
534 | $tmp_img = ImageCreate(max($tmp_largeur, 1), max($tmp_hauteur, 1));
535 | $tmp_noir = ImageColorAllocate($tmp_img, 0, 0, 0);
536 | $tmp_blanc = ImageColorAllocate($tmp_img, 255, 255, 255);
537 | $tmp_blanc = imagecolortransparent($tmp_img, $tmp_blanc);
538 | ImageFilledRectangle($tmp_img, 0, 0, $tmp_largeur, $tmp_hauteur, $tmp_blanc);
539 | ImageTTFText($tmp_img, $t, 0, 5, $tmp_hauteur / 2, $tmp_noir, $font, $texte);
540 | $img = $tmp_img;
541 | $toutblanc = true;
542 | $sx = $sy = $ex = $ey = -1;
543 | for ($y = 0; $y < $tmp_hauteur; $y++) {
544 | for ($x = 0; $x < $tmp_largeur; $x++) {
545 | $rgb = ImageColorAt($tmp_img, $x, $y);
546 | if ($rgb != $tmp_blanc) {
547 | $toutblanc = false;
548 | if ($sy == -1) {
549 | $sy = $y;
550 | } else {
551 | $ey = $y;
552 | }
553 |
554 | if ($sx == -1) {
555 | $sx = $x;
556 | } else {
557 | if ($x < $sx) {
558 | $sx = $x;
559 | } else if ($x > $ex) {
560 | $ex = $x;
561 | }
562 | }
563 | }
564 | }
565 | }
566 | $nx = abs($ex - $sx);
567 | $ny = abs($ey - $sy);
568 | if ($toutblanc) {
569 | $img = ImageCreate(1, max($haut, 1));
570 | $blanc = ImageColorAllocate($img, 255, 255, 255);
571 | $blanc = imagecolortransparent($img, $blanc);
572 | ImageFilledRectangle($img, 0, 0, 1, $haut, $blanc);
573 | } else {
574 | $img = ImageCreate(max($nx + 4, 1), max($ny + 4, 1));
575 | $blanc = ImageColorAllocate($img, 255, 255, 255);
576 | $blanc = imagecolortransparent($img, $blanc);
577 | ImageFilledRectangle($img, 0, 0, $nx + 4, $ny + 4, $blanc);
578 | ImageCopy($img, $tmp_img, 2, 2, $sx, $sy, min($nx + 2, $tmp_largeur - $sx), min($ny + 2, $tmp_hauteur - $sy));
579 | }
580 | break;
581 | default:
582 | if (isset($fontesmath[$texte])) {
583 | $font = $dirfonts . "/" . $fontesmath[$texte] . ".ttf";
584 | } elseif (est_nombre($texte)) {
585 | $font = $dirfonts . "/cmr10.ttf";
586 | } else {
587 | $font = $dirfonts . "/cmmi10.ttf";
588 | }
589 | $t = 6;
590 | if (isset($symboles[$texte])) {
591 | $texte = $symboles[$texte];
592 | }
593 | do {
594 | $tmp_dim = ImageTTFBBox($t, 0, $font, $texte);
595 | $t+=1;
596 | } while ((abs($tmp_dim[3] - $tmp_dim[5]) < $haut));
597 | $tmp_largeur = abs($tmp_dim[2] - $tmp_dim[0]) * 2;
598 | $tmp_hauteur = abs($tmp_dim[3] - $tmp_dim[5]) * 2;
599 | $tmp_img = ImageCreate(max($tmp_largeur, 1), max($tmp_hauteur, 1));
600 | $tmp_noir = ImageColorAllocate($tmp_img, 0, 0, 0);
601 | $tmp_blanc = ImageColorAllocate($tmp_img, 255, 255, 255);
602 | $tmp_blanc = imagecolortransparent($tmp_img, $tmp_blanc);
603 | ImageFilledRectangle($tmp_img, 0, 0, $tmp_largeur, $tmp_hauteur, $tmp_blanc);
604 | ImageTTFText($tmp_img, $t, 0, 0, $tmp_hauteur / 4, $tmp_noir, $font, $texte);
605 | // ImageTTFText($tmp_img, $t, 0,5,5,$tmp_noir, $font,$texte);
606 | // $img=$tmp_img;
607 | $toutblanc = true;
608 | $sx = $sy = $ex = $ey = -1;
609 | for ($y = 0; $y < $tmp_hauteur; $y++) {
610 | for ($x = 0; $x < $tmp_largeur; $x++) {
611 | $rgb = ImageColorAt($tmp_img, $x, $y);
612 | if ($rgb != $tmp_blanc) {
613 | $toutblanc = false;
614 | if ($sy == -1) {
615 | $sy = $y;
616 | } else {
617 | $ey = $y;
618 | }
619 |
620 | if ($sx == -1) {
621 | $sx = $x;
622 | } else {
623 | if ($x < $sx) {
624 | $sx = $x;
625 | } else if ($x > $ex) {
626 | $ex = $x;
627 | }
628 | }
629 | }
630 | }
631 | }
632 | $nx = abs($ex - $sx);
633 | $ny = abs($ey - $sy);
634 | if ($toutblanc) {
635 | $img = ImageCreate(1, max($haut, 1));
636 | $blanc = ImageColorAllocate($img, 255, 255, 255);
637 | $blanc = imagecolortransparent($img, $blanc);
638 | ImageFilledRectangle($img, 0, 0, 1, $haut, $blanc);
639 | } else {
640 | $img = ImageCreate(max($nx + 4, 1), max($ny + 4, 1));
641 | $blanc = ImageColorAllocate($img, 255, 255, 255);
642 | $blanc = imagecolortransparent($img, $blanc);
643 | ImageFilledRectangle($img, 0, 0, $nx + 4, $ny + 4, $blanc);
644 | ImageCopy($img, $tmp_img, 2, 2, $sx, $sy, min($nx + 2, $tmp_largeur - $sx), min($ny + 2, $tmp_hauteur - $sy));
645 | }
646 | break;
647 | }
648 | //$rouge=ImageColorAllocate($img,255,0,0);
649 | //ImageRectangle($img,0,0,ImageSX($img)-1,ImageSY($img)-1,$rouge);
650 | return $img;
651 | }
652 |
653 | function affiche_texte($texte, $taille)
654 | {
655 | $dirfonts = GlobalVar::$dirfonts;
656 | $taille = max($taille, 6);
657 | $texte = stripslashes($texte);
658 | $font = $dirfonts . "/cmr10.ttf";
659 | $htexte = 'dg' . $texte;
660 | $hdim = ImageTTFBBox($taille, 0, $font, $htexte);
661 | $wdim = ImageTTFBBox($taille, 0, $font, $texte);
662 | $dx = max($wdim[2], $wdim[4]) - min($wdim[0], $wdim[6]) + ceil($taille / 8);
663 | $dy = max($hdim[1], $hdim[3]) - min($hdim[5], $hdim[7]) + ceil($taille / 8);
664 | $img = ImageCreate(max($dx, 1), max($dy, 1));
665 | $noir = ImageColorAllocate($img, 0, 0, 0);
666 | $blanc = ImageColorAllocate($img, 255, 255, 255);
667 | $blanc = imagecolortransparent($img, $blanc);
668 | ImageFilledRectangle($img, 0, 0, $dx, $dy, $blanc);
669 | //ImageRectangle($img,0,0,$dx-1,$dy-1,$noir);
670 | ImageTTFText($img, $taille, $angle, 0, -min($hdim[5], $hdim[7]), $noir, $font, $texte);
671 | return $img;
672 | }
673 |
674 | function affiche_math($texte, $taille)
675 | {
676 | $dirfonts = GlobalVar::$dirfonts;
677 | $taille = max($taille, 6);
678 |
679 | $symboles = GlobalVar::$symboles;
680 | $fontesmath = GlobalVar::$fontesmath;
681 | $texte = stripslashes($texte);
682 | if (isset($fontesmath[$texte])) {
683 | $font = $dirfonts . "/" . $fontesmath[$texte] . ".ttf";
684 | } elseif (preg_match("/[a-zA-Z]/", $texte)) {
685 | $font = $dirfonts . "/cmmi10.ttf";
686 | } else {
687 | $font = $dirfonts . "/cmr10.ttf";
688 | }
689 | if (isset($symboles[$texte])) {
690 | $texte = $symboles[$texte];
691 | }
692 | $htexte = 'dg' . $texte;
693 | $hdim = ImageTTFBBox($taille, 0, $font, $htexte);
694 | $wdim = ImageTTFBBox($taille, 0, $font, $texte);
695 | $dx = max($wdim[2], $wdim[4]) - min($wdim[0], $wdim[6]) + ceil($taille / 8);
696 | $dy = max($hdim[1], $hdim[3]) - min($hdim[5], $hdim[7]) + ceil($taille / 8);
697 | $img = ImageCreate(max($dx, 1), max($dy, 1));
698 | $noir = ImageColorAllocate($img, 0, 0, 0);
699 | $blanc = ImageColorAllocate($img, 255, 255, 255);
700 | $blanc = imagecolortransparent($img, $blanc);
701 | ImageFilledRectangle($img, 0, 0, $dx, $dy, $blanc);
702 | //ImageRectangle($img,0,0,$dx-1,$dy-1,$noir);
703 | ImageTTFText($img, $taille, 0, 0, -min($hdim[5], $hdim[7]), $noir, $font, $texte);
704 | return $img;
705 | }
706 |
707 | function parenthese($hauteur, $style)
708 | {
709 | $image = affiche_symbol($style, $hauteur);
710 | return $image;
711 | }
712 |
713 | function alignement2($image1, $base1, $image2, $base2)
714 | {
715 | $largeur1 = imagesx($image1);
716 | $hauteur1 = imagesy($image1);
717 | $largeur2 = imagesx($image2);
718 | $hauteur2 = imagesy($image2);
719 | $dessus = max($base1, $base2);
720 | $dessous = max($hauteur1 - $base1, $hauteur2 - $base2);
721 | $largeur = $largeur1 + $largeur2;
722 | $hauteur = $dessus + $dessous;
723 | $result = ImageCreate(max($largeur, 1), max($hauteur, 1));
724 | $noir = ImageColorAllocate($result, 0, 0, 0);
725 | $blanc = ImageColorAllocate($result, 255, 255, 255);
726 | $blanc = imagecolortransparent($result, $blanc);
727 | ImageFilledRectangle($result, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
728 | ImageCopy($result, $image1, 0, $dessus - $base1, 0, 0, $largeur1, $hauteur1);
729 | ImageCopy($result, $image2, $largeur1, $dessus - $base2, 0, 0, $largeur2, $hauteur2);
730 | //ImageRectangle($result,0,0,$largeur-1,$hauteur-1,$noir);
731 | return $result;
732 | }
733 |
734 | function alignement3($image1, $base1, $image2, $base2, $image3, $base3)
735 | {
736 | $largeur1 = imagesx($image1);
737 | $hauteur1 = imagesy($image1);
738 | $largeur2 = imagesx($image2);
739 | $hauteur2 = imagesy($image2);
740 | $largeur3 = imagesx($image3);
741 | $hauteur3 = imagesy($image3);
742 | $dessus = max($base1, $base2, $base3);
743 | $dessous = max($hauteur1 - $base1, $hauteur2 - $base2, $hauteur3 - $base3);
744 | $largeur = $largeur1 + $largeur2 + $largeur3;
745 | $hauteur = $dessus + $dessous;
746 | $result = ImageCreate(max($largeur, 1), max($hauteur, 1));
747 | $noir = ImageColorAllocate($result, 0, 0, 0);
748 | $blanc = ImageColorAllocate($result, 255, 255, 255);
749 | $blanc = imagecolortransparent($result, $blanc);
750 | ImageFilledRectangle($result, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
751 | ImageCopy($result, $image1, 0, $dessus - $base1, 0, 0, $largeur1, $hauteur1);
752 | ImageCopy($result, $image2, $largeur1, $dessus - $base2, 0, 0, $largeur2, $hauteur2);
753 | ImageCopy($result, $image3, $largeur1 + $largeur2, $dessus - $base3, 0, 0, $largeur3, $hauteur3);
754 | //ImageRectangle($result,0,0,$largeur-1,$hauteur-1,$noir);
755 | return $result;
756 | }
757 |
758 | //*****************************************************************
759 | class expression
760 | {
761 | var $texte;
762 | var $image;
763 | var $base_verticale;
764 |
765 | }
766 |
767 | //*****************************************************************
768 | class expression_texte extends expression
769 | {
770 |
771 | function __construct($exp)
772 | {
773 | $this->texte = $exp;
774 | }
775 |
776 | function dessine($taille)
777 | {
778 | $this->image = affiche_math($this->texte, $taille);
779 | $this->base_verticale = imagesy($this->image) / 2;
780 | }
781 | }
782 |
783 | //*****************************************************************
784 | class expression_math extends expression
785 | {
786 | var $noeuds;
787 |
788 | function __construct($exp)
789 | {
790 | $this->texte = "&$";
791 | $this->noeuds = $exp;
792 | $this->noeuds = $this->parse();
793 | }
794 |
795 | function parse()
796 | {
797 | if (count($this->noeuds) <= 3) {
798 | return $this->noeuds;
799 | }
800 | $ret = array();
801 | $parentheses = array();
802 | for ($i = 0; $i < count($this->noeuds); $i++) {
803 | if ($this->noeuds[$i]->texte == '(' || $this->noeuds[$i]->texte == '{') {
804 | array_push($parentheses, $i);
805 | } elseif ($this->noeuds[$i]->texte == ')' || $this->noeuds[$i]->texte == '}') {
806 | $pos = array_pop($parentheses);
807 | if (count($parentheses) == 0) {
808 | $sub = array_slice($this->noeuds, $pos + 1, $i - $pos - 1);
809 | if ($this->noeuds[$i]->texte == ')') {
810 | $ret[] = new expression_math(array(new expression_texte("("), new expression_math($sub), new expression_texte(")")));
811 | } else {
812 | $ret[] = new expression_math($sub);
813 | }
814 | }
815 | } elseif (count($parentheses) == 0) {
816 | $ret[] = $this->noeuds[$i];
817 | }
818 | }
819 | $ret = $this->traite_fonction($ret, 'sqrt', 1);
820 | $ret = $this->traite_fonction($ret, 'vec', 1);
821 | $ret = $this->traite_fonction($ret, 'overline', 1);
822 | $ret = $this->traite_fonction($ret, 'underline', 1);
823 | $ret = $this->traite_fonction($ret, 'hat', 1);
824 | $ret = $this->traite_fonction($ret, 'int', 3);
825 | $ret = $this->traite_fonction($ret, 'doubleint', 3);
826 | $ret = $this->traite_fonction($ret, 'tripleint', 3);
827 | $ret = $this->traite_fonction($ret, 'oint', 3);
828 | $ret = $this->traite_fonction($ret, 'prod', 3);
829 | $ret = $this->traite_fonction($ret, 'sum', 3);
830 | $ret = $this->traite_fonction($ret, 'bigcup', 3);
831 | $ret = $this->traite_fonction($ret, 'bigcap', 3);
832 | $ret = $this->traite_fonction($ret, 'delim', 3);
833 | $ret = $this->traite_fonction($ret, 'lim', 2);
834 | $ret = $this->traite_fonction($ret, 'root', 2);
835 | $ret = $this->traite_fonction($ret, 'matrix', 3);
836 | $ret = $this->traite_fonction($ret, 'tabular', 3);
837 |
838 | $ret = $this->traite_operation($ret, '^');
839 | $ret = $this->traite_operation($ret, 'over');
840 | $ret = $this->traite_operation($ret, '_');
841 | $ret = $this->traite_operation($ret, 'under');
842 | $ret = $this->traite_operation($ret, '*');
843 | $ret = $this->traite_operation($ret, '/');
844 | $ret = $this->traite_operation($ret, '+');
845 | $ret = $this->traite_operation($ret, '-');
846 | return $ret;
847 | }
848 |
849 | function traite_operation($noeuds, $operation)
850 | {
851 | do {
852 | $change = false;
853 | if (count($noeuds) <= 3) {
854 | return $noeuds;
855 | }
856 | $ret = array();
857 | for ($i = 0; $i < count($noeuds); $i++) {
858 | if (!$change && $i < count($noeuds) - 2 && $noeuds[$i + 1]->texte == $operation) {
859 | $ret[] = new expression_math(array($noeuds[$i], $noeuds[$i + 1], $noeuds[$i + 2]));
860 | $i += 2;
861 | $change = true;
862 | } else {
863 | $ret[] = $noeuds[$i];
864 | }
865 | }
866 | $noeuds = $ret;
867 | } while ($change);
868 | return $ret;
869 | }
870 |
871 | function traite_fonction($noeuds, $fonction, $nbarg)
872 | {
873 | if (count($noeuds) <= $nbarg + 1) {
874 | return $noeuds;
875 | }
876 | $ret = array();
877 | for ($i = 0; $i < count($noeuds); $i++) {
878 | if ($i < count($noeuds) - $nbarg && $noeuds[$i]->texte == $fonction) {
879 | $a = array();
880 | for ($j = $i; $j <= $i + $nbarg; $j++) {
881 | $a[] = $noeuds[$j];
882 | }
883 | $ret[] = new expression_math($a);
884 | $i += $nbarg;
885 | } else {
886 | $ret[] = $noeuds[$i];
887 | }
888 | }
889 | return $ret;
890 | }
891 |
892 | function dessine($taille)
893 | {
894 | switch (count($this->noeuds)) {
895 | case 1:
896 | $this->noeuds[0]->dessine($taille);
897 | $this->image = $this->noeuds[0]->image;
898 | $this->base_verticale = $this->noeuds[0]->base_verticale;
899 | break;
900 | case 2:
901 | switch ($this->noeuds[0]->texte) {
902 | case 'sqrt':
903 | $this->dessine_racine($taille);
904 | break;
905 | case 'vec':
906 | $this->dessine_vecteur($taille);
907 | break;
908 | case 'overline':
909 | $this->dessine_overline($taille);
910 | break;
911 | case 'underline':
912 | $this->dessine_underline($taille);
913 | break;
914 | case 'hat':
915 | $this->dessine_chapeau($taille);
916 | break;
917 | default:
918 | $this->dessine_expression($taille);
919 | break;
920 | }
921 | break;
922 | case 3:
923 | if ($this->noeuds[0]->texte == "lim") {
924 | $this->dessine_limite($taille);
925 | } elseif ($this->noeuds[0]->texte == "root") {
926 | $this->dessine_root($taille);
927 | } else {
928 | switch ($this->noeuds[1]->texte) {
929 | case '/':
930 | $this->dessine_fraction($taille);
931 | break;
932 | case '^':
933 | $this->dessine_exposant($taille);
934 | break;
935 | case 'over':
936 | $this->dessine_dessus($taille);
937 | break;
938 | case '_':
939 | $this->dessine_indice($taille);
940 | break;
941 | case 'under':
942 | $this->dessine_dessous($taille);
943 | break;
944 | default:
945 | $this->dessine_expression($taille);
946 | break;
947 | }
948 | }
949 | break;
950 | case 4:
951 | switch ($this->noeuds[0]->texte) {
952 | case 'int':
953 | $this->dessine_grandoperateur($taille, '_integrale');
954 | break;
955 | case 'doubleint':
956 | $this->dessine_grandoperateur($taille, '_dintegrale');
957 | break;
958 | case 'tripleint':
959 | $this->dessine_grandoperateur($taille, '_tintegrale');
960 | break;
961 | case 'oint':
962 | $this->dessine_grandoperateur($taille, '_ointegrale');
963 | break;
964 | case 'sum':
965 | $this->dessine_grandoperateur($taille, '_somme');
966 | break;
967 | case 'prod':
968 | $this->dessine_grandoperateur($taille, '_produit');
969 | break;
970 | case 'bigcap':
971 | $this->dessine_grandoperateur($taille, '_intersection');
972 | break;
973 | case 'bigcup':
974 | $this->dessine_grandoperateur($taille, '_reunion');
975 | break;
976 | case 'delim':
977 | $this->dessine_delimiteur($taille);
978 | break;
979 | case 'matrix':
980 | $this->dessine_matrice($taille);
981 | break;
982 | case 'tabular':
983 | $this->dessine_tableau($taille);
984 | break;
985 | default:
986 | $this->dessine_expression($taille);
987 | break;
988 | }
989 | break;
990 | default:
991 | $this->dessine_expression($taille);
992 | break;
993 | }
994 | }
995 |
996 | function dessine_expression($taille)
997 | {
998 | $largeur = 1;
999 | $hauteur = 1;
1000 | $dessus = 1;
1001 | $dessous = 1;
1002 | for ($i = 0; $i < count($this->noeuds); $i++) {
1003 | if ($this->noeuds[$i]->texte != '(' && $this->noeuds[$i]->texte != ')') {
1004 | $this->noeuds[$i]->dessine($taille);
1005 | $img[$i] = $this->noeuds[$i]->image;
1006 | $base[$i] = $this->noeuds[$i]->base_verticale;
1007 | $dessus = max($base[$i], $dessus);
1008 | $dessous = max(imagesy($img[$i]) - $base[$i], $dessous);
1009 | }
1010 | }
1011 | $hauteur = $dessus + $dessous;
1012 | $paro = parenthese(max($dessus, $dessous) * 2, "(");
1013 | $parf = parenthese(max($dessus, $dessous) * 2, ")");
1014 | for ($i = 0; $i < count($this->noeuds); $i++) {
1015 | if (!isset($img[$i])) {
1016 | if ($this->noeuds[$i]->texte == "(") {
1017 | $img[$i] = $paro;
1018 | } else {
1019 | $img[$i] = $parf;
1020 | }
1021 | $dessus = max(imagesy($img[$i]) / 2, $dessus);
1022 | $base[$i] = imagesy($img[$i]) / 2;
1023 | $dessous = max(imagesy($img[$i]) - $base[$i], $dessous);
1024 | $hauteur = max(imagesy($img[$i]), $hauteur);
1025 | }
1026 | $largeur+=imagesx($img[$i]);
1027 | }
1028 | $this->base_verticale = $dessus;
1029 | $result = ImageCreate(max($largeur, 1), max($hauteur, 1));
1030 | $noir = ImageColorAllocate($result, 0, 0, 0);
1031 | $blanc = ImageColorAllocate($result, 255, 255, 255);
1032 | $blanc = imagecolortransparent($result, $blanc);
1033 | ImageFilledRectangle($result, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1034 | $pos = 0;
1035 | for ($i = 0; $i < count($img); $i++) {
1036 | if (isset($img[$i])) {
1037 | ImageCopy($result, $img[$i], $pos, $dessus - $base[$i], 0, 0, imagesx($img[$i]), imagesy($img[$i]));
1038 | $pos += imagesx($img[$i]);
1039 | }
1040 | }
1041 | $this->image = $result;
1042 | }
1043 |
1044 | function dessine_fraction($taille)
1045 | {
1046 | $this->noeuds[0]->dessine($taille * 0.9);
1047 | $img1 = $this->noeuds[0]->image;
1048 | $base1 = $this->noeuds[0]->base_verticale;
1049 | $this->noeuds[2]->dessine($taille * 0.9);
1050 | $img2 = $this->noeuds[2]->image;
1051 | $base2 = $this->noeuds[2]->base_verticale;
1052 | $hauteur1 = imagesy($img1);
1053 | $hauteur2 = imagesy($img2);
1054 | $largeur1 = imagesx($img1);
1055 | $largeur2 = imagesx($img2);
1056 | $largeur = max($largeur1, $largeur2);
1057 | $hauteur = $hauteur1 + $hauteur2 + 4;
1058 | $result = ImageCreate(max($largeur + 5, 1), max($hauteur, 1));
1059 | $noir = ImageColorAllocate($result, 0, 0, 0);
1060 | $blanc = ImageColorAllocate($result, 255, 255, 255);
1061 | $blanc = imagecolortransparent($result, $blanc);
1062 | $this->base_verticale = $hauteur1 + 2;
1063 | ImageFilledRectangle($result, 0, 0, $largeur + 4, $hauteur - 1, $blanc);
1064 | ImageCopy($result, $img1, ($largeur - $largeur1) / 2, 0, 0, 0, $largeur1, $hauteur1);
1065 | imageline($result, 0, $this->base_verticale, $largeur, $this->base_verticale, $noir);
1066 | ImageCopy($result, $img2, ($largeur - $largeur2) / 2, $hauteur1 + 4, 0, 0, $largeur2, $hauteur2);
1067 | $this->image = $result;
1068 | }
1069 |
1070 | function dessine_exposant($taille)
1071 | {
1072 | $this->noeuds[0]->dessine($taille);
1073 | $img1 = $this->noeuds[0]->image;
1074 | $base1 = $this->noeuds[0]->base_verticale;
1075 | $this->noeuds[2]->dessine($taille * 0.8);
1076 | $img2 = $this->noeuds[2]->image;
1077 | $base2 = $this->noeuds[2]->base_verticale;
1078 | $hauteur1 = imagesy($img1);
1079 | $hauteur2 = imagesy($img2);
1080 | $largeur1 = imagesx($img1);
1081 | $largeur2 = imagesx($img2);
1082 | $largeur = $largeur1 + $largeur2;
1083 | if ($hauteur1 >= $hauteur2) {
1084 | $hauteur = ceil($hauteur2 / 2 + $hauteur1);
1085 | $this->base_verticale = $hauteur2 / 2 + $base1;
1086 | $result = ImageCreate(max($largeur, 1), max($hauteur, 1));
1087 | $noir = ImageColorAllocate($result, 0, 0, 0);
1088 | $blanc = ImageColorAllocate($result, 255, 255, 255);
1089 | $blanc = imagecolortransparent($result, $blanc);
1090 | ImageFilledRectangle($result, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1091 | ImageCopy($result, $img1, 0, ceil($hauteur2 / 2), 0, 0, $largeur1, $hauteur1);
1092 | ImageCopy($result, $img2, $largeur1, 0, 0, 0, $largeur2, $hauteur2);
1093 | } else {
1094 | $hauteur = ceil($hauteur1 / 2 + $hauteur2);
1095 | $this->base_verticale = $hauteur2 - $base1 + $hauteur1 / 2;
1096 | $result = ImageCreate(max($largeur, 1), max($hauteur, 1));
1097 | $noir = ImageColorAllocate($result, 0, 0, 0);
1098 | $blanc = ImageColorAllocate($result, 255, 255, 255);
1099 | $blanc = imagecolortransparent($result, $blanc);
1100 | ImageFilledRectangle($result, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1101 | ImageCopy($result, $img1, 0, ceil($hauteur2 - $hauteur1 / 2), 0, 0, $largeur1, $hauteur1);
1102 | ImageCopy($result, $img2, $largeur1, 0, 0, 0, $largeur2, $hauteur2);
1103 | }
1104 | $this->image = $result;
1105 | }
1106 |
1107 | function dessine_indice($taille)
1108 | {
1109 | $this->noeuds[0]->dessine($taille);
1110 | $img1 = $this->noeuds[0]->image;
1111 | $base1 = $this->noeuds[0]->base_verticale;
1112 | $this->noeuds[2]->dessine($taille * 0.8);
1113 | $img2 = $this->noeuds[2]->image;
1114 | $base2 = $this->noeuds[2]->base_verticale;
1115 | $hauteur1 = imagesy($img1);
1116 | $hauteur2 = imagesy($img2);
1117 | $largeur1 = imagesx($img1);
1118 | $largeur2 = imagesx($img2);
1119 | $largeur = $largeur1 + $largeur2;
1120 | if ($hauteur1 >= $hauteur2) {
1121 | $hauteur = ceil($hauteur2 / 2 + $hauteur1);
1122 | $this->base_verticale = $base1;
1123 | $result = ImageCreate(max($largeur, 1), max($hauteur, 1));
1124 | $noir = ImageColorAllocate($result, 0, 0, 0);
1125 | $blanc = ImageColorAllocate($result, 255, 255, 255);
1126 | $blanc = imagecolortransparent($result, $blanc);
1127 | ImageFilledRectangle($result, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1128 | ImageCopy($result, $img1, 0, 0, 0, 0, $largeur1, $hauteur1);
1129 | ImageCopy($result, $img2, $largeur1, ceil($hauteur1 - $hauteur2 / 2), 0, 0, $largeur2, $hauteur2);
1130 | } else {
1131 | $hauteur = ceil($hauteur1 / 2 + $hauteur2);
1132 | $this->base_verticale = $base1;
1133 | $result = ImageCreate(max($largeur, 1), max($hauteur, 1));
1134 | $noir = ImageColorAllocate($result, 0, 0, 0);
1135 | $blanc = ImageColorAllocate($result, 255, 255, 255);
1136 | $blanc = imagecolortransparent($result, $blanc);
1137 | ImageFilledRectangle($result, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1138 | ImageCopy($result, $img1, 0, 0, 0, 0, $largeur1, $hauteur1);
1139 | ImageCopy($result, $img2, $largeur1, ceil($hauteur1 / 2), 0, 0, $largeur2, $hauteur2);
1140 | }
1141 | $this->image = $result;
1142 | }
1143 |
1144 | function dessine_racine($taille)
1145 | {
1146 | $this->noeuds[1]->dessine($taille);
1147 | $imgexp = $this->noeuds[1]->image;
1148 | $baseexp = $this->noeuds[1]->base_verticale;
1149 | $largeurexp = imagesx($imgexp);
1150 | $hauteurexp = imagesy($imgexp);
1151 |
1152 | $imgrac = affiche_symbol("_racine", $hauteurexp + 2);
1153 | $largeurrac = imagesx($imgrac);
1154 | $hauteurrac = imagesy($imgrac);
1155 | $baserac = $hauteurrac / 2;
1156 |
1157 | $largeur = $largeurrac + $largeurexp;
1158 | $hauteur = max($hauteurexp, $hauteurrac);
1159 | $result = ImageCreate(max($largeur, 1), max($hauteur, 1));
1160 | $noir = ImageColorAllocate($result, 0, 0, 0);
1161 | $blanc = ImageColorAllocate($result, 255, 255, 255);
1162 | $blanc = imagecolortransparent($result, $blanc);
1163 | ImageFilledRectangle($result, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1164 | ImageCopy($result, $imgrac, 0, 0, 0, 0, $largeurrac, $hauteurrac);
1165 | ImageCopy($result, $imgexp, $largeurrac, $hauteur - $hauteurexp, 0, 0, $largeurexp, $hauteurexp);
1166 | imagesetthickness($result, 1);
1167 | imageline($result, $largeurrac - 2, 2, $largeurrac + $largeurexp + 2, 2, $noir);
1168 | $this->base_verticale = $hauteur - $hauteurexp + $baseexp;
1169 | $this->image = $result;
1170 | }
1171 |
1172 | function dessine_root($taille)
1173 | {
1174 | $this->noeuds[1]->dessine($taille * 0.6);
1175 | $imgroot = $this->noeuds[1]->image;
1176 | $baseroot = $this->noeuds[1]->base_verticale;
1177 | $largeurroot = imagesx($imgroot);
1178 | $hauteurroot = imagesy($imgroot);
1179 |
1180 | $this->noeuds[2]->dessine($taille);
1181 | $imgexp = $this->noeuds[2]->image;
1182 | $baseexp = $this->noeuds[2]->base_verticale;
1183 | $largeurexp = imagesx($imgexp);
1184 | $hauteurexp = imagesy($imgexp);
1185 |
1186 | $imgrac = affiche_symbol("_racine", $hauteurexp + 2);
1187 | $largeurrac = imagesx($imgrac);
1188 | $hauteurrac = imagesy($imgrac);
1189 | $baserac = $hauteurrac / 2;
1190 |
1191 | $largeur = $largeurrac + $largeurexp;
1192 | $hauteur = max($hauteurexp, $hauteurrac);
1193 | $result = ImageCreate(max($largeur, 1), max($hauteur, 1));
1194 | $noir = ImageColorAllocate($result, 0, 0, 0);
1195 | $blanc = ImageColorAllocate($result, 255, 255, 255);
1196 | $blanc = imagecolortransparent($result, $blanc);
1197 | ImageFilledRectangle($result, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1198 | ImageCopy($result, $imgrac, 0, 0, 0, 0, $largeurrac, $hauteurrac);
1199 | ImageCopy($result, $imgexp, $largeurrac, $hauteur - $hauteurexp, 0, 0, $largeurexp, $hauteurexp);
1200 | imagesetthickness($result, 1);
1201 | imageline($result, $largeurrac - 2, 2, $largeurrac + $largeurexp + 2, 2, $noir);
1202 | ImageCopy($result, $imgroot, 0, 0, 0, 0, $largeurroot, $hauteurroot);
1203 | $this->base_verticale = $hauteur - $hauteurexp + $baseexp;
1204 | $this->image = $result;
1205 | }
1206 |
1207 | function dessine_grandoperateur($taille, $caractere)
1208 | {
1209 | $this->noeuds[1]->dessine($taille * 0.8);
1210 | $img1 = $this->noeuds[1]->image;
1211 | $base1 = $this->noeuds[1]->base_verticale;
1212 | $this->noeuds[2]->dessine($taille * 0.8);
1213 | $img2 = $this->noeuds[2]->image;
1214 | $base2 = $this->noeuds[2]->base_verticale;
1215 | $this->noeuds[3]->dessine($taille);
1216 | $imgexp = $this->noeuds[3]->image;
1217 | $baseexp = $this->noeuds[3]->base_verticale;
1218 | //borneinf
1219 | $largeur1 = imagesx($img1);
1220 | $hauteur1 = imagesy($img1);
1221 | //bornesup
1222 | $largeur2 = imagesx($img2);
1223 | $hauteur2 = imagesy($img2);
1224 | //expression
1225 | $hauteurexp = imagesy($imgexp);
1226 | $largeurexp = imagesx($imgexp);
1227 | //caractere
1228 | $imgsymbole = affiche_symbol($caractere, $baseexp * 1.8); //max($baseexp,$hauteurexp-$baseexp)*2);
1229 | $largeursymbole = imagesx($imgsymbole);
1230 | $hauteursymbole = imagesy($imgsymbole);
1231 | $basesymbole = $hauteursymbole / 2;
1232 |
1233 | $hauteurgauche = $hauteursymbole + $hauteur1 + $hauteur2;
1234 | $largeurgauche = max($largeursymbole, $largeur1, $largeur2);
1235 | $imggauche = ImageCreate(max($largeurgauche, 1), max($hauteurgauche, 1));
1236 | $noir = ImageColorAllocate($imggauche, 0, 0, 0);
1237 | $blanc = ImageColorAllocate($imggauche, 255, 255, 255);
1238 | $blanc = imagecolortransparent($imggauche, $blanc);
1239 | ImageFilledRectangle($imggauche, 0, 0, $largeurgauche - 1, $hauteurgauche - 1, $blanc);
1240 | ImageCopy($imggauche, $imgsymbole, ($largeurgauche - $largeursymbole) / 2, $hauteur2, 0, 0, $largeursymbole, $hauteursymbole);
1241 | ImageCopy($imggauche, $img2, ($largeurgauche - $largeur2) / 2, 0, 0, 0, $largeur2, $hauteur2);
1242 | ImageCopy($imggauche, $img1, ($largeurgauche - $largeur1) / 2, $hauteur2 + $hauteursymbole, 0, 0, $largeur1, $hauteur1);
1243 | $imgfin = alignement2($imggauche, $basesymbole + $hauteur2, $imgexp, $baseexp);
1244 | $this->image = $imgfin;
1245 | $this->base_verticale = max($basesymbole + $hauteur2, $baseexp + $hauteur2);
1246 | }
1247 |
1248 | function dessine_dessus($taille)
1249 | {
1250 | $this->noeuds[2]->dessine($taille * 0.8);
1251 | $imgsup = $this->noeuds[2]->image;
1252 | $basesup = $this->noeuds[2]->base_verticale;
1253 | $this->noeuds[0]->dessine($taille);
1254 | $imgexp = $this->noeuds[0]->image;
1255 | $baseexp = $this->noeuds[0]->base_verticale;
1256 | //expression
1257 | $largeurexp = imagesx($imgexp);
1258 | $hauteurexp = imagesy($imgexp);
1259 | //bornesup
1260 | $largeursup = imagesx($imgsup);
1261 | $hauteursup = imagesy($imgsup);
1262 | //fin
1263 | $hauteur = $hauteurexp + $hauteursup;
1264 | $largeur = max($largeursup, $largeurexp) + ceil($taille / 8);
1265 | $imgfin = ImageCreate(max($largeur, 1), max($hauteur, 1));
1266 | $noir = ImageColorAllocate($imgfin, 0, 0, 0);
1267 | $blanc = ImageColorAllocate($imgfin, 255, 255, 255);
1268 | $blanc = imagecolortransparent($imgfin, $blanc);
1269 | ImageFilledRectangle($imgfin, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1270 | ImageCopy($imgfin, $imgsup, ($largeur - $largeursup) / 2, 0, 0, 0, $largeursup, $hauteursup);
1271 | ImageCopy($imgfin, $imgexp, ($largeur - $largeurexp) / 2, $hauteursup, 0, 0, $largeurexp, $hauteurexp);
1272 | $this->image = $imgfin;
1273 | $this->base_verticale = $baseexp + $hauteursup;
1274 | }
1275 |
1276 | function dessine_dessous($taille)
1277 | {
1278 | $this->noeuds[2]->dessine($taille * 0.8);
1279 | $imginf = $this->noeuds[2]->image;
1280 | $baseinf = $this->noeuds[2]->base_verticale;
1281 | $this->noeuds[0]->dessine($taille);
1282 | $imgexp = $this->noeuds[0]->image;
1283 | $baseexp = $this->noeuds[0]->base_verticale;
1284 | //expression
1285 | $largeurexp = imagesx($imgexp);
1286 | $hauteurexp = imagesy($imgexp);
1287 | //borneinf
1288 | $largeurinf = imagesx($imginf);
1289 | $hauteurinf = imagesy($imginf);
1290 | //fin
1291 | $hauteur = $hauteurexp + $hauteurinf;
1292 | $largeur = max($largeurinf, $largeurexp) + ceil($taille / 8);
1293 | $imgfin = ImageCreate(max($largeur, 1), max($hauteur, 1));
1294 | $noir = ImageColorAllocate($imgfin, 0, 0, 0);
1295 | $blanc = ImageColorAllocate($imgfin, 255, 255, 255);
1296 | $blanc = imagecolortransparent($imgfin, $blanc);
1297 | ImageFilledRectangle($imgfin, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1298 | ImageCopy($imgfin, $imgexp, ($largeur - $largeurexp) / 2, 0, 0, 0, $largeurexp, $hauteurexp);
1299 | ImageCopy($imgfin, $imginf, ($largeur - $largeurinf) / 2, $hauteurexp, 0, 0, $largeurinf, $hauteurinf);
1300 | $this->image = $imgfin;
1301 | $this->base_verticale = $baseexp;
1302 | }
1303 |
1304 | function dessine_matrice($taille)
1305 | {
1306 | $padding = 8;
1307 | $nbligne = $this->noeuds[1]->noeuds[0]->texte;
1308 | $nbcolonne = $this->noeuds[2]->noeuds[0]->texte;
1309 | $largeur_case = 0;
1310 | $hauteur_case = 0;
1311 |
1312 | for ($ligne = 0; $ligne < $nbligne; $ligne++) {
1313 | $hauteur_ligne[$ligne] = 0;
1314 | $dessus_ligne[$ligne] = 0;
1315 | }
1316 | for ($col = 0; $col < $nbcolonne; $col++) {
1317 | $largeur_colonne[$col] = 0;
1318 | }
1319 | $i = 0;
1320 | for ($ligne = 0; $ligne < $nbligne; $ligne++) {
1321 | for ($col = 0; $col < $nbcolonne; $col++) {
1322 | if ($i < count($this->noeuds[3]->noeuds)) {
1323 | $this->noeuds[3]->noeuds[$i]->dessine($taille * 0.9);
1324 | $img[$i] = $this->noeuds[3]->noeuds[$i]->image;
1325 | $base[$i] = $this->noeuds[3]->noeuds[$i]->base_verticale;
1326 | $dessus_ligne[$ligne] = max($base[$i], $dessus_ligne[$ligne]);
1327 | $largeur[$i] = imagesx($img[$i]);
1328 | $hauteur[$i] = imagesy($img[$i]);
1329 | $hauteur_ligne[$ligne] = max($hauteur_ligne[$ligne], $hauteur[$i]);
1330 | $largeur_colonne[$col] = max($largeur_colonne[$col], $largeur[$i]);
1331 | }
1332 | $i++;
1333 | }
1334 | }
1335 |
1336 | $hauteurfin = 0;
1337 | $largeurfin = 0;
1338 | for ($ligne = 0; $ligne < $nbligne; $ligne++) {
1339 | $hauteurfin+=$hauteur_ligne[$ligne] + $padding;
1340 | }
1341 | for ($col = 0; $col < $nbcolonne; $col++) {
1342 | $largeurfin+=$largeur_colonne[$col] + $padding;
1343 | }
1344 | $hauteurfin-=$padding;
1345 | $largeurfin-=$padding;
1346 | $imgfin = ImageCreate(max($largeurfin, 1), max($hauteurfin, 1));
1347 | $noir = ImageColorAllocate($imgfin, 0, 0, 0);
1348 | $blanc = ImageColorAllocate($imgfin, 255, 255, 255);
1349 | $blanc = imagecolortransparent($imgfin, $blanc);
1350 | ImageFilledRectangle($imgfin, 0, 0, $largeurfin - 1, $hauteurfin - 1, $blanc);
1351 | $i = 0;
1352 | $h = $padding / 2 - 1;
1353 | for ($ligne = 0; $ligne < $nbligne; $ligne++) {
1354 | $l = $padding / 2 - 1;
1355 | for ($col = 0; $col < $nbcolonne; $col++) {
1356 | if ($i < count($this->noeuds[3]->noeuds)) {
1357 | ImageCopy($imgfin, $img[$i], $l + ceil($largeur_colonne[$col] - $largeur[$i]) / 2, $h + $dessus_ligne[$ligne] - $base[$i], 0, 0, $largeur[$i], $hauteur[$i]);
1358 | //ImageRectangle($imgfin,$l,$h,$l+$largeur_colonne[$col],$h+$hauteur_ligne[$ligne],$noir);
1359 | }
1360 | $l+=$largeur_colonne[$col] + $padding;
1361 | $i++;
1362 | }
1363 | $h+=$hauteur_ligne[$ligne] + $padding;
1364 | }
1365 | //ImageRectangle($imgfin,0,0,$largeurfin-1,$hauteurfin-1,$noir);
1366 | $this->image = $imgfin;
1367 | $this->base_verticale = imagesy($imgfin) / 2;
1368 | }
1369 |
1370 | function dessine_tableau($taille)
1371 | {
1372 | $padding = 8;
1373 | $typeligne = $this->noeuds[1]->noeuds[0]->texte;
1374 | $typecolonne = $this->noeuds[2]->noeuds[0]->texte;
1375 | $nbligne = strlen($typeligne) - 1;
1376 | $nbcolonne = strlen($typecolonne) - 1;
1377 | $largeur_case = 0;
1378 | $hauteur_case = 0;
1379 |
1380 | for ($ligne = 0; $ligne < $nbligne; $ligne++) {
1381 | $hauteur_ligne[$ligne] = 0;
1382 | $dessus_ligne[$ligne] = 0;
1383 | }
1384 | for ($col = 0; $col < $nbcolonne; $col++) {
1385 | $largeur_colonne[$col] = 0;
1386 | }
1387 | $i = 0;
1388 | for ($ligne = 0; $ligne < $nbligne; $ligne++) {
1389 | for ($col = 0; $col < $nbcolonne; $col++) {
1390 | if ($i < count($this->noeuds[3]->noeuds)) {
1391 | $this->noeuds[3]->noeuds[$i]->dessine($taille * 0.9);
1392 | $img[$i] = $this->noeuds[3]->noeuds[$i]->image;
1393 | $base[$i] = $this->noeuds[3]->noeuds[$i]->base_verticale;
1394 | $dessus_ligne[$ligne] = max($base[$i], $dessus_ligne[$ligne]);
1395 | $largeur[$i] = imagesx($img[$i]);
1396 | $hauteur[$i] = imagesy($img[$i]);
1397 | $hauteur_ligne[$ligne] = max($hauteur_ligne[$ligne], $hauteur[$i]);
1398 | $largeur_colonne[$col] = max($largeur_colonne[$col], $largeur[$i]);
1399 | }
1400 | $i++;
1401 | }
1402 | }
1403 |
1404 | $hauteurfin = 0;
1405 | $largeurfin = 0;
1406 | for ($ligne = 0; $ligne < $nbligne; $ligne++) {
1407 | $hauteurfin+=$hauteur_ligne[$ligne] + $padding;
1408 | }
1409 | for ($col = 0; $col < $nbcolonne; $col++) {
1410 | $largeurfin+=$largeur_colonne[$col] + $padding;
1411 | }
1412 | $imgfin = ImageCreate(max($largeurfin, 1), max($hauteurfin, 1));
1413 | $noir = ImageColorAllocate($imgfin, 0, 0, 0);
1414 | $blanc = ImageColorAllocate($imgfin, 255, 255, 255);
1415 | $blanc = imagecolortransparent($imgfin, $blanc);
1416 | ImageFilledRectangle($imgfin, 0, 0, $largeurfin - 1, $hauteurfin - 1, $blanc);
1417 | $i = 0;
1418 | $h = $padding / 2 - 1;
1419 | if (substr($typeligne, 0, 1) == "1") {
1420 | ImageLine($imgfin, 0, 0, $largeurfin - 1, 0, $noir);
1421 | }
1422 | for ($ligne = 0; $ligne < $nbligne; $ligne++) {
1423 | $l = $padding / 2 - 1;
1424 | if (substr($typecolonne, 0, 1) == "1") {
1425 | ImageLine($imgfin, 0, $h - $padding / 2, 0, $h + $hauteur_ligne[$ligne] + $padding / 2, $noir);
1426 | }
1427 | for ($col = 0; $col < $nbcolonne; $col++) {
1428 | if ($i < count($this->noeuds[3]->noeuds)) {
1429 | ImageCopy($imgfin, $img[$i], $l + ceil($largeur_colonne[$col] - $largeur[$i]) / 2, $h + $dessus_ligne[$ligne] - $base[$i], 0, 0, $largeur[$i], $hauteur[$i]);
1430 | if (substr($typecolonne, $col + 1, 1) == "1") {
1431 | ImageLine($imgfin, $l + $largeur_colonne[$col] + $padding / 2, $h - $padding / 2, $l + $largeur_colonne[$col] + $padding / 2, $h + $hauteur_ligne[$ligne] + $padding / 2, $noir);
1432 | }
1433 | }
1434 | $l+=$largeur_colonne[$col] + $padding;
1435 | $i++;
1436 | }
1437 | if (substr($typeligne, $ligne + 1, 1) == "1") {
1438 | ImageLine($imgfin, 0, $h + $hauteur_ligne[$ligne] + $padding / 2, $largeurfin - 1, $h + $hauteur_ligne[$ligne] + $padding / 2, $noir);
1439 | }
1440 | $h+=$hauteur_ligne[$ligne] + $padding;
1441 | }
1442 | $this->image = $imgfin;
1443 | $this->base_verticale = imagesy($imgfin) / 2;
1444 | }
1445 |
1446 | function dessine_vecteur($taille)
1447 | {
1448 | //expression
1449 | $this->noeuds[1]->dessine($taille);
1450 | $imgexp = $this->noeuds[1]->image;
1451 | $baseexp = $this->noeuds[1]->base_verticale;
1452 | $largeurexp = imagesx($imgexp);
1453 | $hauteurexp = imagesy($imgexp);
1454 | //fleche
1455 | $imgsup = affiche_symbol("right", 16);
1456 | $largeursup = imagesx($imgsup);
1457 | $hauteursup = imagesy($imgsup);
1458 | //fin
1459 | $hauteur = $hauteurexp + $hauteursup;
1460 | $largeur = $largeurexp;
1461 | $imgfin = ImageCreate(max($largeur, 1), max($hauteur, 1));
1462 | $noir = ImageColorAllocate($imgfin, 0, 0, 0);
1463 | $blanc = ImageColorAllocate($imgfin, 255, 255, 255);
1464 | $blanc = imagecolortransparent($imgfin, $blanc);
1465 | ImageFilledRectangle($imgfin, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1466 | ImageCopy($imgfin, $imgsup, $largeur - 6, 0, $largeursup - 6, 0, $largeursup, $hauteursup);
1467 | imagesetthickness($imgfin, 1);
1468 | imageline($imgfin, 0, 6, $largeur - 4, 6, $noir);
1469 | ImageCopy($imgfin, $imgexp, ($largeur - $largeurexp) / 2, $hauteursup, 0, 0, $largeurexp, $hauteurexp);
1470 | $this->image = $imgfin;
1471 | $this->base_verticale = $baseexp + $hauteursup;
1472 | }
1473 |
1474 | function dessine_overline($taille)
1475 | {
1476 | //expression
1477 | $this->noeuds[1]->dessine($taille);
1478 | $imgexp = $this->noeuds[1]->image;
1479 | $baseexp = $this->noeuds[1]->base_verticale;
1480 | $largeurexp = imagesx($imgexp);
1481 | $hauteurexp = imagesy($imgexp);
1482 |
1483 | $hauteur = $hauteurexp + 2;
1484 | $largeur = $largeurexp;
1485 | $imgfin = ImageCreate(max($largeur, 1), max($hauteur, 1));
1486 | $noir = ImageColorAllocate($imgfin, 0, 0, 0);
1487 | $blanc = ImageColorAllocate($imgfin, 255, 255, 255);
1488 | $blanc = imagecolortransparent($imgfin, $blanc);
1489 | ImageFilledRectangle($imgfin, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1490 | imagesetthickness($imgfin, 1);
1491 | imageline($imgfin, 0, 1, $largeur, 1, $noir);
1492 | ImageCopy($imgfin, $imgexp, 0, 2, 0, 0, $largeurexp, $hauteurexp);
1493 | $this->image = $imgfin;
1494 | $this->base_verticale = $baseexp + 2;
1495 | }
1496 |
1497 | function dessine_underline($taille)
1498 | {
1499 | //expression
1500 | $this->noeuds[1]->dessine($taille);
1501 | $imgexp = $this->noeuds[1]->image;
1502 | $baseexp = $this->noeuds[1]->base_verticale;
1503 | $largeurexp = imagesx($imgexp);
1504 | $hauteurexp = imagesy($imgexp);
1505 |
1506 | $hauteur = $hauteurexp + 2;
1507 | $largeur = $largeurexp;
1508 | $imgfin = ImageCreate(max($largeur, 1), max($hauteur, 1));
1509 | $noir = ImageColorAllocate($imgfin, 0, 0, 0);
1510 | $blanc = ImageColorAllocate($imgfin, 255, 255, 255);
1511 | $blanc = imagecolortransparent($imgfin, $blanc);
1512 | ImageFilledRectangle($imgfin, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1513 | imagesetthickness($imgfin, 1);
1514 | imageline($imgfin, 0, $hauteurexp + 1, $largeur, $hauteurexp + 1, $noir);
1515 | ImageCopy($imgfin, $imgexp, 0, 0, 0, 0, $largeurexp, $hauteurexp);
1516 | $this->image = $imgfin;
1517 | $this->base_verticale = $baseexp;
1518 | }
1519 |
1520 | function dessine_chapeau($taille)
1521 | {
1522 |
1523 | $imgsup = affiche_symbol("_hat", $taille);
1524 |
1525 | $this->noeuds[1]->dessine($taille);
1526 | $imgexp = $this->noeuds[1]->image;
1527 | $baseexp = $this->noeuds[1]->base_verticale;
1528 | //expression
1529 | $largeurexp = imagesx($imgexp);
1530 | $hauteurexp = imagesy($imgexp);
1531 | //bornesup
1532 | $largeursup = imagesx($imgsup);
1533 | $hauteursup = imagesy($imgsup);
1534 | //fin
1535 | $hauteur = $hauteurexp + $hauteursup;
1536 | $largeur = max($largeursup, $largeurexp) + ceil($taille / 8);
1537 | $imgfin = ImageCreate(max($largeur, 1), max($hauteur, 1));
1538 | $noir = ImageColorAllocate($imgfin, 0, 0, 0);
1539 | $blanc = ImageColorAllocate($imgfin, 255, 255, 255);
1540 | $blanc = imagecolortransparent($imgfin, $blanc);
1541 | ImageFilledRectangle($imgfin, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1542 | ImageCopy($imgfin, $imgsup, ($largeur - $largeursup) / 2, 0, 0, 0, $largeursup, $hauteursup);
1543 | ImageCopy($imgfin, $imgexp, ($largeur - $largeurexp) / 2, $hauteursup, 0, 0, $largeurexp, $hauteurexp);
1544 | $this->image = $imgfin;
1545 | $this->base_verticale = $baseexp + $hauteursup;
1546 | }
1547 |
1548 | function dessine_limite($taille)
1549 | {
1550 | $imglim = affiche_math("_lim", $taille);
1551 | $largeurlim = imagesx($imglim);
1552 | $hauteurlim = imagesy($imglim);
1553 | $baselim = $hauteurlim / 2;
1554 |
1555 | $this->noeuds[1]->dessine($taille * 0.8);
1556 | $imginf = $this->noeuds[1]->image;
1557 | $baseinf = $this->noeuds[1]->base_verticale;
1558 | $largeurinf = imagesx($imginf);
1559 | $hauteurinf = imagesy($imginf);
1560 |
1561 | $this->noeuds[2]->dessine($taille);
1562 | $imgexp = $this->noeuds[2]->image;
1563 | $baseexp = $this->noeuds[2]->base_verticale;
1564 | $largeurexp = imagesx($imgexp);
1565 | $hauteurexp = imagesy($imgexp);
1566 |
1567 | $hauteur = $hauteurlim + $hauteurinf;
1568 | $largeur = max($largeurinf, $largeurlim) + ceil($taille / 8);
1569 | $imgfin = ImageCreate(max($largeur, 1), max($hauteur, 1));
1570 | $noir = ImageColorAllocate($imgfin, 0, 0, 0);
1571 | $blanc = ImageColorAllocate($imgfin, 255, 255, 255);
1572 | $blanc = imagecolortransparent($imgfin, $blanc);
1573 | ImageFilledRectangle($imgfin, 0, 0, $largeur - 1, $hauteur - 1, $blanc);
1574 | ImageCopy($imgfin, $imglim, ($largeur - $largeurlim) / 2, 0, 0, 0, $largeurlim, $hauteurlim);
1575 | ImageCopy($imgfin, $imginf, ($largeur - $largeurinf) / 2, $hauteurlim, 0, 0, $largeurinf, $hauteurinf);
1576 |
1577 | $this->image = alignement2($imgfin, $baselim, $imgexp, $baseexp);
1578 | $this->base_verticale = max($baselim, $baseexp);
1579 | }
1580 |
1581 | function dessine_delimiteur($taille)
1582 | {
1583 | $this->noeuds[2]->dessine($taille);
1584 | $imgexp = $this->noeuds[2]->image;
1585 | $baseexp = $this->noeuds[2]->base_verticale;
1586 | $hauteurexp = imagesy($imgexp);
1587 | if ($this->noeuds[1]->texte == "&$") {
1588 | $imggauche = parenthese($hauteurexp, $this->noeuds[1]->noeuds[0]->texte);
1589 | } else {
1590 | $imggauche = parenthese($hauteurexp, $this->noeuds[1]->texte);
1591 | }
1592 | $basegauche = imagesy($imggauche) / 2;
1593 | if ($this->noeuds[3]->texte == "&$") {
1594 | $imgdroit = parenthese($hauteurexp, $this->noeuds[3]->noeuds[0]->texte);
1595 | } else {
1596 | $imgdroit = parenthese($hauteurexp, $this->noeuds[3]->texte);
1597 | }
1598 | $basedroit = imagesy($imgdroit) / 2;
1599 | $this->image = alignement3($imggauche, $basegauche, $imgexp, $baseexp, $imgdroit, $basedroit);
1600 | $this->base_verticale = max($basegauche, $baseexp, $basedroit);
1601 | }
1602 | }
1603 |
1604 | //******************************************************************************************
1605 |
1606 | function detectimg($n)
1607 | {
1608 | /*
1609 | Detects if the formula image already exists in the $dirimg cache directory.
1610 | In that case, the function returns a parameter (recorded in the name of the image file) which allows to align correctly the image with the text.
1611 | */
1612 | $dirimg = GlobalVar::$dirimg;
1613 | $ret = 0;
1614 | $handle = opendir($dirimg);
1615 | while ($fi = readdir($handle)) {
1616 | $info = pathinfo($fi);
1617 | if ($fi != "." && $fi != ".." && $info["extension"] == "png" && preg_match("/^math/", $fi)) {
1618 | list($math, $v, $name) = explode("_", $fi);
1619 | if ($name == $n) {
1620 | $ret = $v;
1621 | break;
1622 | }
1623 | }
1624 | }
1625 | closedir($handle);
1626 | return $ret;
1627 | }
1628 |
1629 | function mathimage($text, $size, $pathtoimg)
1630 | {
1631 | /*
1632 | Creates the formula image (if the image is not in the cache) and returns the
html code.
1633 | */
1634 | $dirimg = GlobalVar::$dirimg;
1635 | $nameimg = md5(trim($text) . $size) . '.png';
1636 | $v = detectimg($nameimg);
1637 | if ($v == 0) {
1638 | //the image doesn't exist in the cache directory. we create it.
1639 | $formula = new expression_math(tableau_expression(trim($text)));
1640 | $formula->dessine($size);
1641 | $v = 1000 - imagesy($formula->image) + $formula->base_verticale + 3;
1642 | //1000+baseline ($v) is recorded in the name of the image
1643 | ImagePNG($formula->image, $dirimg . "/math_" . $v . "_" . $nameimg);
1644 | }
1645 | $valign = $v - 1000;
1646 | return '
';
1647 | }
1648 |
1649 | function mathfilter($text, $size, $pathtoimg)
1650 | {
1651 | /* THE MAIN FUNCTION
1652 | 1) the content of the math tags () are extracted in the $t variable (you can replace by your own tag).
1653 | 2) the "mathimage" function replaces the $t code by
according to this method :
1654 | - if the image corresponding to the formula doesn't exist in the $dirimg cache directory (detectimg($nameimg)=0), the script creates the image and returns the "
" code.
1655 | - otherwise, the script returns only the
" code.
1656 | To align correctly the formula image with the text, the "valign" parameter of the image is required.
1657 | That's why a parameter (1000+valign) is recorded in the name of the image file (the "detectimg" function returns this parameter if the image exists in the cache directory)
1658 | To be sure that the name of the image file is unique and to allow the script to retrieve the valign parameter without re-creating the image, the syntax of the image filename is :
1659 | math_(1000+valign)_md5(formulatext.size).png.
1660 | (1000+valign is used instead of valign directly to avoid a negative number)
1661 | */
1662 | $text = stripslashes($text);
1663 | $size = max($size, 10);
1664 | $size = min($size, 24);
1665 | preg_match_all("|(.*?)|", $text, $regs, PREG_SET_ORDER);
1666 | foreach ($regs as $math) {
1667 | $t = str_replace('', '', $math[0]);
1668 | $t = str_replace('', '', $t);
1669 | $code = mathimage(trim($t), $size, $pathtoimg);
1670 | $text = str_replace($math[0], $code, $text);
1671 | }
1672 | return $text;
1673 | }
1674 |
--------------------------------------------------------------------------------