source view: latex.php


<?php



$latex        = null;
$latex_result = null;

$dpi = '';
$tex = '';

if ( isset( $_POST['tex'] ) )
{
  require_once( './classes/LaTeX.php' );

  $latex = LaTeX::getByTeXAndDPI( $_POST['tex'], $_POST['dpi'] );
  if ( !$latex->isLoaded() )
  {
    $latex_result = $latex->fetchAndStore();
  }

  $tex = $latex->getTeX();
  $dpi = $latex->getDPI();

  if ( isset( $_GET['xml'] ) )
  {
    header( 'Content-Type: text/xml' );
    print( '<?xml version="1.0"?>' . chr( 13 ) );
    $latex_result->toXML();
    exit;
  }
  else if  ( isset( $_GET['json'] ) )
  {
    header( 'Content-Type: application/json' );
    $latex_result->toJSON();
    exit;
  }
}
else
{
  require_once( './classes/DB.php' );
  $random_formula_query = DB::getDB()->prepare( 'SELECT latexFormula, dpi FROM tblBlogFormulas ORDER BY RAND() LIMIT 1' );
  $random_formula_query->bind_result( $tex, $dpi );
  $random_formula_query->execute();
  $random_formula_query->fetch();
  $random_formula_query->close();
}



  //
  // the latex.load script defines a whole bunch of variables that we're using here.
  // basically, there was a huge chunk of code that got pulled to another file for
  // simpler opening. realistically, we should instantiate some sort of class and
  // access the variables that way, but for now this'll work.
  //
  // but note that it's terrible, terrible form (check the references to $dpi and $filename...).

  require_once( './classes/Admin.php' );
  require_once( './classes/Page.php' );
  $page = new Page();
  $page->setTitle( 'LaTeX to png' );
  $page->setParentURL( 'research.php' );



  $page->addHeader( <<<EOS
<style type="text/css">
span.plug
{
  display: block;
  margin:  1.5ex 0ex;
}
</style>
EOS
  );



  $page->printPageHeader();



//  include 'latex-load.php';
if ( !is_null( $latex ) && $latex->isLoaded() )
{
?>
<h3 style="margin-bottom:1ex;">png output</h3>
<p>
  <img alt="<?php print $_POST['tex'];?>" src="<?php print Admin::isDevServer() ? 'https://kylewoodward.com' : ''; ?>/<?php print $latex->getFilename(); ?>" title="<?php print htmlspecialchars( $tex ); ?> (@ <?php print $dpi; ?> DPI)" />
  <br />
  File at <a href="https://kylewoodward.com<?php print $latex->getFilename();?>">https://kylewoodward.com<?php print $latex->getFilename(); ?></a>
  <span class="plug">
  <span style="font-weight:500;">Want to see this output on a mask?</span> <a href="/latex-mask.php?id=<?php print $latex->getID(); ?>" target="new">Obviously</a>.</span>
</p>
<?php
}
  else if ( !is_null( $latex ) && $latex_result->hasError() )
  {
?>
<h3>LaTeX error</h3>
<p>
  <div class="code_block" style="height:8em;"><pre>
<?php print str_replace( '&#x0D;', '', htmlspecialchars( $latex_result->getErrorString() ) );?>
</pre></div>
<?php
}
else
{
?>
<h3>LaTeX input</h3>
<p style="font-style:italic;margin:2ex 0ex;padding:1ex;background-color:#eee;">
  <span style="font-weight:500;">Note 2020-07-24:</span> Sincere apologies, this site has been down for nearly two years. The error was in the PNG conversion, due to the discovery of a ghostscript security hole. Everything should be operating normally now.
</p>
<?php
}
?>
        <p>
          <form action="latex.php<?php print isset($_GET['xml'])?'?xml':'';?>" method="POST">
            <textarea id="tex" name="tex" rows="5" style="font-size:8pt;width:100%;" title="LaTeX source"><?php print str_replace('<','&lt;',$tex);?></textarea>
            DPI <input id="dpi" name="dpi" style="margin-left:1em;" type="text" value="<?php print $dpi;?>" />
            <input type="submit" value="Convert" />
          </form>
        </p>
        <p style="margin-top:1em;text-align:center;">
        </p>
        <h3>more information</h3>
        <p>
          After <a href="http://math.b3co.com/">math.b3co.com</a> went down, I needed a new source for my online LaTeX conversion. This page converts LaTeX fomulas to png &mdash; it automatically adds the necessary <tt>\begin{document}</tt> cruft &mdash;
          on a one-off basis; script access is available through POST.
          <ul style="list-style-type:square;margin-left:0ex;padding-left:4ex;">
            <li><tt>dpi=</tt> is the DPI of the result image; there is no default, but 150 is about right.</li>
            <li><tt>tex=</tt> is the LaTeX to convert</li>
          </ul>
        </p>
        <p>
          Optionally, you may pass <tt>?xml</tt> as a GET parameter, and the results will be returned as
        </p>
        <div class="code_block"><pre>&lt;?xml version="1.0"?&gt;
&lt;latexresult&gt;
  &lt;lateximage location="_url_" source="pngtex.618034.com" /&gt;
&lt;/latexresult&gt;
</pre></div>
        <p>
          This way, you can avoid parsing all the surrounding cruft. Images are maintained locally, so you don't need to copy them to your home server
          (however, as a nice script kiddie, that's a kind thing to do).
        </p>
<?php



  $page->printPageFooter();



?>