/** * function render_image * Create the image and place it in the proper place * @param string $texcode The LaTeX code to render * @param integer $post_id The post id * @param integer $commend_id The commend id if a comment * @return string The image filename */ private function render_image($texcode, $post_id, $comment_id = null) { $this->error_texcode = rtrim($texcode, '/') . '/'; $tmp = Options::get('jLaTeX__tmp'); //Make sure the tmp directory exists if (!is_dir($tmp)) { mkdir($tmp, 0755, true); } $filename = md5($texcode); //Put the formula in the latex document template $latex_document = sprintf(Options::get('jLaTeX__template'), $texcode); //Set the textwidth of the document $textwidth = round(Options::get('jLaTeX__imagewidth') / Options::get('jLaTeX__imagedpi'), 2); $latex_document = preg_replace('/\\\\begin{document}/si', "\\\\setlength{\\\\textwidth}{" . $textwidth . "in}\n\\\\begin{document}", $latex_document); //Create the temproary TEX file file_put_contents($tmp . $filename . '.tex', $latex_document); //Create the temprorary DVI file $command = Options::get('jLaTeX__latex') . ' --interaction=nonstopmode ' . $filename . '.tex'; if ($this->do_command($command, $tmp) === false) { return false; } //Convert dvi file to png using dvipng $command = Options::get('jLaTeX__dvipng') . ' ' . $filename . '.dvi -pp 1 -D ' . Options::get('jLaTeX__imagedpi') . ' -T tight -o ' . $filename . '.png'; if ($this->do_command($command, $tmp) === false) { return false; } //Store the file in cache $file = $tmp . $filename . '.png'; $group = 'jLaTeX'; $name = $post_id . '-' . $comment_id . '-' . md5($texcode); RenderCache::put(array($group, $name), $file, 60 * 60 * 24 * 7, true); }