Advertisement
FichteFoll

php - Steins;Gate Divergenzmeter

Sep 6th, 2011
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.10 KB | None | 0 0
  1. <?php
  2. /* INFO **********************************************************************
  3.  *
  4.  *                ~ Divergenzmeter generator from Steins;Gate ~
  5.  *
  6.  *  Requires GD for graphic operations
  7.  *  Every access and the generated number is logged,
  8.  *  there is also a counter that's being increased.
  9.  *
  10.  *  Copyright (c) by FichteFoll, 2011-09-06
  11.  *
  12.  *****************************************************************************/
  13.  
  14. /* PARAMETERS [$_REQUEST] ****************************************************
  15.  *
  16.  *  @int    width                           Used as maximum width
  17.  *  @int    height                          Used as maximum height
  18.  *  @float  num                             Number to show, generated otherwise
  19.  *  @int    precision    = $precision       Amount of digits after the point
  20.  *  @bool   small  [0|1] = $default_small   _small images ar half-sized
  21.  *  @bool   unprop [0|1] = 0                Allows unproportinal resizing (no upscales though)
  22.  *
  23.  *****************************************************************************/
  24.  
  25. /* LICENCE *******************************************************************
  26.  *
  27.  *  Acutally, no licence required here. You may use this as you please.
  28.  *  It's also not required to name me when you use it or modify,
  29.  *  though you're still allowed to.
  30.  *
  31.  *****************************************************************************/
  32.  
  33.  /* CHANGELOG *****************************************************************
  34.  *
  35.  *      2011-09-06
  36.  *          + Added: "num" parameter
  37.  *          + Added: Decent algorhythm to generate a digit that exceeds 1
  38.  *          * Fixed: unprop and resizing did not work as expected when ?height=100&unprop=1
  39.  *
  40.  *      2011-08-25
  41.  *          ~ Releasable state
  42.  *
  43.  *****************************************************************************/
  44.  
  45.  
  46. /* Some Notes:
  47.  *
  48.  *  I don't use much error handling in this. Logging may be turned off
  49.  *
  50.  */
  51.  
  52.     $appname = 'divmet';
  53.  
  54.     // some variables you might want to change
  55.     $source = 'source/';
  56.     $precision = 6;
  57.     $default_small = 0;
  58.     $log = "$appname.log";
  59.     $log_enable = true;
  60.     $counter = "$appname.counter.txt";
  61.  
  62.     // Parse input from $_REQUEST
  63.     $small = !!$_REQUEST['small'] or $default_small;
  64.     $precision = is_numeric($_REQUEST['precision']) ? $_REQUEST['precision'] : $precision;
  65.     $num = is_numeric($_REQUEST['num']) ? (float) $_REQUEST['num'] : -1;
  66.     $maxwidth = is_numeric($_REQUEST['width']) ? $_REQUEST['width'] : 0;
  67.     $maxheight = is_numeric($_REQUEST['height']) ? $_REQUEST['height'] : 0;
  68.     $prop = !$_REQUEST['unprop'];
  69.     $suffix = $small ? '_small' : '';
  70.  
  71.     // logging
  72.     $IP = $_SERVER['REMOTE_ADDR'] or '';
  73.  
  74.     function writeLog($type, $message = null)
  75.     {
  76.         global $log, $IP, $log_enable;
  77.         if (!$log_enable) return ;
  78.         if ($message == null or strlen($type) != 1)
  79.         {
  80.             $message = $type;
  81.             $type = " ";
  82.         }
  83.         $timestamp = date("Y-m-d H:i:s");
  84.         $logstr = $type.' '.$timestamp.' '.$IP.' '.trim($message)."\n";
  85.         return file_put_contents($log, $logstr, FILE_APPEND);
  86.     }
  87.  
  88.     function incCounter()
  89.     {
  90.         global $counter;
  91.         if (!$f_counter = fopen($counter, (file_exists($counter) ? 'r+' : 'w+')))
  92.             return "";
  93.         $count = (int) fgets($f_counter);
  94.         if ($count == "")
  95.             $count = 0;
  96.         rewind($f_counter);
  97.         fwrite($f_counter, ++$count);
  98.         fclose($f_counter);
  99.     }
  100.  
  101. // action starts here /////////////////////////////////////////////////////////
  102.  
  103.     // calculate acutal number if not -1
  104.     if ($num == -1)
  105.     {
  106.         $num = fmt_rand(0, 1.1);
  107.         if ($num > 1)
  108.             $num = 1 +  fmt_rand(0, 0.8);
  109.     }
  110.     $numstr = sprintf("%.{$precision}f", $num = round($num, $precision));
  111.  
  112.     // create array for keeping opend handles
  113.     $img_array = array();
  114.  
  115.     // retrieve size of point to calculate size since it's used anyway
  116.     $img_array['.'] = imagecreatefrompng("{$source}Point{$suffix}.png")
  117.                       or die("Failed to determine dimensions");
  118.     $sw = imagesx($img_array['.']);
  119.     $sh = imagesy($img_array['.']);
  120.     $height = $sh;
  121.     $width  = $sw * strlen($numstr);
  122.  
  123.  
  124.     // create output image
  125.     $img = imagecreatetruecolor($width, $height)
  126.            or die("Failed to create image");
  127.     // no transparency needed as these images are not transparent, too
  128.     //~ $trans_colour = imagecolorallocatealpha($img, 0, 0, 0, 127);
  129.     //~ imagefill($img, 0, 0, $trans_colour);
  130.  
  131.     // fill output image
  132.     for ($i = 0; $i < strlen($numstr); ++$i)
  133.     {
  134.         $sub = substr($numstr, $i, 1);
  135.         if (!$img_array[$sub] and !($img_array[$sub] = imagecreatefrompng("{$source}{$sub}{$suffix}.png")))
  136.             die("failed to load image for '$sub'");
  137.         imagecopy($img, $img_array[$sub], $i * $sw, 0, 0, 0, $sw, $sh);
  138.     }
  139.  
  140.     // resize (no upscale)
  141.     if ($maxwidth or $maxheight)
  142.     {
  143.         if ($newimg = imageresample($img, $maxwidth, $maxheight, $width, $height, $prop))
  144.         {
  145.             imagedestroy($img);
  146.             $img =& $newimg;
  147.         }
  148.     }
  149.  
  150.     // do logging
  151.     writeLog("$numstr | " . ($newimg ? "Resized to: {$maxwidth}x{$maxheight}" : "{$width}x{$height}"));
  152.     incCounter();
  153.  
  154.  
  155.     // output
  156.     header("Content-type: image/png");
  157.     imagepng($img);
  158.     imagedestroy($img);
  159.     foreach ($img_array as $t_img)
  160.         imagedestroy($t_img);
  161.  
  162.  
  163. // functions //////////////////////////////////////////////////////////////////
  164.     function fmt_rand($min = 0, $max = 1) // "f" for "float"
  165.     {
  166.         return $min + ($max-$min) * mt_rand()/mt_getrandmax();
  167.     }
  168.  
  169.     function imageresample($image, &$width, &$height, $width_orig = 0, $height_orig = 0, $prop = true)
  170.     {
  171.         def($width_orig, imagesx($image));
  172.         def($height_orig, imagesy($image));
  173.         if (!$prop)
  174.         {
  175.             def($width, $width_orig);
  176.             def($heigth, $heigth_orig);
  177.         }
  178.         else
  179.         {
  180.             // calculate dimensions
  181.             $ratio = min($width ? $width/$width_orig : 1, $height ? $height/$height_orig : 1);
  182.             $height = (int) ($height_orig * $ratio);
  183.             $width = (int) ($width_orig * $ratio);
  184.         }
  185.         // no upscale
  186.         if ($width > $width_orig or $height > $height_orig) return null;
  187.  
  188.         // create and copy image
  189.         if (!$image_out = imagecreatetruecolor($width, $height))
  190.             return null;
  191.         return imagecopyresampled($image_out, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig)
  192.                 ? $image_out : null;
  193.     }
  194.  
  195.     function  def(&$ref, $alt)
  196.     {
  197.         $ref = ($ref == 0) ? $alt : $ref or $alt;
  198.     }
  199. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement