Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- // 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/
- /* ------------------------------------------------ */
- /* 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
- z - colour or variation. A font file may contain several fonts stacked vertically.
- This chooses how far down to start. (starts at zero!)
- h - height of character (default is 8)
- w - width of character (default is 8)
- 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
- 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/';
- // 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;
- /* ------------------------------------------------ */
- /* 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']);
- $filename = urldecode($filename);
- $bits = explode("/",$filename);
- $out = array();
- foreach($bits as $bite) {
- $temp = explode("-",$bite, 2);
- $out[$temp[0]] = $temp[1];
- }
- // String to create:
- $string = $out["x"];
- // Get the desired font file:
- $fontchoice = $out["y"];
- // Row offset (font colour in multiple-colour fonts)
- if ($out["z"]) {
- $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"]);
- }
- } else {
- // This is the normal version, using standard URL parameters
- // String to create:
- $string = $_GET["x"];
- // Get the desired font file:
- $fontchoice = $_GET["y"];
- // Vertical offset (font colour in multiple-colour fonts)
- if ($_GET["z"]) {
- $charcolor = $_GET["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"]);
- }
- }
- // 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).
- $string = preg_replace('/[^(\x20-\x7E)]*/','', $string);
- // 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:
- $stringpure = urlencode($string);
- $current = "./cache/".$fontchoice.$charcolor.$doublesize.$charheight.$charwidth.$stringpure.".png";
- // Check the cache. If the file exists, spit it out and die:
- if (file_exists($current)) {
- $contentType = 'Content-type: image/png';
- header ($contentType);
- readfile($current);
- die;
- }
- /* ------------------------------------------------ */
- /* This is the preparation section */
- /* ------------------------------------------------ */
- //The new image width should equal the # of chars x the width of each.
- $newimgwidth = ($charwidth * strlen($string));
- // Since the source charset starts with SPACE, add 32 to the ASCIIno to skip unwanted chars.
- $newimgheight = $charheight;
- /* ------------------------------------------------ */
- /* 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
- if ($handle = opendir($dir)) {
- while (false !== ($file = readdir($handle))) {
- $temp = explode("-",$file,2);
- if(count($temp) > 1) { // means it was split up
- if($fontchoice == $temp[0]) {
- // If the current file prefix matches the $fontchoice, specify the SOURCE image
- $srcimg = imagecreatefrompng($dir.$file);
- }
- }
- }
- }
- /* ------------------------------------------------ */
- /* Start building the new PNG image */
- /* ------------------------------------------------ */
- // Create the image (width, height)
- $newimg = imagecreatetruecolor($newimgwidth, $charheight);
- // Set up the transparent background:
- imagealphablending($newimg,false);
- $col=imagecolorallocatealpha($newimg,44,0,0,127);
- imagefilledrectangle($newimg,0,0,$newimgwidth,$charheight,$col);
- /* ------------------------------------------------ */
- /* Now the character cut/paste loop */
- /* ------------------------------------------------ */
- // Set loop char-counter to zero
- $charcount = 0;
- // Start Loop, repeat for each character in the string:
- foreach(str_split($string) as $char){
- // returns ASCII number
- $ASCIIno = ord($char);
- // 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, 0, $srcimgcharpos, $charcolor * $charheight, $charwidth, $charheight);
- // Move to next char in string
- $charcount++;
- }
- imagesavealpha($newimg,true);
- /* ------------------------------------------------ */
- /* 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 ($doublesize > 1) {
- // Create NewNewImg
- $newnewimg = imagecreatetruecolor($newimgwidth*$doublesize, $newimgheight*$doublesize);
- // Set up the transparent background:
- imagealphablending($newnewimg,false);
- $col=imagecolorallocatealpha($newnewimg,44,0,0,127);
- imagefilledrectangle($newnewimg,0,0,$newimgwidth*$doublesize,$charheight * $doublesize,$col);
- // 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');
- imagesavealpha($newnewimg,true);
- // If cache is set to yes, save the file:
- if ($cacheornot == 1) {
- imagepng($newnewimg, $filepath.$filename);
- }
- imagepng($newnewimg);
- imagedestroy($newimg);
- imagedestroy($newnewimg);
- imagedestroy($srcimg);
- } else {
- // Output and free from memory:
- header('Content-Type: image/png');
- imagepng($newimg);
- // If cache is set to yes, save the file:
- if ($cacheornot == 1) {
- imagepng($newimg, $filepath.$filename);
- }
- imagedestroy($newimg);
- imagedestroy($srcimg);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement