Advertisement
Guest User

NFG

a guest
Mar 25th, 2009
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 21.71 KB | None | 0 0
  1. <?
  2.  
  3.                 // This is the PHP script of DOOM
  4.                 // It prolly won't work.
  5.  
  6.                 // Written by NFG ( http://nfgworld.com ) in February 2009.
  7.  
  8.                 // While I take credit for making the thing assemble fonts
  9.                 // Which I'm mad proud of, 'cause I don't code...
  10.                 // Many kudos to Twyst for helping with some tricky stuff:
  11.                 //      PHPBB URLs
  12.                 //      maintenance-friendly filenames.
  13.                 //      colour shifting
  14.  
  15.  
  16. /* ------------------------------------------------ */
  17. /*                      License                     */
  18. /* ------------------------------------------------ */
  19.  
  20. /*              This script is released under a Creative Commons Zero license.
  21.                 This means it's free of just about all encumbrances.
  22.                 Do what you want with it.
  23.                 A tip of the hat for ol' NFG would be nice, but not required.  =)
  24.                
  25.                 http://creativecommons.org/licenses/zero/1.0/
  26.  
  27. /* ------------------------------------------------ */
  28. /*                  Version History                 */
  29. /* ------------------------------------------------ */
  30. /*
  31.         1.0 - First Public Release
  32.         1.1 - Added Font List & Font Counter, fixed some bugs that only became apparent with the lister...
  33.               Includes word-balloon addition by Twystneko.
  34.         1.2 - Added random font option.  This required some internal changes, but no functional differences.
  35.  
  36. /* ------------------------------------------------ */
  37. /*              Instructional Block                 */
  38. /* ------------------------------------------------ */
  39.  
  40. /*  HOW TO USE THIS SCRIPT
  41.         1. Requirements
  42.                 PHP
  43.                 GD graphics library (almost always included with PHP)
  44.                 A website (duh)
  45.                 This script (also duh)
  46.                 Some fonts (slightly less, but still duh)
  47.  
  48.         2. Usage
  49.                 . First, decide if you'll use PHPBB-friendly parameters or not.  
  50.           I suggest leaving this on by default, and just get used to the way it works.
  51.                 . The output of this script is a .PNG image.
  52.                   To link to it, or use it in a webpage, just put the script URL and
  53.                   parameters between some [img] or <img> tags.  It's not tricky.
  54.  
  55.            Examples
  56.                 . Webpage: <img src="http://server/path-to-script/arcade.php/y-font/x-phrase to encode" />
  57.                 . Forum: [img]http://server/path-to-script/arcade.php/y-font/x-phrase to encode[/img]
  58.  
  59.         3. Things to watch for
  60.                 . Some forums don't like spaces in the names.  Use the HTML-friendly space:   %20    instead.  so,   /x-phrase%20to%20encode
  61.                 . Many parameters are optional.  The minimum is font (y) and phrase (x).  
  62.  
  63.         4. Parameters
  64.                 x - words or phrase to create image with
  65.                 y - font to use.  Use r for random (usage:  /y-r/ )
  66.                 z - colour or variation.  A font file may contain several fonts stacked vertically.  
  67.                     This chooses how many rows down an image file to start. (starts at zero!)
  68.                     use r for random (usage: /z-r/ )
  69.                 h - height of character (default is 8 if unset)
  70.                 w - width of character (default is 8 if unset)
  71.                     Note that all characters must be the same height and width.  
  72.                     There is no facility for variable width fonts.
  73.                 dbl - doublesize or not.  If un-set, defaults to 1x, but you can set dbl to 2-6.  
  74.                     The script will reduce all values larger than six to six.
  75.                     4x example:  /dbl-4    
  76.                 cs - colour shift, to adjust the hue of the font.  
  77.                     Format is red.green.blue.  
  78.                     Example:  /cs-0.128.64
  79.                 list - list all fonts.  Ignores the x-phrase and y-fontchoice, but obeys all other commands.  (usage:  /list-1/ )
  80.                 count - changes x-phrase into the number of available fonts.  Obeys all other commands.   (usage: /count-1/ )
  81.                 b - Bubble position. This activates the speech bubble mode, and is either "u" (up) or "d" (down).
  82.                 bp - a percentage of where it is along the bubble.
  83.  
  84.     5. Filenames
  85.         Your filenames MUST be in this format:  shortcut-whateveryouwant.png
  86.                 where:      shortcut        is the Y/Fontchoice value used in the URL.
  87.         where:      whateveryouwant     is the descriptive name of the font.
  88.                 example:    fz-FantasyZone.png      the Y/URL code for this font is fz.
  89.                 example:    cotn-Cotton.png     the Y/URL code for this one is cotn.
  90.                 usage:      arcade.php?y=cotn&x=This is the Cotton font!
  91.  
  92. */
  93.  
  94. /* ------------------------------------------------ */
  95. /*              Configuration Block                 */
  96. /*         Edit these settings as desired           */
  97. /* ------------------------------------------------ */
  98.  
  99.                 // NOTE: Config options: 1 = YES, 0 = NO
  100.  
  101.                 // Many forums, including the popular phpBB, won't allow images with ?, = or & in the path.
  102.                 // Do you want standard URLs    arcade.php?param=value&param2=value2
  103.                 // Or PHPbb-friendly URLs?              arcade.php/param-value/param2-value2/.png
  104. $phpbbfriendly = 1;
  105.  
  106.                 // Cache previously-created images?
  107.         // Note that on a Windows system, there's no filename difference between upper and lower case, so
  108.         // cached phrases will no refresh if you change the capitalization later.
  109.         // For example, Windows thinks phrase.png is the same as Phrase.png and PHRASE.png and PhRaSe.png
  110. $cacheornot = 0;
  111.  
  112.                 // Directory to store cached images:
  113.                 // Should prolly be something like   ./cache/  for most servers.  
  114.         // Be sure to create this directory!  
  115.         // And make it writable!!
  116. $filepath = './cache/';
  117.  
  118.                 // Set the directory where you store the source fontfiles.  Should be ./fonts/ for most servers.
  119. $dir = './fonts/';
  120.  
  121.                 // where the components for the speech bubbles are
  122. $bubblepath = "./bubble/";
  123.  
  124.                 // I suspect you'll want to stick to 8x8 characters, but if you get something bigger, specify the size here.
  125.                 // Note: this can be overridden later with the H and V parameters.  This just sets the default.
  126.                 // Set default source character width in pixels
  127. $charwidth = 8;
  128.                 // Set default source character height in pixels
  129. $charheight = 8;
  130.  
  131.                 // Character offset: It skips the first 32 ASCII chars, 'cause they're all control-characters that you'll never use.
  132.                 // You should never need to change this.
  133. $charoffset = 32;
  134.  
  135.                 // Set the maximum phrase length (default 100 chars:
  136. $stringlimit = 100;
  137.  
  138.                 // Allow the creation of a long list of available fonts?
  139.                 // When doing a font list, the spacing between fonts is 1 x fontsize, so 3x font = 3px gap.
  140. $allowlist = 1;
  141.  
  142. /* ------------------------------------------------ */
  143. /*    You shouldn't need to muck about past here    */
  144. /*           Commented for your enjoy!              */
  145. /*                                                  */
  146. /*     First, get the vars from the URL request     */
  147. /* ------------------------------------------------ */
  148.  
  149. if ($phpbbfriendly == 1) {                                                                                                              
  150.             // This is the PHPbb-friendly version
  151.                 $scriptname = $_SERVER['SCRIPT_NAME']."/";
  152.                 $filename = str_replace($scriptname, '', $_SERVER['REQUEST_URI']);
  153.                 $filename = urldecode($filename);
  154.                 $bits = explode("/",$filename);
  155.                 $out = array();
  156.                 foreach($bits as $bite) {
  157.                                 $temp = explode("-",$bite, 2);
  158.                                 $out[$temp[0]] = $temp[1];
  159.                 }
  160.                                 // String to create:
  161.                 $string = $out["x"];
  162.                                 // Get the desired font file:
  163.                 if ($out["y"] == "r") {
  164.                     $randomfont = TRUE;
  165.                 } else {
  166.                     $fontchoice = $out["y"];
  167.                 }
  168.                                 // Row offset (font colour in multiple-colour fonts)
  169.                 if ($out["z"]) {
  170.                         if ($out["z"] == "r") {
  171.                             $randomfontcolour = TRUE;
  172.                         } else {
  173.                             $randomfontcolour = FALSE;
  174.                             $charcolor = $out["z"];
  175.                         }
  176.                 }
  177.                                 // Check dbl: if set, it becomes the multiplier (up to 6x)
  178.                 if ($out["dbl"]) {
  179.                         $doublesize = $out["dbl"];
  180.                                 // There's no point setting the multiplier to ONE, so if set, change it to TWO
  181.                         if ( $doublesize <= 1 ) {
  182.                                 $doublesize = 2;
  183.                         }
  184.                 }      
  185.                                 // Check for height/width overrides.  If H or V parameters exist, change the font size.
  186.                 if ($out["w"]) {
  187.                         $charwidth = $out["w"];
  188.                 }
  189.                 if ($out["h"]) {
  190.                         $charheight = $out["h"];
  191.                 }
  192.                 if ($out["cs"]) {
  193.                         $colorize = explode(".",$out["cs"]);
  194.                 }
  195.                 if($out["b"]) {
  196.                     $b = $out['b'];
  197.                 }
  198.                 if($out["bp"]) {
  199.                     $bp = $out['bp'];
  200.                 } else {
  201.                     $bp = 0;
  202.                 }
  203.                 if ($out["list"]) {
  204.                         $listfonts = true;
  205.                 } else {
  206.                         $listfonts = false;
  207.                 }
  208.                 if ($out["count"]) {
  209.                         $countfonts = true;
  210.                 } else {
  211.                         $countfonts = false;
  212.                 }
  213.  
  214. } else {                                                                                                                                                
  215.             // This is the normal version, using standard URL parameters
  216.                 // String to create:
  217.                 $string = $_GET["x"];  
  218.                                 // Get the desired font file:
  219.                 if ($_GET["y"] == "r") {
  220.                     $randomfont = TRUE;
  221.                 } else {
  222.                     $fontchoice = $_GET["y"];
  223.                 }
  224.                                 // Vertical offset (font colour in multiple-colour fonts)
  225.                 if ($_GET["z"]) {
  226.                         if ($_GET["z"] == "r") {
  227.                             $randomfontcolour = TRUE;
  228.                         } else {
  229.                             $randomfontcolour = FALSE;
  230.                             $charcolor = $out["z"];
  231.                         }
  232.                 }
  233.                                 // Check dbl: if set, it becomes the multiplier (up to 6x)
  234.                 if ($_GET["dbl"]) {
  235.                         $doublesize = $_GET["dbl"];
  236.                                         // There's no point setting the multiplier to ONE, so if set, change it to TWO
  237.                         if ( $doublesize <= 1 ) {
  238.                                 $doublesize = 2;
  239.                         }
  240.                 }
  241.                                         // Check for height/width overrides.  If H or V parameters exist, change the font size.
  242.                 if ($_GET["w"]) {
  243.                         $charwidth = $_GET["s"];
  244.                 }
  245.                 if ($_GET["h"]) {
  246.                         $charheight = $_GET["h"];
  247.                 }
  248.                 if ($_GET["cs"]) {
  249.                         $colorize = explode(".",$_GET["cs"]);
  250.                 }
  251.                 if($_GET["b"]) {
  252.                         $b = $_GET['b'];
  253.                 }
  254.                 if($_GET["bp"]) {
  255.                         $bp = $_GET['bp'];
  256.                 } else {
  257.                     $bp = 0;
  258.                 }
  259.                 if ($_GET["list"]) {
  260.                         $listfonts = true;
  261.                 } else {
  262.                         $listfonts = false;
  263.                 }
  264.                 if ($_GET["count"]) {
  265.                         $countfonts = true;
  266.                 } else {
  267.                         $countfonts = false;
  268.                 }                
  269. }
  270.  if($b == "none") {
  271.     unset($b);
  272.  }
  273.  
  274.                 // We don't need any fonts larger than 6x, do we?
  275. if ($doublesize > 6) {
  276.         $doublesize = 6;
  277. }
  278.  
  279. /* ------------------------------------------------ */
  280. /*         Sanitize the string, for safety!         */
  281. /* ------------------------------------------------ */
  282.  
  283.                 // First, check for chars outside ASCII range 32-126 (20-7E in hex).
  284. $string = preg_replace('/[^(\x20-\x7E)]*/','', $string);
  285.                 // Limit string to the number of chars specified in config:
  286. $string = substr($string, 0, $stringlimit);
  287.  
  288. /* ------------------------------------------------ */
  289. /*   Now, generate a filename and check the cache   */
  290. /* ------------------------------------------------ */
  291.  
  292.                 // Create the filename.  Skip the entire cache procedure if we're listing or counting fonts.
  293. if ((!$listfonts) or (!$countfonts)) {
  294.     $stringpure = urlencode($string);
  295.     $current = "./cache/".$fontchoice.$charcolor.$doublesize.$charheight.$charwidth.$stringpure.".png";
  296.                 // Check the cache.  If the file exists, spit it out and die:
  297.     if (file_exists($current)) {
  298.         $contentType = 'Content-type: image/png';
  299.         header ($contentType);
  300.         readfile($current);
  301.         die;
  302.     }
  303. }
  304.  
  305.  
  306. /* ------------------------------------------------ */
  307. /*     This is the new font Detect and Select!      */
  308. /*   Look for a file which matches the $fontchoice  */
  309. /* ------------------------------------------------ */
  310.  
  311.                 // Thanks Twyst for this segment!
  312.  
  313.                 // Basically opens the $dir specified in the config block
  314.                 // then loops through every file looking for one that matches the Y/Fontchoice value
  315.                 // New in 1.1 - remembers the number of fonts and the max font-name-length for the fontlist
  316.  
  317. $maxfontlength = "0";   // For counting the max chars in a font name (for calc'ing font list width)
  318. $numfonts = "0";        // for counting the number of fonts
  319.  
  320. if ($handle = opendir($dir)) {
  321.         while (false !== ($file = readdir($handle))) {
  322.                 $temp = explode("-",$file,2);
  323.                 if(count($temp) > 1) {                      // means it was split up
  324.                     $fontnames[$numfonts] = $file;          // An array of filenames, for later abuse.
  325.                     $maxfontlength = max($maxfontlength, strlen(substr($temp[1],0,-4)));
  326.                     if ($listfonts) { $fontfiles[$numfonts] = $dir.$file; }
  327.                         if($fontchoice == $temp[0]) {
  328.                             $fontfile = $dir.$file;         // If the current file prefix matches the $fontchoice, specify the SOURCE image
  329.  
  330.                         }
  331.                     $numfonts++;                            // increment the font count.  Also used for incrementing $fontnames array.
  332.                     }
  333.         }
  334. }
  335.  
  336. /* ------------------------------------------------ */
  337. /*             Special-case adjustments             */
  338. /*        For LIST, COUNT and similar things        */
  339. /* ------------------------------------------------ */
  340.  
  341.                 // Are we counting the fonts?  Change the string to the number of fonts.
  342. if ($countfonts) {
  343.         $string = $numfonts;
  344. }
  345.  
  346.                 // If we're doing $randomfonts, change the font filename to be a random one:
  347. if ($randomfont) {
  348.     $fontarraychoice = rand(0,$numfonts);
  349.     $fontfile = $dir.$fontnames[$fontarraychoice];
  350. }
  351.  
  352.                 // Is the font colour random?  Check the z-depth and pick one.
  353. if ($randomfontcolour) {
  354.     $size = getimagesize($fontfile);
  355.     $zcount = $size[1] / $charheight;
  356.     $charcolor = rand(0,$zcount);
  357. }
  358.  
  359. /* ------------------------------------------------ */
  360. /*          This is the preparation section         */
  361. /* ------------------------------------------------ */
  362.  
  363.                 //The new image width should equal the # of chars x the width of each.
  364. if ($listfonts) {
  365.     $newimgwidth = ($charwidth * $maxfontlength);
  366. } else {
  367.     $newimgwidth = ($charwidth * strlen($string));
  368. }
  369.                 //The new image height should equal the # of fonts x the height of each char of each.
  370. if ($listfonts) {
  371.     $newimgheight = (($charheight * $numfonts) + ($numfonts * 2));
  372. } else {
  373.     $newimgheight = $charheight;
  374. }
  375.  
  376. /* ------------------------------------------------ */
  377. /*         Start building the new PNG image         */
  378. /* ------------------------------------------------ */
  379.  
  380.  
  381.                 // Create the image (width, height)
  382. $newimg = imagecreatetruecolor($newimgwidth, $newimgheight);
  383.  
  384.                 // Set up the transparent background:
  385. imagealphablending($newimg,false);
  386. $col=imagecolorallocatealpha($newimg,44,0,0,127);
  387. imagefilledrectangle($newimg,0,0,$newimgwidth,$newimgheight,$col);
  388.  
  389. /* ------------------------------------------------ */
  390. /*          Now the character cut/paste loop        */
  391. /* ------------------------------------------------ */
  392.  
  393.  
  394.         //  NEW for v1.1 - loops through the vertical to accomodate the list.
  395.  
  396.         // Begin vertical loop.  Number of fonts determines number of loops.
  397.         // $fontnames is the array
  398.  
  399.                 // If we're counting fonts, echo only the count value, replacing any user string in the URL
  400.  
  401. $vertloop = 0;                      // set vertical loop counter to zero
  402.  
  403. foreach ($fontnames as $currentfont) {
  404.     if ($listfonts) {
  405.         $fontbits = explode("-",$currentfont,2);
  406.         $string = substr($fontbits[1],0,-4);        // If this is a font list, the current string should be the font name
  407.         $currentVpos = (($charheight + 2) * $vertloop);
  408.         $srcimg = imagecreatefrompng($fontfiles[$vertloop]);
  409.     } else {
  410.         $srcimg = imagecreatefrompng($fontfile);    // Change to the current fontfile.
  411.     }
  412.                     echo $fontfiles[$numfonts];
  413.                     // Set horizontal loop char-counter to zero
  414.         $charcount = 0;
  415.                     // Start Loop, repeat for each character in the string:
  416.         foreach(str_split($string) as $char) {
  417.                     // returns ASCII number
  418.             $ASCIIno = ord($char);
  419.                     // Set NEWIMG current cursor H position
  420.             $currentHpos = ($charwidth * $charcount);
  421.                     // H-position for grabbing char from SRC image (width x ASCII number + offset)
  422.             $srcimgcharpos = ($ASCIIno - $charoffset) * $charwidth;
  423.                     // Copy the letter from font image to new image
  424.             imagecopy($newimg, $srcimg, $currentHpos, $currentVpos, $srcimgcharpos, $charcolor * $charheight, $charwidth, $charheight);
  425.                     // Move to next char in string
  426.             $charcount++;
  427.         }
  428.         $vertloop++;                // Increment the vertical counter
  429. }
  430.  
  431. imagesavealpha($newimg,true);
  432.  
  433. /* ------------------------------------------------ */
  434. /*              Colour Shift If Desired             */
  435. /* ------------------------------------------------ */
  436.  
  437.  if($colorize) {
  438.         imagefilter($newimg, IMG_FILTER_COLORIZE, $colorize[0],$colorize[1],$colorize[2]);
  439. }
  440.  
  441. /* ------------------------------------------------ */
  442. /*         Two save-routines: big and x size        */
  443. /* ------------------------------------------------ */
  444.  
  445.                 // Filename is the $string:
  446. $filename = $fontchoice.$charcolor.$doublesize.$charheight.$charwidth.$stringpure.".png";
  447. if($b) {
  448.        
  449.     if($b == "d") {
  450.         // the pointer is down.
  451.         $dir = "bot";
  452.     } else {
  453.         $dir = "top";
  454.     }
  455.     if($bp <= 40) {
  456.         $s = "l";
  457.     } elseif($bp >= 60) {
  458.         $s = "r";
  459.     } else {
  460.         $s = "c";
  461.     }
  462.     $pname = $s."-".$dir.".png";
  463.     $pt = imagecreatefrompng($bubblepath.$pname);
  464.     // got the pointer.
  465.     // now build step 1 - the background
  466.     $bgt = imagecreatefrompng($bubblepath."bg.png");
  467.    
  468.    
  469.     $width = imagesx($newimg);
  470.     $height = imagesy($newimg);
  471.    
  472.    
  473.    
  474.     $step1 = imagecreatetruecolor($width+4, $height+8);
  475.     imagesavealpha($step1,true);
  476.     imagecopyresized($step1, $bgt, 0,0,0,0,$width+4, $height+8,imagesx($bgt),imagesy($bgt));
  477.     imagecopy($step1,$newimg,2,4,0,0,$width,$height);
  478.     $widthb = imagesx($step1);
  479.     $heightb= $height+8;
  480.     $step2 = imagecreatetruecolor($widthb +8, $heightb);
  481.     imagesavealpha($step2,true);
  482.     $bg = imagecolorallocatealpha($step2,255,255,255,127);
  483.     imagefill($step2,0,0,$bg);
  484.     imagecopy($step2,$step1,4,0,0,0,$widthb,$heightb);
  485.     $lft = imagecreatefrompng($bubblepath.'l-end.png');
  486.     $rgt = imagecreatefrompng($bubblepath.'r-end.png');
  487.     imagecopy($step2,$lft,0,0,0,0,4,16);
  488.     imagecopy($step2,$rgt,$widthb+4,0,0,0,4,16);
  489.    
  490.    
  491.     $step3 = imagecreatetruecolor(imagesx($step2), imagesy($step2)+8);
  492.     imagesavealpha($step3,true);
  493.     $bg = imagecolorallocatealpha($step3,255,255,255,127);
  494.     imagefill($step3,0,0,$bg);
  495.     if($b == "u") {
  496.         $voffset = 8;
  497.         $poffset = 0;
  498.     } else {
  499.         $voffset = 0;
  500.         $poffset = imagesy($step2) - 2;
  501.     }
  502.    
  503.     $xpos = floor($width * ($bp/100)) + 4;
  504.     imagecopy($step3,$step2,0,$voffset,0,0,imagesx($step2), imagesy($step2));
  505.     imagecopy($step3,$pt,$xpos,$poffset,0,0,imagesx($pt),imagesy($pt));
  506.     $newimg = $step3;
  507.    
  508. }
  509. if ($doublesize > 1) {
  510.                         // Create NewNewImg
  511.         $newimgwidth = imagesx($newimg);
  512.         $newimgheight = imagesy($newimg);
  513.         $newnewimg = imagecreatetruecolor($newimgwidth*$doublesize, $newimgheight*$doublesize);
  514.  
  515.                         // Set up the transparent background:
  516.         imagealphablending($newnewimg,false);
  517.         $col=imagecolorallocatealpha($newnewimg,44,0,0,127);
  518.         imagefilledrectangle($newnewimg,0,0,$newimgwidth*$doublesize,$charheight * $doublesize,$col);
  519.  
  520.                         // Double the size from old image to new image:
  521.         imagecopyresized($newnewimg, $newimg, 0, 0, 0, 0, $newimgwidth * $doublesize, $newimgheight * $doublesize, $newimgwidth, $newimgheight);
  522.  
  523.         // Output and free from memory:
  524.         header('Content-Type: image/png');
  525.         imagesavealpha($newnewimg,true);
  526.                         // If cache is set to yes, save the file:
  527.         if ($cacheornot == 1) {
  528.                 imagepng($newnewimg, $filepath.$filename);
  529.         }
  530.         imagepng($newnewimg);
  531.         imagedestroy($newimg);
  532.         imagedestroy($newnewimg);
  533.         imagedestroy($srcimg);
  534.  
  535. } else {
  536.  
  537.         // Output and free from memory:
  538.         header('Content-Type: image/png');
  539.         imagepng($newimg);
  540.                                 // If cache is set to yes, save the file:
  541.         if ($cacheornot == 1) {
  542.                 imagepng($newimg, $filepath.$filename);
  543.         }
  544.         imagedestroy($newimg);
  545.         imagedestroy($srcimg);
  546.  
  547. }
  548.  
  549. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement