weekly release 3.6dev
[moodle.git] / filter / tex / pix.php
blobd66faf223948e5d881cfcd96169cfefa6e6e4288
1 <?php
2 // This function fetches math. images from the data directory
3 // If not, it obtains the corresponding TeX expression from the cache_tex db table
4 // and uses mimeTeX to create the image file
6 // disable moodle specific debug messages and any errors in output
7 define('NO_DEBUG_DISPLAY', true);
8 define('NO_MOODLE_COOKIES', true); // Because it interferes with caching
10 require_once('../../config.php');
12 if (!filter_is_enabled('tex')) {
13 print_error('filternotenabled');
16 require_once($CFG->libdir.'/filelib.php');
17 require_once($CFG->dirroot.'/filter/tex/lib.php');
18 require_once($CFG->dirroot.'/filter/tex/latex.php');
20 $cmd = ''; // Initialise these variables
21 $status = '';
23 $relativepath = get_file_argument();
25 $args = explode('/', trim($relativepath, '/'));
27 if (count($args) == 1) {
28 $image = $args[0];
29 $pathname = $CFG->dataroot.'/filter/tex/'.$image;
30 } else {
31 print_error('invalidarguments', 'error');
34 if (!file_exists($pathname)) {
35 $convertformat = get_config('filter_tex', 'convertformat');
36 if (strpos($image, '.png')) {
37 $convertformat = 'png';
39 $md5 = str_replace(".{$convertformat}", '', $image);
40 if ($texcache = $DB->get_record('cache_filters', array('filter'=>'tex', 'md5key'=>$md5))) {
41 if (!file_exists($CFG->dataroot.'/filter/tex')) {
42 make_upload_directory('filter/tex');
45 // try and render with latex first
46 $latex = new latex();
47 $density = get_config('filter_tex', 'density');
48 $background = get_config('filter_tex', 'latexbackground');
49 $texexp = $texcache->rawtext; // the entities are now decoded before inserting to DB
50 $lateximage = $latex->render($texexp, $image, 12, $density, $background);
51 if ($lateximage) {
52 copy($lateximage, $pathname);
53 $latex->clean_up($md5);
55 } else {
56 // failing that, use mimetex
57 $texexp = $texcache->rawtext;
58 $texexp = str_replace('&lt;', '<', $texexp);
59 $texexp = str_replace('&gt;', '>', $texexp);
60 $texexp = preg_replace('!\r\n?!', ' ', $texexp);
61 $texexp = '\Large '.$texexp;
62 $cmd = filter_tex_get_cmd($pathname, $texexp);
63 system($cmd, $status);
68 if (file_exists($pathname)) {
69 send_file($pathname, $image);
70 } else {
71 if (debugging()) {
72 echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
73 echo "Image not found!<br />";
74 echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a>";
75 } else {
76 echo "Image not found!<br />";
77 echo "Please try the <a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">debugging script</a><br />";
78 echo "Please turn on debug mode in site configuration to see more info here.";