Ejemplo n.º 1
0
function tex2image($texexp, $md5, $return = false)
{
    global $CFG;
    if (!$texexp) {
        echo 'No tex expresion specified';
        return;
    }
    $texexp = '\\Large ' . $texexp;
    $image = $md5 . ".gif";
    $filetype = 'image/gif';
    if (!file_exists("{$CFG->dataroot}/filter/algebra")) {
        make_upload_directory("filter/algebra");
    }
    $pathname = "{$CFG->dataroot}/filter/algebra/{$image}";
    if (file_exists($pathname)) {
        unlink($pathname);
    }
    $commandpath = filter_tex_get_executable(true);
    $cmd = filter_tex_get_cmd($pathname, $texexp);
    system($cmd, $status);
    if ($return) {
        return $image;
    }
    if (file_exists($pathname)) {
        send_file($pathname, $image);
    } else {
        $ecmd = "{$cmd} 2>&1";
        echo `{$ecmd}` . "<br />\n";
        echo "The shell command<br />{$cmd}<br />returned status = {$status}<br />\n";
        if ($status == 4) {
            echo "Status corresponds to illegal instruction<br />\n";
        } else {
            if ($status == 11) {
                echo "Status corresponds to bus error<br />\n";
            } else {
                if ($status == 22) {
                    echo "Status corresponds to abnormal termination<br />\n";
                }
            }
        }
        if (file_exists($commandpath)) {
            echo "File size of mimetex executable  {$commandpath} is " . filesize($commandpath) . "<br />";
            echo "The file permissions are: " . decoct(fileperms($commandpath)) . "<br />";
            if (function_exists("md5_file")) {
                echo "The md5 checksum of the file is " . md5_file($commandpath) . "<br />";
            } else {
                $handle = fopen($commandpath, "rb");
                $contents = fread($handle, 16384);
                fclose($handle);
                echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />";
            }
        } else {
            echo "mimetex executable {$commandpath} not found!<br />";
        }
        echo "Image not found!";
    }
}
Ejemplo n.º 2
0
 /**
  * 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('&lt;', '<', $texexp);
                                 $texexp = str_replace('&gt;', '>', $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;
 }
Ejemplo n.º 3
0
    $pathname = $CFG->dataroot . '/filter/algebra/' . $image;
} else {
    print_error('invalidarguments', 'error');
}
if (!file_exists($pathname)) {
    $md5 = str_replace('.gif', '', $image);
    if ($texcache = $DB->get_record('cache_filters', array('filter' => 'algebra', 'md5key' => $md5))) {
        if (!file_exists($CFG->dataroot . '/filter/algebra')) {
            make_upload_directory('filter/algebra');
        }
        $texexp = $texcache->rawtext;
        $texexp = str_replace('&lt;', '<', $texexp);
        $texexp = str_replace('&gt;', '>', $texexp);
        $texexp = preg_replace('!\\r\\n?!', ' ', $texexp);
        $texexp = '\\Large ' . $texexp;
        $cmd = filter_tex_get_cmd($pathname, $texexp);
        system($cmd, $status);
    }
}
if (file_exists($pathname)) {
    send_file($pathname, $image);
} else {
    if (debugging()) {
        echo "The shell command<br />{$cmd}<br />returned status = {$status}<br />\n";
        echo "Image not found!<br />";
        echo "Please try the <a href=\"{$CFG->wwwroot}/filter/algebra/algebradebug.php\">debugging script</a>";
    } else {
        echo "Image not found!<br />";
        echo "Please try the <a href=\"{$CFG->wwwroot}/filter/algebra/algebradebug.php\">debugging script</a><br />";
        echo "Please turn on debug mode in site configuration to see more info here.";
    }
Ejemplo n.º 4
0
    function tex2image($texexp, $return=false) {
        global $CFG;

        if (!$texexp) {
            echo 'No tex expresion specified';
            return;
        }

        $image  = md5($texexp) . ".gif";
        $filetype = 'image/gif';
        if (!file_exists("$CFG->dataroot/filter/tex")) {
            make_upload_directory("filter/tex");
        }
        $pathname = "$CFG->dataroot/filter/tex/$image";
        if (file_exists($pathname)) {
            unlink($pathname);
        }

        $texexp = '\Large '.$texexp;
        $commandpath = filter_tex_get_executable(true);
        $cmd = filter_tex_get_cmd($pathname, $texexp);
        system($cmd, $status);

        if ($return) {
          return $image;
        }

        if (file_exists($pathname)) {
            send_file($pathname, $image);

        } else if (debugging()) {
            $ecmd = "$cmd 2>&1";
            echo `$ecmd` . "<br />\n";
            echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
            if ($status == 4) {
                echo "Status corresponds to illegal instruction<br />\n";
            } else if ($status == 11) {
                echo "Status corresponds to bus error<br />\n";
            } else if ($status == 22) {
                echo "Status corresponds to abnormal termination<br />\n";
            }
            if (file_exists($commandpath)) {
                echo "File size of mimetex executable  $commandpath is " . filesize($commandpath) . "<br />";
                echo "The file permissions are: " . decoct(fileperms($commandpath)) . "<br />";
                if (function_exists("md5_file")) {
                    echo "The md5 checksum of the file is " . md5_file($commandpath) . "<br />";
                } else {
                    $handle = fopen($commandpath,"rb");
                    $contents = fread($handle,16384);
                    fclose($handle);
                    echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />";
                }
            } else {
                echo "mimetex executable $commandpath not found!<br />";
            }
            echo "Image not found!";
        } else {
            echo "Can not output detailed information due to security concerns, please turn on debug mode first.";
        }
    }