Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* INFO **********************************************************************
- *
- * ~ Divergenzmeter generator from Steins;Gate ~
- *
- * Requires GD for graphic operations
- * Every access and the generated number is logged,
- * there is also a counter that's being increased.
- *
- * Copyright (c) by FichteFoll, 2011-09-06
- *
- *****************************************************************************/
- /* PARAMETERS [$_REQUEST] ****************************************************
- *
- * @int width Used as maximum width
- * @int height Used as maximum height
- * @float num Number to show, generated otherwise
- * @int precision = $precision Amount of digits after the point
- * @bool small [0|1] = $default_small _small images ar half-sized
- * @bool unprop [0|1] = 0 Allows unproportinal resizing (no upscales though)
- *
- *****************************************************************************/
- /* LICENCE *******************************************************************
- *
- * Acutally, no licence required here. You may use this as you please.
- * It's also not required to name me when you use it or modify,
- * though you're still allowed to.
- *
- *****************************************************************************/
- /* CHANGELOG *****************************************************************
- *
- * 2011-09-06
- * + Added: "num" parameter
- * + Added: Decent algorhythm to generate a digit that exceeds 1
- * * Fixed: unprop and resizing did not work as expected when ?height=100&unprop=1
- *
- * 2011-08-25
- * ~ Releasable state
- *
- *****************************************************************************/
- /* Some Notes:
- *
- * I don't use much error handling in this. Logging may be turned off
- *
- */
- $appname = 'divmet';
- // some variables you might want to change
- $source = 'source/';
- $precision = 6;
- $default_small = 0;
- $log = "$appname.log";
- $log_enable = true;
- $counter = "$appname.counter.txt";
- // Parse input from $_REQUEST
- $small = !!$_REQUEST['small'] or $default_small;
- $precision = is_numeric($_REQUEST['precision']) ? $_REQUEST['precision'] : $precision;
- $num = is_numeric($_REQUEST['num']) ? (float) $_REQUEST['num'] : -1;
- $maxwidth = is_numeric($_REQUEST['width']) ? $_REQUEST['width'] : 0;
- $maxheight = is_numeric($_REQUEST['height']) ? $_REQUEST['height'] : 0;
- $prop = !$_REQUEST['unprop'];
- $suffix = $small ? '_small' : '';
- // logging
- $IP = $_SERVER['REMOTE_ADDR'] or '';
- function writeLog($type, $message = null)
- {
- global $log, $IP, $log_enable;
- if (!$log_enable) return ;
- if ($message == null or strlen($type) != 1)
- {
- $message = $type;
- $type = " ";
- }
- $timestamp = date("Y-m-d H:i:s");
- $logstr = $type.' '.$timestamp.' '.$IP.' '.trim($message)."\n";
- return file_put_contents($log, $logstr, FILE_APPEND);
- }
- function incCounter()
- {
- global $counter;
- if (!$f_counter = fopen($counter, (file_exists($counter) ? 'r+' : 'w+')))
- return "";
- $count = (int) fgets($f_counter);
- if ($count == "")
- $count = 0;
- rewind($f_counter);
- fwrite($f_counter, ++$count);
- fclose($f_counter);
- }
- // action starts here /////////////////////////////////////////////////////////
- // calculate acutal number if not -1
- if ($num == -1)
- {
- $num = fmt_rand(0, 1.1);
- if ($num > 1)
- $num = 1 + fmt_rand(0, 0.8);
- }
- $numstr = sprintf("%.{$precision}f", $num = round($num, $precision));
- // create array for keeping opend handles
- $img_array = array();
- // retrieve size of point to calculate size since it's used anyway
- $img_array['.'] = imagecreatefrompng("{$source}Point{$suffix}.png")
- or die("Failed to determine dimensions");
- $sw = imagesx($img_array['.']);
- $sh = imagesy($img_array['.']);
- $height = $sh;
- $width = $sw * strlen($numstr);
- // create output image
- $img = imagecreatetruecolor($width, $height)
- or die("Failed to create image");
- // no transparency needed as these images are not transparent, too
- //~ $trans_colour = imagecolorallocatealpha($img, 0, 0, 0, 127);
- //~ imagefill($img, 0, 0, $trans_colour);
- // fill output image
- for ($i = 0; $i < strlen($numstr); ++$i)
- {
- $sub = substr($numstr, $i, 1);
- if (!$img_array[$sub] and !($img_array[$sub] = imagecreatefrompng("{$source}{$sub}{$suffix}.png")))
- die("failed to load image for '$sub'");
- imagecopy($img, $img_array[$sub], $i * $sw, 0, 0, 0, $sw, $sh);
- }
- // resize (no upscale)
- if ($maxwidth or $maxheight)
- {
- if ($newimg = imageresample($img, $maxwidth, $maxheight, $width, $height, $prop))
- {
- imagedestroy($img);
- $img =& $newimg;
- }
- }
- // do logging
- writeLog("$numstr | " . ($newimg ? "Resized to: {$maxwidth}x{$maxheight}" : "{$width}x{$height}"));
- incCounter();
- // output
- header("Content-type: image/png");
- imagepng($img);
- imagedestroy($img);
- foreach ($img_array as $t_img)
- imagedestroy($t_img);
- // functions //////////////////////////////////////////////////////////////////
- function fmt_rand($min = 0, $max = 1) // "f" for "float"
- {
- return $min + ($max-$min) * mt_rand()/mt_getrandmax();
- }
- function imageresample($image, &$width, &$height, $width_orig = 0, $height_orig = 0, $prop = true)
- {
- def($width_orig, imagesx($image));
- def($height_orig, imagesy($image));
- if (!$prop)
- {
- def($width, $width_orig);
- def($heigth, $heigth_orig);
- }
- else
- {
- // calculate dimensions
- $ratio = min($width ? $width/$width_orig : 1, $height ? $height/$height_orig : 1);
- $height = (int) ($height_orig * $ratio);
- $width = (int) ($width_orig * $ratio);
- }
- // no upscale
- if ($width > $width_orig or $height > $height_orig) return null;
- // create and copy image
- if (!$image_out = imagecreatetruecolor($width, $height))
- return null;
- return imagecopyresampled($image_out, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig)
- ? $image_out : null;
- }
- function def(&$ref, $alt)
- {
- $ref = ($ref == 0) ? $alt : $ref or $alt;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement