<?
// This is the PHP script of DOOM
// It prolly won't work.
// Written by NFG ( http://nfgworld.com ) in February 2009.
// While I take credit for making the thing assemble fonts
// Which I'm mad proud of, 'cause I don't code...
// Many kudos to Twyst for helping with some tricky stuff:
// PHPBB URLs
// maintenance-friendly filenames.
// colour shifting
/* ------------------------------------------------ */
/* License */
/* ------------------------------------------------ */
/* This script is released under a Creative Commons Zero license.
This means it's free of just about all encumbrances.
Do what you want with it.
A tip of the hat for ol' NFG would be nice, but not required. =)
http://creativecommons.org/licenses/zero/1.0/
/* ------------------------------------------------ */
/* Version History */
/* ------------------------------------------------ */
/*
1.0 - First Public Release
1.1 - Added Font List & Font Counter, fixed some bugs that only became apparent with the lister...
Includes word-balloon addition by Twystneko.
1.2 - Added random font option. This required some internal changes, but no functional differences.
/* ------------------------------------------------ */
/* Instructional Block */
/* ------------------------------------------------ */
/* HOW TO USE THIS SCRIPT
1. Requirements
PHP
GD graphics library (almost always included with PHP)
A website (duh)
This script (also duh)
Some fonts (slightly less, but still duh)
2. Usage
. First, decide if you'll use PHPBB-friendly parameters or not.
I suggest leaving this on by default, and just get used to the way it works.
. The output of this script is a .PNG image.
To link to it, or use it in a webpage, just put the script URL and
parameters between some [img] or <img> tags. It's not tricky.
Examples
. Webpage: <img src="http://server/path-to-script/arcade.php/y-font/x-phrase to encode" />
. Forum: [img]http://server/path-to-script/arcade.php/y-font/x-phrase to encode[/img]
3. Things to watch for
. Some forums don't like spaces in the names. Use the HTML-friendly space: %20 instead. so, /x-phrase%20to%20encode
. Many parameters are optional. The minimum is font (y) and phrase (x).
4. Parameters
x - words or phrase to create image with
y - font to use. Use r for random (usage: /y-r/ )
z - colour or variation. A font file may contain several fonts stacked vertically.
This chooses how many rows down an image file to start. (starts at zero!)
use r for random (usage: /z-r/ )
h - height of character (default is 8 if unset)
w - width of character (default is 8 if unset)
Note that all characters must be the same height and width.
There is no facility for variable width fonts.
dbl - doublesize or not. If un-set, defaults to 1x, but you can set dbl to 2-6.
The script will reduce all values larger than six to six.
4x example: /dbl-4
cs - colour shift, to adjust the hue of the font.
Format is red.green.blue.
Example: /cs-0.128.64
list - list all fonts. Ignores the x-phrase and y-fontchoice, but obeys all other commands. (usage: /list-1/ )
count - changes x-phrase into the number of available fonts. Obeys all other commands. (usage: /count-1/ )
b - Bubble position. This activates the speech bubble mode, and is either "u" (up) or "d" (down).
bp - a percentage of where it is along the bubble.
5. Filenames
Your filenames MUST be in this format: shortcut-whateveryouwant.png
where: shortcut is the Y/Fontchoice value used in the URL.
where: whateveryouwant is the descriptive name of the font.
example: fz-FantasyZone.png the Y/URL code for this font is fz.
example: cotn-Cotton.png the Y/URL code for this one is cotn.
usage: arcade.php?y=cotn&x=This is the Cotton font!
*/
/* ------------------------------------------------ */
/* Configuration Block */
/* Edit these settings as desired */
/* ------------------------------------------------ */
// NOTE: Config options: 1 = YES, 0 = NO
// Many forums, including the popular phpBB, won't allow images with ?, = or & in the path.
// Do you want standard URLs arcade.php?param=value¶m2=value2
// Or PHPbb-friendly URLs? arcade.php/param-value/param2-value2/.png
$phpbbfriendly = 1;
// Cache previously-created images?
// Note that on a Windows system, there's no filename difference between upper and lower case, so
// cached phrases will no refresh if you change the capitalization later.
// For example, Windows thinks phrase.png is the same as Phrase.png and PHRASE.png and PhRaSe.png
$cacheornot = 0;
// Directory to store cached images:
// Should prolly be something like ./cache/ for most servers.
// Be sure to create this directory!
// And make it writable!!
$filepath = './cache/';
// Set the directory where you store the source fontfiles. Should be ./fonts/ for most servers.
$dir = './fonts/';
// where the components for the speech bubbles are
$bubblepath = "./bubble/";
// I suspect you'll want to stick to 8x8 characters, but if you get something bigger, specify the size here.
// Note: this can be overridden later with the H and V parameters. This just sets the default.
// Set default source character width in pixels
$charwidth = 8;
// Set default source character height in pixels
$charheight = 8;
// Character offset: It skips the first 32 ASCII chars, 'cause they're all control-characters that you'll never use.
// You should never need to change this.
$charoffset = 32;
// Set the maximum phrase length (default 100 chars:
$stringlimit = 100;
// Allow the creation of a long list of available fonts?
// When doing a font list, the spacing between fonts is 1 x fontsize, so 3x font = 3px gap.
$allowlist = 1;
/* ------------------------------------------------ */
/* You shouldn't need to muck about past here */
/* Commented for your enjoy! */
/* */
/* First, get the vars from the URL request */
/* ------------------------------------------------ */
if ($phpbbfriendly == 1) {
// This is the PHPbb-friendly version
$scriptname = $_SERVER['SCRIPT_NAME']."/";
$filename = str_replace($scriptname, '', $_SERVER['REQUEST_URI']);
foreach($bits as $bite) {
$out[$temp[0]] = $temp[1];
}
// String to create:
$string = $out["x"];
// Get the desired font file:
if ($out["y"] == "r") {
$randomfont = TRUE;
} else {
$fontchoice = $out["y"];
}
// Row offset (font colour in multiple-colour fonts)
if ($out["z"]) {
if ($out["z"] == "r") {
$randomfontcolour = TRUE;
} else {
$randomfontcolour = FALSE;
$charcolor = $out["z"];
}
}
// Check dbl: if set, it becomes the multiplier (up to 6x)
if ($out["dbl"]) {
$doublesize = $out["dbl"];
// There's no point setting the multiplier to ONE, so if set, change it to TWO
if ( $doublesize <= 1 ) {
$doublesize = 2;
}
}
// Check for height/width overrides. If H or V parameters exist, change the font size.
if ($out["w"]) {
$charwidth = $out["w"];
}
if ($out["h"]) {
$charheight = $out["h"];
}
if ($out["cs"]) {
$colorize = explode(".",$out["cs"]);
}
if($out["b"]) {
$b = $out['b'];
}
if($out["bp"]) {
$bp = $out['bp'];
} else {
$bp = 0;
}
if ($out["list"]) {
$listfonts = true;
} else {
$listfonts = false;
}
if ($out["count"]) {
$countfonts = true;
} else {
$countfonts = false;
}
} else {
// This is the normal version, using standard URL parameters
// String to create:
$string = $_GET["x"];
// Get the desired font file:
if ($_GET["y"] == "r") {
$randomfont = TRUE;
} else {
$fontchoice = $_GET["y"];
}
// Vertical offset (font colour in multiple-colour fonts)
if ($_GET["z"]) {
if ($_GET["z"] == "r") {
$randomfontcolour = TRUE;
} else {
$randomfontcolour = FALSE;
$charcolor = $out["z"];
}
}
// Check dbl: if set, it becomes the multiplier (up to 6x)
if ($_GET["dbl"]) {
$doublesize = $_GET["dbl"];
// There's no point setting the multiplier to ONE, so if set, change it to TWO
if ( $doublesize <= 1 ) {
$doublesize = 2;
}
}
// Check for height/width overrides. If H or V parameters exist, change the font size.
if ($_GET["w"]) {
$charwidth = $_GET["s"];
}
if ($_GET["h"]) {
$charheight = $_GET["h"];
}
if ($_GET["cs"]) {
$colorize = explode(".",$_GET["cs"]);
}
if($_GET["b"]) {
$b = $_GET['b'];
}
if($_GET["bp"]) {
$bp = $_GET['bp'];
} else {
$bp = 0;
}
if ($_GET["list"]) {
$listfonts = true;
} else {
$listfonts = false;
}
if ($_GET["count"]) {
$countfonts = true;
} else {
$countfonts = false;
}
}
if($b == "none") {
}
// We don't need any fonts larger than 6x, do we?
if ($doublesize > 6) {
$doublesize = 6;
}
/* ------------------------------------------------ */
/* Sanitize the string, for safety! */
/* ------------------------------------------------ */
// First, check for chars outside ASCII range 32-126 (20-7E in hex).
// Limit string to the number of chars specified in config:
$string = substr($string, 0
, $stringlimit);
/* ------------------------------------------------ */
/* Now, generate a filename and check the cache */
/* ------------------------------------------------ */
// Create the filename. Skip the entire cache procedure if we're listing or counting fonts.
if ((!$listfonts) or (!$countfonts)) {
$current = "./cache/".$fontchoice.$charcolor.$doublesize.$charheight.$charwidth.$stringpure.".png";
// Check the cache. If the file exists, spit it out and die:
$contentType = 'Content-type: image/png';
}
}
/* ------------------------------------------------ */
/* This is the new font Detect and Select! */
/* Look for a file which matches the $fontchoice */
/* ------------------------------------------------ */
// Thanks Twyst for this segment!
// Basically opens the $dir specified in the config block
// then loops through every file looking for one that matches the Y/Fontchoice value
// New in 1.1 - remembers the number of fonts and the max font-name-length for the fontlist
$maxfontlength = "0"; // For counting the max chars in a font name (for calc'ing font list width)
$numfonts = "0"; // for counting the number of fonts
while (false !== ($file = readdir($handle))) {
if(count($temp) > 1) { // means it was split up
$fontnames[$numfonts] = $file; // An array of filenames, for later abuse.
if ($listfonts) { $fontfiles[$numfonts] = $dir.$file; }
if($fontchoice == $temp[0]) {
$fontfile = $dir.$file; // If the current file prefix matches the $fontchoice, specify the SOURCE image
}
$numfonts++; // increment the font count. Also used for incrementing $fontnames array.
}
}
}
/* ------------------------------------------------ */
/* Special-case adjustments */
/* For LIST, COUNT and similar things */
/* ------------------------------------------------ */
// Are we counting the fonts? Change the string to the number of fonts.
if ($countfonts) {
$string = $numfonts;
}
// If we're doing $randomfonts, change the font filename to be a random one:
if ($randomfont) {
$fontarraychoice = rand(0
,$numfonts);
$fontfile = $dir.$fontnames[$fontarraychoice];
}
// Is the font colour random? Check the z-depth and pick one.
if ($randomfontcolour) {
$zcount = $size[1] / $charheight;
$charcolor = rand(0
,$zcount);
}
/* ------------------------------------------------ */
/* This is the preparation section */
/* ------------------------------------------------ */
//The new image width should equal the # of chars x the width of each.
if ($listfonts) {
$newimgwidth = ($charwidth * $maxfontlength);
} else {
$newimgwidth = ($charwidth * strlen($string));
}
//The new image height should equal the # of fonts x the height of each char of each.
if ($listfonts) {
$newimgheight = (($charheight * $numfonts) + ($numfonts * 2));
} else {
$newimgheight = $charheight;
}
/* ------------------------------------------------ */
/* Start building the new PNG image */
/* ------------------------------------------------ */
// Create the image (width, height)
// Set up the transparent background:
/* ------------------------------------------------ */
/* Now the character cut/paste loop */
/* ------------------------------------------------ */
// NEW for v1.1 - loops through the vertical to accomodate the list.
// Begin vertical loop. Number of fonts determines number of loops.
// $fontnames is the array
// If we're counting fonts, echo only the count value, replacing any user string in the URL
$vertloop = 0; // set vertical loop counter to zero
foreach ($fontnames as $currentfont) {
if ($listfonts) {
$fontbits = explode("-",$currentfont,2
);
$string = substr($fontbits[1
],0
,-4
); // If this is a font list, the current string should be the font name
$currentVpos = (($charheight + 2) * $vertloop);
} else {
}
echo $fontfiles[$numfonts];
// Set horizontal loop char-counter to zero
$charcount = 0;
// Start Loop, repeat for each character in the string:
// returns ASCII number
// Set NEWIMG current cursor H position
$currentHpos = ($charwidth * $charcount);
// H-position for grabbing char from SRC image (width x ASCII number + offset)
$srcimgcharpos = ($ASCIIno - $charoffset) * $charwidth;
// Copy the letter from font image to new image
imagecopy($newimg, $srcimg, $currentHpos, $currentVpos, $srcimgcharpos, $charcolor * $charheight, $charwidth, $charheight);
// Move to next char in string
$charcount++;
}
$vertloop++; // Increment the vertical counter
}
/* ------------------------------------------------ */
/* Colour Shift If Desired */
/* ------------------------------------------------ */
if($colorize) {
imagefilter($newimg, IMG_FILTER_COLORIZE
, $colorize[0
],$colorize[1
],$colorize[2
]);
}
/* ------------------------------------------------ */
/* Two save-routines: big and x size */
/* ------------------------------------------------ */
// Filename is the $string:
$filename = $fontchoice.$charcolor.$doublesize.$charheight.$charwidth.$stringpure.".png";
if($b) {
if($b == "d") {
// the pointer is down.
$dir = "bot";
} else {
$dir = "top";
}
if($bp <= 40) {
$s = "l";
} elseif($bp >= 60) {
$s = "r";
} else {
$s = "c";
}
$pname = $s."-".$dir.".png";
// got the pointer.
// now build step 1 - the background
imagecopy($step1,$newimg,2
,4
,0
,0
,$width,$height);
$heightb= $height+8;
imagecopy($step2,$step1,4
,0
,0
,0
,$widthb,$heightb);
if($b == "u") {
$voffset = 8;
$poffset = 0;
} else {
$voffset = 0;
}
$xpos = floor($width * ($bp/100
)) + 4;
$newimg = $step3;
}
if ($doublesize > 1) {
// Create NewNewImg
// Set up the transparent background:
// Double the size from old image to new image:
imagecopyresized($newnewimg, $newimg, 0
, 0
, 0
, 0
, $newimgwidth * $doublesize, $newimgheight * $doublesize, $newimgwidth, $newimgheight);
// Output and free from memory:
header('Content-Type: image/png');
// If cache is set to yes, save the file:
if ($cacheornot == 1) {
imagepng($newnewimg, $filepath.$filename);
}
} else {
// Output and free from memory:
header('Content-Type: image/png');
// If cache is set to yes, save the file:
if ($cacheornot == 1) {
}
}
?>