function TexOutput($expression, $graphic = false) { global $CFG; $output = ''; $latex = new latex(); // first check if it is likely to work at all $output .= "<h3>Checking executables</h3>\n"; $executables_exist = true; if (is_file($CFG->filter_tex_pathlatex)) { $output .= "latex executable ({$CFG->filter_tex_pathlatex}) is readable<br />\n"; } else { $executables_exist = false; $output .= "<b>Error:</b> latex executable ({$CFG->filter_tex_pathlatex}) is not readable<br />\n"; } if (is_file($CFG->filter_tex_pathdvips)) { $output .= "dvips executable ({$CFG->filter_tex_pathdvips}) is readable<br />\n"; } else { $executables_exist = false; $output .= "<b>Error:</b> dvips executable ({$CFG->filter_tex_pathdvips}) is not readable<br />\n"; } if (is_file($CFG->filter_tex_pathconvert)) { $output .= "convert executable ({$CFG->filter_tex_pathconvert}) is readable<br />\n"; } else { $executables_exist = false; $output .= "<b>Error:</b> convert executable ({$CFG->filter_tex_pathconvert}) is not readable<br />\n"; } // knowing that it might work.. $md5 = md5($expression); $output .= "<p>base filename for expression is '{$md5}'</p>\n"; // temporary paths $tex = "{$latex->temp_dir}/{$md5}.tex"; $dvi = "{$latex->temp_dir}/{$md5}.dvi"; $ps = "{$latex->temp_dir}/{$md5}.ps"; $img = "{$latex->temp_dir}/{$md5}.{$CFG->filter_tex_convertformat}"; // put the expression as a file into the temp area $expression = html_entity_decode($expression); $output .= "<p>Processing TeX expression:</p><pre>{$expression}</pre>\n"; $doc = $latex->construct_latex_document($expression); $fh = fopen($tex, 'w'); fputs($fh, $doc); fclose($fh); // cd to temp dir chdir($latex->temp_dir); // step 1: latex command $cmd = "{$CFG->filter_tex_pathlatex} --interaction=nonstopmode {$tex}"; $output .= execute($cmd); // step 2: dvips command $cmd = "{$CFG->filter_tex_pathdvips} -E {$dvi} -o {$ps}"; $output .= execute($cmd); // step 3: convert command $cmd = "{$CFG->filter_tex_pathconvert} -density 240 -trim {$ps} {$img} "; $output .= execute($cmd); if (!$graphic) { echo $output; } else { if (file_exists($img)) { send_file($img, "{$md5}.{$CFG->filter_tex_convertformat}"); } else { echo "Error creating image, see command execution output for more details."; } } }
$relativepath = get_file_argument('pix.php'); $args = explode('/', trim($relativepath, '/')); if (count($args) == 1) { $image = $args[0]; $pathname = $CFG->dataroot . '/filter/tex/' . $image; } else { error('No valid arguments supplied'); } if (!file_exists($pathname)) { $md5 = str_replace(".{$CFG->filter_tex_convertformat}", '', $image); if ($texcache = get_record('cache_filters', 'filter', 'tex', 'md5key', $md5)) { if (!file_exists($CFG->dataroot . '/filter/tex')) { make_upload_directory('filter/tex'); } // try and render with latex first $latex = new latex(); $density = $CFG->filter_tex_density; $background = $CFG->filter_tex_latexbackground; $texexp = html_entity_decode($texcache->rawtext); $latex_path = $latex->render($texexp, $md5, 12, $density, $background); if ($latex_path) { copy($latex_path, $pathname); $latex->clean_up($md5); } else { // failing that, use mimetex $texexp = $texcache->rawtext; $texexp = str_replace('<', '<', $texexp); $texexp = str_replace('>', '>', $texexp); $texexp = preg_replace('!\\r\\n?!', ' ', $texexp); $texexp = '\\Large ' . $texexp; $cmd = tex_filter_get_cmd($pathname, $texexp);
/** * Function to replace @@PLUGINFILE@@ references to image files by local file:// URLs. * * @param string $input The input string (Moodle HTML) * @param int $contextid The context ID. * @param string $filearea The filearea used to locate the image files. * @param int $itemid The itemid used to locate the image files. * @param float $kfactor A magnification factor. * @param int $maxwidth The maximum width in pixels for images. * @return string The result string */ public function fix_image_paths($input, $contextid, $filearea, $itemid, $kfactor, $maxwidth, $format = 'pdf') { global $CFG, $DB; require_once $CFG->dirroot . '/filter/tex/lib.php'; require_once $CFG->dirroot . '/filter/tex/latex.php'; $fs = get_file_storage(); $output = $input; $strings = preg_split("/<img/i", $output); $output = array_shift($strings); foreach ($strings as $string) { // Define a unique temporary name for each image file. srand(microtime() * 1000000); $unique = str_replace('.', '', microtime(true) . '_' . rand(0, 100000)); $imagetag = substr($string, 0, strpos($string, '>')); $attributestrings = explode(' ', $imagetag); $attributes = array(); foreach ($attributestrings as $attributestring) { $valuepair = explode('=', $attributestring); if (count($valuepair) > 1 && strlen(trim($valuepair[0])) > 0) { $attributes[strtolower(trim($valuepair[0]))] = str_replace('"', '', str_replace("'", '', $valuepair[1])); } } if (array_key_exists('width', $attributes) && $attributes['width'] > 0) { $imagewidth = $attributes['width']; } else { $imagewidth = 0; } if (array_key_exists('height', $attributes) && $attributes['height'] > 0) { $imageheight = $attributes['height']; } else { $imageheight = 0; } $imagefilename = ''; if (array_key_exists('src', $attributes) && strlen($attributes['src']) > 10) { $pluginfilename = $attributes['src']; $imageurl = false; $teximage = false; $pluginfile = false; $parts = preg_split("!{$CFG->wwwroot}/filter/tex/pix.php/!", $pluginfilename); if (preg_match('!@@PLUGINFILE@@/!', $pluginfilename)) { $pluginfilename = str_replace('@@PLUGINFILE@@/', '', $pluginfilename); $pathparts = pathinfo($pluginfilename); if (!empty($pathparts['dirname']) && $pathparts['dirname'] != '.') { $filepath = '/' . $pathparts['dirname'] . '/'; } else { $filepath = '/'; } if ($imagefile = $fs->get_file($contextid, 'question', $filearea, $itemid, $filepath, rawurldecode($pathparts['basename']))) { $imagefilename = $imagefile->get_filename(); // Copy image content to temporary file. $pathparts = pathinfo($imagefilename); $file = $CFG->dataroot . "/temp/offlinequiz/" . $unique . '.' . strtolower($pathparts["extension"]); clearstatcache(); if (!check_dir_exists($CFG->dataroot . "/temp/offlinequiz", true, true)) { print_error("Could not create data directory"); } $imagefile->copy_content_to($file); $pluginfile = true; } else { $output .= 'Image file not found ' . $pathparts['dirname'] . '/' . $pathparts['basename']; } } else { if (count($parts) > 1) { $teximagefile = $CFG->dataroot . '/filter/tex/' . $parts[1]; if (!file_exists($teximagefile)) { // Create the TeX image if it does not exist yet. $convertformat = $DB->get_field('config_plugins', 'value', array('plugin' => 'filter_tex', 'name' => 'convertformat')); $md5 = str_replace(".{$convertformat}", '', $parts[1]); if ($texcache = $DB->get_record('cache_filters', array('filter' => 'tex', 'md5key' => $md5))) { if (!file_exists($CFG->dataroot . '/filter/tex')) { make_upload_directory('filter/tex'); } // Try and render with latex first. $latex = new latex(); $density = $DB->get_field('config_plugins', 'value', array('plugin' => 'filter_tex', 'name' => 'density')); $background = $DB->get_field('config_plugins', 'value', array('plugin' => 'filter_tex', 'name' => 'latexbackground')); $texexp = $texcache->rawtext; // The entities are now decoded before inserting to DB. $latexpath = $latex->render($texexp, $md5, 12, $density, $background); if ($latexpath) { copy($latexpath, $teximagefile); $latex->clean_up($md5); } else { // Failing that, use mimetex. $texexp = $texcache->rawtext; $texexp = str_replace('<', '<', $texexp); $texexp = str_replace('>', '>', $texexp); $texexp = preg_replace('!\\r\\n?!', ' ', $texexp); $texexp = '\\Large ' . $texexp; $cmd = filter_tex_get_cmd($teximagefile, $texexp); system($cmd, $status); } } } $pathparts = pathinfo($teximagefile); $file = $CFG->dataroot . "/temp/offlinequiz/" . $unique . '.' . strtolower($pathparts["extension"]); clearstatcache(); if (!check_dir_exists($CFG->dataroot . "/temp/offlinequiz", true, true)) { print_error("Could not create data directory"); } copy($teximagefile, $file); $teximage = true; } else { // Image file URL. $imageurl = true; } } $factor = 2; // Per default show images half sized. if (!$imageurl) { if (!file_exists($file)) { $output .= get_string('imagenotfound', 'offlinequiz', $file); } else { // Use imagemagick to remove alpha channel and reduce resolution of large images. $imageinfo = getimagesize($file); $filewidth = $imageinfo[0]; $fileheight = $imageinfo[1]; $pathconvert = $DB->get_field('config_plugins', 'value', array('plugin' => 'filter_tex', 'name' => 'pathconvert')); if (file_exists($pathconvert)) { $newfile = $CFG->dataroot . "/temp/offlinequiz/" . $unique . '_c.png'; $resize = ''; $percent = round(200000000 / ($filewidth * $fileheight)); if ($percent < 100) { $resize = ' -resize ' . $percent . '%'; } $handle = popen($pathconvert . ' ' . $file . $resize . ' -background white -flatten +matte ' . $newfile, 'r'); pclose($handle); $this->tempfiles[] = $file; $file = $newfile; if ($percent < 100) { $imageinfo = getimagesize($file); $filewidth = $imageinfo[0]; $fileheight = $imageinfo[1]; } } if ($imagewidth > 0) { if ($imageheight > 0) { $fileheight = $imageheight; } else { $fileheight = $imagewidth / $filewidth * $fileheight; } $filewidth = $imagewidth; } if ($teximage) { if ($format == 'pdf') { $factor = $factor * 1.2; } else { $factor = $factor * 1.5; } } $width = round($filewidth / ($kfactor * $factor)); if ($width > $maxwidth) { $width = $maxwidth; } $height = round($fileheight * $width / $filewidth); // Add filename to list of temporary files. $this->tempfiles[] = $file; // In answer texts we want a line break to avoid the picture going above the line. if ($filearea == 'answer') { $output .= '<br/>'; } // Finally, add the image tag for tcpdf. $output .= '<img src="file://' . $file . '" align="middle" width="' . $width . '" height="' . $height . '"/>'; } } else { if ($imagewidth > 0 && $imageheight > 0) { $width = $imagewidth / ($kfactor * $factor); if ($width > $maxwidth) { $width = $maxwidth; } $height = $imageheight * $width / $imagewidth; $output .= '<img src="' . $pluginfilename . '" align="middle" width="' . $width . '" height="' . $height . '"/>'; } else { $output .= '<img src="' . $pluginfilename . '" align="middle"/>'; } } } $output .= substr($string, strpos($string, '>') + 1); } return $output; }
$pathname = $CFG->dataroot . '/filter/texwjax/' . $image; } else { print_error('invalidarguments', 'error'); } if (!file_exists($pathname)) { $convertformat = get_config('filter_texwjax', 'convertformat'); if (strpos($image, '.png')) { $convertformat = 'png'; } $md5 = str_replace(".{$convertformat}", '', $image); if ($texcache = $DB->get_record('cache_filters', array('filter' => 'texwjax', 'md5key' => $md5))) { if (!file_exists($CFG->dataroot . '/filter/texwjax')) { make_upload_directory('filter/texwjax'); } // try and render with latex first $latex = new latex(); $density = get_config('filter_texwjax', 'density'); $background = get_config('filter_texwjax', 'latexbackground'); $texexp = $texcache->rawtext; // the entities are now decoded before inserting to DB $lateximage = $latex->render($texexp, $image, 12, $density, $background); if ($lateximage) { copy($lateximage, $pathname); $latex->clean_up($md5); } else { // failing that, use mimetex $texexp = $texcache->rawtext; $texexp = str_replace('<', '<', $texexp); $texexp = str_replace('>', '>', $texexp); $texexp = preg_replace('!\\r\\n?!', ' ', $texexp); $texexp = '\\Large ' . $texexp;
function TexOutput($expression, $graphic=false) { global $CFG; $output = ''; $latex = new latex(); // first check if it is likely to work at all $output .= "<h3>Checking executables</h3>\n"; $executables_exist = true; if (is_file($CFG->filter_tex_pathlatex)) { $output .= "latex executable ($CFG->filter_tex_pathlatex) is readable<br />\n"; } else { $executables_exist = false; $output .= "<b>Error:</b> latex executable ($CFG->filter_tex_pathlatex) is not readable<br />\n"; } if (is_file($CFG->filter_tex_pathdvips)) { $output .= "dvips executable ($CFG->filter_tex_pathdvips) is readable<br />\n"; } else { $executables_exist = false; $output .= "<b>Error:</b> dvips executable ($CFG->filter_tex_pathdvips) is not readable<br />\n"; } if (is_file($CFG->filter_tex_pathconvert)) { $output .= "convert executable ($CFG->filter_tex_pathconvert) is readable<br />\n"; } else { $executables_exist = false; $output .= "<b>Error:</b> convert executable ($CFG->filter_tex_pathconvert) is not readable<br />\n"; } // knowing that it might work.. $md5 = md5($expression); $output .= "<p>base filename for expression is '$md5'</p>\n"; // temporary paths $tex = "$latex->temp_dir/$md5.tex"; $dvi = "$latex->temp_dir/$md5.dvi"; $ps = "$latex->temp_dir/$md5.ps"; $img = "$latex->temp_dir/$md5.{$CFG->filter_tex_convertformat}"; // put the expression as a file into the temp area $expression = html_entity_decode($expression); $output .= "<p>Processing TeX expression:</p><pre>$expression</pre>\n"; $doc = $latex->construct_latex_document($expression); $fh = fopen($tex, 'w'); fputs($fh, $doc); fclose($fh); // cd to temp dir chdir($latex->temp_dir); // step 1: latex command $cmd = "$CFG->filter_tex_pathlatex --interaction=nonstopmode $tex"; $output .= execute($cmd); // step 2: dvips command $cmd = "$CFG->filter_tex_pathdvips -E $dvi -o $ps"; $output .= execute($cmd); // step 3: convert command $cmd = "$CFG->filter_tex_pathconvert -density 240 -trim $ps $img "; $output .= execute($cmd); if (!$graphic) { echo($output); } else { send_file($img, "$md5.{$CFG->filter_tex_convertformat}"); } }
function TexOutput($expression, $graphic = false) { global $CFG; $output = ''; $latex = new latex(); // first check if it is likely to work at all $output .= "<h3>Checking executables</h3>\n"; $executables_exist = true; if (is_file($CFG->filter_tex_pathlatex)) { $output .= "latex executable ({$CFG->filter_tex_pathlatex}) is readable<br />\n"; } else { $executables_exist = false; $output .= "<b>Error:</b> latex executable ({$CFG->filter_tex_pathlatex}) is not readable<br />\n"; } if (is_file($CFG->filter_tex_pathdvips)) { $output .= "dvips executable ({$CFG->filter_tex_pathdvips}) is readable<br />\n"; } else { $executables_exist = false; $output .= "<b>Error:</b> dvips executable ({$CFG->filter_tex_pathdvips}) is not readable<br />\n"; } if (is_file($CFG->filter_tex_pathconvert)) { $output .= "convert executable ({$CFG->filter_tex_pathconvert}) is readable<br />\n"; } else { $executables_exist = false; $output .= "<b>Error:</b> convert executable ({$CFG->filter_tex_pathconvert}) is not readable<br />\n"; } // knowing that it might work.. $md5 = md5($expression); $output .= "<p>base filename for expression is '{$md5}'</p>\n"; // temporary paths $tex = "{$latex->temp_dir}/{$md5}.tex"; $dvi = "{$latex->temp_dir}/{$md5}.dvi"; $ps = "{$latex->temp_dir}/{$md5}.ps"; $gif = "{$latex->temp_dir}/{$md5}.gif"; // put the expression as a file into the temp area $expression = stripslashes($expression); $expression = html_entity_decode($expression); $output .= "<p>Processing TeX expression:</p><pre>{$expression}</pre>\n"; $doc = $latex->construct_latex_document($expression); $fh = fopen($tex, 'w'); fputs($fh, $doc); fclose($fh); // cd to temp dir chdir($latex->temp_dir); // step 1: latex command $cmd = "{$CFG->filter_tex_pathlatex} --interaction=nonstopmode {$tex}"; $output .= execute($cmd); // step 2: dvips command $cmd = "{$CFG->filter_tex_pathdvips} -E {$dvi} -o {$ps}"; $output .= execute($cmd); // step 3: convert command $cmd = "{$CFG->filter_tex_pathconvert} -density 240 -trim {$ps} {$gif} "; $output .= execute($cmd); if (!$graphic) { echo $output; } else { $lastmodified = filemtime($gif); $lifetime = 86400; $filetype = 'image/gif'; $image = "{$md5}.gif"; header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT"); header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT"); header("Cache-control: max_age = {$lifetime}"); // a day header("Pragma: "); header("Content-disposition: inline; filename={$image}"); header("Content-length: " . filesize($gif)); header("Content-type: {$filetype}"); readfile("{$gif}"); } }