<?php
  
if ($_SERVER['QUERY_STRING'] == "source") die( highlight_file($_SERVER['SCRIPT_FILENAME'], true));

  
// default values
  
$diameter '19';
  
$width '1440';
  
$height '900';
  
$wratio '16';
  
$hratio '10';

  if (isset(
$_GET['submit'])) {
    
$_GET preg_replace('/,/','.',$_GET);

    if (
is_numeric($_GET['diameter'])) {
      
$diameter $_GET['diameter'];
    } else {
      
$message 'Diameter is not a number';
    }
    if (
is_numeric($_GET['width'])) {
      
$width $_GET['width'];
    } else {
      
$message 'Width is not a number';
    }
    if (
is_numeric($_GET['height'])) {
      
$height $_GET['height'];
    } else {
      
$message 'Height is not a number';
    }
    if (
$_GET['autoratio'] != "on") {
      if (
is_numeric($_GET['wratio'])) {
        
$wratio $_GET['wratio'];
      } else {
        
$message 'Width ratio is not a number';
      }
      if (
is_numeric($_GET['hratio'])) {
        
$hratio $_GET['hratio'];
      } else {
        
$message 'Height ratio is not a number';
      }
    } else {
      if (!isset(
$message)) {
        if ((
$width/4)*== $height) {
          
$wratio 4;
          
$hratio 3;
        } else if ((
$width/5)*== $height) {
          
$wratio 5;
          
$hratio 4;
        } else if ((
$width/16)*== $height) {
          
$wratio 16;
          
$hratio 9;
        } else if ((
$width/16)*10 == $height) {
          
$wratio 16;
          
$hratio 10;
        } else if ((
$width/21)*== $height) {
          
$wratio 21;
          
$hratio 9;
        } else if ((
$width/3)*== $height) {
          
$wratio 3;
          
$hratio 2;
        } else {
          
$message 'Autodetect ratio failed';
        }
      }
    }
  }

  if (!isset(
$message)) {
    
$cvalue sqrt($wratio*$wratio $hratio*$hratio); // pythagoras
    
$swidth = ($diameter/$cvalue) * $wratio;
    
$sheight = ($diameter/$cvalue) * $hratio;

    
$wdpi $width $swidth;
    
$hdpi $height $sheight;
  }
?>
<html>
  <head>
    <title>Calculate screen DPI</title>
    <style>
      input.ibox { width: 50px; }
    </style>
  </head>
  <body>
    <h1>Calculate screen DPI</h1>
    <form method="get" action='<?php echo $_SERVER['SCRIPT_NAME']?>'>
      <table>
        <tr>
          <td>Screendiameter in inch</td>
          <td><input type='text' class='ibox' name='diameter' value='<?php echo $diameter?>'> inch</td>
        </tr>
        <tr>
          <td>Screenratio</td>
          <td>
            <input type='text' class='ibox' name='wratio' value='<?php echo $wratio?>'>:<input class='ibox' type='text' name='hratio' value='<?php echo $hratio?>'>
            &nbsp;<input type='checkbox' class='ibox' name='autoratio' <?php if($_GET['autoratio'] == "on") echo "checked";?>>Autodetect
          </td>
        </tr>
        <tr>
          <td>Screenresolution</td>
          <td>
            <table>
              <tr><td>Width</td><td><input class='ibox' type='text' name='width' value='<?php echo $width?>'> pixels</td></tr>
              <tr><td>Height</td><td><input class='ibox' type='text' name='height' value='<?php echo $height?>'> pixels</td></tr>
            </table>
          </td>
        </tr>
        <tr>
          <th colspan='2'><input type='submit' name='submit' value='Calculate'></input></th>
        </tr>
      </table>
    </form>
<?php
  
if (isset($message)) {
    echo 
"<hr>$message";
  } else if (isset(
$_GET['submit'])) {
?>
  <hr>
  <h2>Calculations for a <?php echo $diameter?><?php echo $wratio?>:<?php echo $hratio?> screen of <?php echo $width?>x<?php echo $height?>:</h2>
  Screen width: <?php echo round($swidth,1)?> inch or <?php echo round($swidth*25.4,1)?> millimeter.<br>
  Screen height: <?php echo round($sheight,1)?> inch or <?php echo round($sheight*25.4,1)?> millimeter.<br>
  Screen surface: <?php echo round($sheight*$swidth,1)?> square inch or <?php echo round(($sheight*2.54)*($swidth*2.54),1)?> square centimeter.<br>
  <br>
  Horizontal DPI: <?php echo round($wdpi,1)?><br>
  Vertical DPI: <?php echo round($hdpi,1)?><br>
  <br>
  Please note that there values are approximate, only the manufacturer can give the exact specifications.<br><br>
<?php
  
}
?>
    <a href="?source">Sourcecode</a>
  </body>
</html>