<?php
define( "IMG_DIR", "./mathimg/" );
define( "IMG_EXT", ".png" );
$math = '(\frac{mv^2}{r}=\frac{e^2}{4\pi\varepsilon_0 r^2}';
$imgfile = md5( $math ).IMG_EXT;
print($imgfile);
if ( !file_exists( IMG_DIR.$imgfile ) ) {
    $tmpfile = IMG_DIR."tmp.tex";
       print($tmpfile);
       $tmp = fopen( $tmpfile, "w" );
       fputs( $tmp, '\documentclass[12pt]{jarticle}'."\n" );
       fputs( $tmp, '\usepackage{amsmath,amssymb,bm}'."\n" );
       fputs( $tmp, '\pagestyle{empty}'."\n" );
       fputs( $tmp, '\begin{document}'."\n" );
       fputs( $tmp, '\thispagestyle{empty}'."\n" );
       fputs( $tmp, '\large\['.$math.'\]'."\n" );
       fputs( $tmp, '\end{document}'."\n" );
       fclose( $tmp );
       chdir( IMG_DIR );
       exec( "platex -halt-on-error tmp.tex" );
       exec( "dvipng -bg 'rgb 1.0 1.0 1.0' -bd 1000 -T tight --gamma 1.1 -o tmp.png tmp.dvi" );
       exec( "mv tmp.png ".$imgfile );
}
?>
