home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / biorhythm.php < prev    next >
Encoding:
PHP Script  |  2003-11-22  |  6.3 KB  |  214 lines

  1. <?php
  2.     include("lang/".file_get_contents("lang.tmp").".php"); 
  3.  
  4.     // Biorhythm by Till Gerken
  5.     // http://www.zend.com/zend/tut/dynamic.php
  6.     //
  7.     // Multi-language support by Kai Oswald Seidler, 2003
  8.     //
  9.     // Print a standard page header
  10.     //
  11.  
  12.  
  13.  
  14.  
  15.     function pageHeader()
  16.     {
  17.         global $TEXT;
  18.  
  19.         print("<html><head>");
  20.         print("<title>Biorhythm with PHP</title>");
  21.         print('<link href="xampp.css" rel="stylesheet" type="text/css">');
  22.         print("</head><body>");
  23.         
  24.         print(" <p><h1>".$TEXT['bio-head']."</h1>");
  25.         print($TEXT['bio-by']." Till Gerken<br><a class=black href=http://www.zend.com/zend/tut/dynamic.php>http://www.zend.com/zend/tut/dynamic.php</a><p>");
  26.  
  27.     }
  28.  
  29.     //
  30.     // Print a standard page footer
  31.     //
  32.     function pageFooter()
  33.     {
  34.  
  35.         print("</body></html>");
  36.  
  37.     }
  38.  
  39.     //
  40.     // Function to draw a curve of the biorythm
  41.     // Parameters are the day number for which to draw,
  42.     // period of the specific curve and its color
  43.     //
  44.     function drawRhythm($daysAlive, $period, $color)
  45.     {
  46.         global $daysToShow, $image, $diagramWidth, $diagramHeight;
  47.  
  48.         // get day on which to center
  49.         $centerDay = $daysAlive - ($daysToShow / 2);
  50.  
  51.         // calculate diagram parameters
  52.         $plotScale = ($diagramHeight - 25) / 2;
  53.         $plotCenter = ($diagramHeight - 25) / 2;
  54.  
  55.         // draw the curve
  56.         for($x = 0; $x <= $daysToShow; $x++)
  57.         {
  58.         // calculate phase of curve at this day, then Y value
  59.         // within diagram
  60.         $phase = (($centerDay + $x) % $period) / $period * 2 * pi();
  61.         $y = 1 - sin($phase) * (float)$plotScale + (float)$plotCenter;
  62.  
  63.         // draw line from last point to current point
  64.         if($x > 0)
  65.             imageLine($image, $oldX, $oldY,
  66.                   $x * $diagramWidth / $daysToShow, $y, $color);
  67.  
  68.         // save current X/Y coordinates as start point for next line
  69.         $oldX = $x * $diagramWidth / $daysToShow;
  70.         $oldY = $y;
  71.         }
  72.  
  73.     }
  74.  
  75.     //
  76.     // ---- MAIN PROGRAM START ----
  77.  
  78.     // check if we already have a date to work with,
  79.     // if not display a form for the user to enter one
  80.     if(!isset($birthdate))
  81.     {
  82.         pageHeader();
  83.  
  84.         ?>
  85.          <form method="get" action="<?php print(basename($PHP_SELF)); ?>">
  86.          <!-- Please enter your birthday: -->
  87.          <?=$TEXT['bio-ask']?>:
  88.         <br>
  89.          <input type="text" name="birthdate" value="MM/DD/YYYY"><p>
  90.          <input type=submit value="<?=$TEXT['bio-ok']?>">
  91.          <input type="hidden" name="showpng" value=1>
  92.          </form>
  93.     
  94.     <? if ($source=="in")
  95.         { include("code.php"); $beispiel = $SCRIPT_FILENAME; pagecode($beispiel);} 
  96.         else
  97.         { print("<p><br><br><h2><U><a href=\"$PHP_SELF?source=in\">".$TEXT['srccode-in']."</a></U></h2>");} ?>
  98.  
  99.         <?php
  100.  
  101.         pageFooter();
  102.  
  103.         exit();
  104.     }
  105.  
  106.     // get different parts of the date
  107.     $birthMonth = substr($birthdate, 0, 2);
  108.     $birthDay = substr($birthdate, 3, 2);
  109.     $birthYear = substr($birthdate, 6, 4);
  110.  
  111.     // check date for validity, display error message if invalid
  112.     if(!checkDate($birthMonth, $birthDay, $birthYear))
  113.     {
  114.         pageHeader();
  115.  
  116.         //print("The date '$birthMonth/$birthDay/$birthYear' is invalid.");
  117.         print("<h2>".$TEXT['bio-error1']." '$birthMonth/$birthDay/$birthYear' ".$TEXT['bio-error2'].".</h2>");
  118.  
  119.         pageFooter();
  120.  
  121.         exit();
  122.     }
  123.  
  124.     if($showpng==1)
  125.     {
  126.         pageHeader();
  127.     
  128.         echo "<img src=".basename($PHP_SELF)."?birthdate=".urlencode($birthdate).">";
  129.         pageFooter();
  130.         exit();
  131.     }
  132.     // specify diagram parameters (these are global)
  133.     $diagramWidth = 710;
  134.     $diagramHeight = 400;
  135.     $daysToShow = 30;
  136.  
  137.     // calculate the number of days this person is alive
  138.     // this works because Julian dates specify an absolute number
  139.     // of days -> the difference between Julian birthday and
  140.     // "Julian today" gives the number of days alive
  141.     $daysGone = abs(gregorianToJD($birthMonth, $birthDay, $birthYear)
  142.             - gregorianToJD(date( "m"), date( "d"), date( "Y")));
  143.  
  144.     // create image
  145.     $image = imageCreate($diagramWidth, $diagramHeight);
  146.  
  147.     // allocate all required colors
  148.     $colorBackgr       = imageColorAllocate($image, 192, 192, 192);
  149.     $colorForegr       = imageColorAllocate($image, 255, 255, 255);
  150.     $colorGrid         = imageColorAllocate($image, 0, 0, 0);
  151.     $colorCross        = imageColorAllocate($image, 0, 0, 0);
  152.     $colorPhysical     = imageColorAllocate($image, 0, 0, 255);
  153.     $colorEmotional    = imageColorAllocate($image, 255, 0, 0);
  154.     $colorIntellectual = imageColorAllocate($image, 0, 255, 0);
  155.  
  156.     // clear the image with the background color
  157.     imageFilledRectangle($image, 0, 0, $width - 1, $height - 1, $colorBackgr);
  158.  
  159.     // calculate start date for diagram and start drawing
  160.     $nrSecondsPerDay = 60 * 60 * 24;
  161.     $diagramDate = time() - ($daysToShow / 2 * $nrSecondsPerDay)
  162.                + $nrSecondsPerDay;
  163.  
  164.     for ($i = 1; $i < $daysToShow; $i++)
  165.     {
  166.         $thisDate = getDate($diagramDate);
  167.         $xCoord = ($diagramWidth / $daysToShow) * $i;
  168.  
  169.         // draw day mark and day number
  170.         imageLine($image, $xCoord, $diagramHeight - 25, $xCoord,
  171.               $diagramHeight - 20, $colorGrid);
  172.         imageString($image, 3, $xCoord - 5, $diagramHeight - 16,
  173.             $thisDate[ "mday"], $colorGrid);
  174.  
  175.         $diagramDate += $nrSecondsPerDay;
  176.     }
  177.  
  178.     // draw rectangle around diagram (marks its boundaries)
  179.     imageRectangle($image, 0, 0, $diagramWidth - 1, $diagramHeight - 20,
  180.                $colorGrid);
  181.  
  182.     // draw middle cross
  183.     imageLine($image, 0, ($diagramHeight - 20) / 2, $diagramWidth,
  184.           ($diagramHeight - 20) / 2, $colorCross);
  185.     imageLine($image, $diagramWidth / 2, 0, $diagramWidth / 2, $diagramHeight - 20,
  186.           $colorCross);
  187.  
  188.     // print descriptive text into the diagram
  189.     imageString($image, 3, 10, 10,  $TEXT['bio-birthday'].": $birthDay.$birthMonth.$birthYear",
  190.             $colorCross);
  191.     imageString($image, 3, 10, 26,  $TEXT['bio-today'].":    ".  date(  "d.m.Y"),  $colorCross);
  192.     imageString($image, 3, 10, $diagramHeight - 42,  $TEXT['bio-physical'], $colorPhysical);
  193.     imageString($image, 3, 10, $diagramHeight - 58,  $TEXT['bio-emotional'], $colorEmotional);
  194.     imageString($image, 3, 10, $diagramHeight - 74,  $TEXT['bio-intellectual'], $colorIntellectual);
  195.  
  196.     // now draw each curve with its appropriate parameters
  197.     drawRhythm($daysGone, 23, $colorPhysical);
  198.     drawRhythm($daysGone, 28, $colorEmotional);
  199.     drawRhythm($daysGone, 33, $colorIntellectual);
  200.  
  201.     // set the content type
  202.     header("Content-type:  image/png");
  203.  
  204.     // create an interlaced image for better loading in the browser
  205.     imageInterlace($image, 1);
  206.  
  207.     // mark background color as being transparent
  208.     imageColorTransparent($image, $colorBackgr);
  209.  
  210.     // now send the picture to the client (this outputs all image data directly)
  211.     imagePNG($image);
  212.  
  213. ?>
  214.