SHARE
TWEET

NFG

a guest Mar 1st, 2009 717 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. /* ------------------------------------------------ */
  29. /*              Instructional Block                 */
  30. /* ------------------------------------------------ */
  31.  
  32. /*  HOW TO USE THIS SCRIPT
  33.         1. Requirements
  34.                 PHP
  35.                 GD graphics library (almost always included with PHP)
  36.                 A website (duh)
  37.                 This script (also duh)
  38.                 Some fonts (slightly less, but still duh)
  39.  
  40.         2. Usage
  41.                 . First, decide if you'll use PHPBB-friendly parameters or not.  
  42.                   I suggest leaving this on by default, and just get used to the way it works.
  43.                 . The output of this script is a .PNG image.
  44.                   To link to it, or use it in a webpage, just put the script URL and
  45.                   parameters between some [img] or <img> tags.  It's not tricky.
  46.  
  47.            Examples
  48.                 . Webpage: <img src="http://server/path-to-script/arcade.php/y-font/x-phrase to encode" />
  49.                 . Forum: [img]http://server/path-to-script/arcade.php/y-font/x-phrase to encode[/img]
  50.  
  51.         3. Things to watch for
  52.                 . Some forums don't like spaces in the names.  Use the HTML-friendly space:   %20    instead.  so,   /x-phrase%20to%20encode
  53.                 . Many parameters are optional.  The minimum is font (y) and phrase (x).  
  54.  
  55.         4. Parameters
  56.                 x - words or phrase to create image with
  57.                 y - font to use
  58.                 z - colour or variation.  A font file may contain several fonts stacked vertically.  
  59.                                         This chooses how far down to start. (starts at zero!)
  60.                 h - height of character (default is 8)
  61.                 w - width of character (default is 8)
  62.                                         Note that all characters must be the same height and width.  
  63.                                         There is no facility for variable width fonts.
  64.                 dbl - doublesize or not.  If un-set, defaults to 1x, but you can set dbl to 2-6.  
  65.                                         The script will reduce all values larger than six to six.
  66.                                         4x example:  /dbl-4    
  67.                 cs - colour shift, to adjust the hue of the font.  
  68.                                         Format is red.green.blue.  
  69.                                         Example:  /cs-0.128.64
  70.  
  71.         5. Filenames
  72.                 Your filenames MUST be in this format:  shortcut-whateveryouwant.png
  73.                 where:          shortcut                is the Y/Fontchoice value used in the URL.
  74.                 where:          whateveryouwant         is the descriptive name of the font.
  75.                 example:        fz-FantasyZone.png      the Y/URL code for this font is fz.
  76.                 example:        cotn-Cotton.png         the Y/URL code for this one is cotn.
  77.                 usage:          arcade.php?y=cotn&x=This is the Cotton font!
  78.  
  79. */
  80.  
  81. /* ------------------------------------------------ */
  82. /*              Configuration Block                 */
  83. /*         Edit these settings as desired           */
  84. /* ------------------------------------------------ */
  85.  
  86.                 // NOTE: Config options: 1 = YES, 0 = NO
  87.  
  88.                 // Many forums, including the popular phpBB, won't allow images with ?, = or & in the path.
  89.                 // Do you want standard URLs    arcade.php?param=value&param2=value2
  90.                 // Or PHPbb-friendly URLs?              arcade.php/param-value/param2-value2/.png
  91. $phpbbfriendly = 1;
  92.  
  93.                 // Cache previously-created images?
  94.                 // Note that on a Windows system, there's no filename difference between upper and lower case, so
  95.                 // cached phrases will no refresh if you change the capitalization later.
  96.                 // For example, Windows thinks phrase.png is the same as Phrase.png and PHRASE.png and PhRaSe.png
  97. $cacheornot = 0;
  98.  
  99.                 // Directory to store cached images:
  100.                 // Should prolly be something like   ./cache/  for most servers.  
  101.                 // Be sure to create this directory!  
  102.                 // And make it writable!!
  103. $filepath = './cache/';
  104.  
  105.                 // Set the directory where you store the source fontfiles.  Should be ./fonts/ for most servers.
  106. $dir = './fonts/';
  107.  
  108.                 // I suspect you'll want to stick to 8x8 characters, but if you get something bigger, specify the size here.
  109.                 // Note: this can be overridden later with the H and V parameters.  This just sets the default.
  110.                 // Set default source character width in pixels
  111. $charwidth = 8;
  112.                 // Set default source character height in pixels
  113. $charheight = 8;
  114.  
  115.                 // Character offset: It skips the first 32 ASCII chars, 'cause they're all control-characters that you'll never use.
  116.                 // You should never need to change this.
  117. $charoffset = 32;
  118.  
  119.                 // Set the maximum phrase length (default 100 chars:
  120. $stringlimit = 100;
  121.  
  122. /* ------------------------------------------------ */
  123. /*    You shouldn't need to muck about past here    */
  124. /*           Commented for your enjoy!              */
  125. /*                                                  */
  126. /*     First, get the vars from the URL request     */
  127. /* ------------------------------------------------ */
  128.  
  129. if ($phpbbfriendly == 1) {                                                                                                              
  130.                         // This is the PHPbb-friendly version
  131.                 $scriptname = $_SERVER['SCRIPT_NAME']."/";
  132.                 $filename = str_replace($scriptname, '', $_SERVER['REQUEST_URI']);
  133.                 $filename = urldecode($filename);
  134.                 $bits = explode("/",$filename);
  135.                 $out = array();
  136.                 foreach($bits as $bite) {
  137.                                 $temp = explode("-",$bite, 2);
  138.                                 $out[$temp[0]] = $temp[1];
  139.                 }
  140.                                 // String to create:
  141.                 $string = $out["x"];
  142.                                 // Get the desired font file:
  143.                 $fontchoice = $out["y"];
  144.                                 // Row offset (font colour in multiple-colour fonts)
  145.                 if ($out["z"]) {
  146.                         $charcolor = $out["z"];
  147.                 }
  148.                                 // Check dbl: if set, it becomes the multiplier (up to 6x)
  149.                 if ($out["dbl"]) {
  150.                         $doublesize = $out["dbl"];
  151.                                 // There's no point setting the multiplier to ONE, so if set, change it to TWO
  152.                         if ( $doublesize <= 1 ) {
  153.                                 $doublesize = 2;
  154.                         }
  155.                 }      
  156.                                 // Check for height/width overrides.  If H or V parameters exist, change the font size.
  157.                 if ($out["w"]) {
  158.                         $charwidth = $out["w"];
  159.                 }
  160.                 if ($out["h"]) {
  161.                         $charheight = $out["h"];
  162.                 }
  163.                 if ($out["cs"]) {
  164.                         $colorize = explode(".",$out["cs"]);
  165.                 }
  166.  
  167. } else {                                                                                                                                                
  168.                         // This is the normal version, using standard URL parameters
  169.                                 // String to create:
  170.                 $string = $_GET["x"];  
  171.                                 // Get the desired font file:
  172.                 $fontchoice = $_GET["y"];
  173.                                 // Vertical offset (font colour in multiple-colour fonts)
  174.                 if ($_GET["z"]) {
  175.                         $charcolor = $_GET["z"];        
  176.                 }
  177.                                 // Check dbl: if set, it becomes the multiplier (up to 6x)
  178.                 if ($_GET["dbl"]) {
  179.                         $doublesize = $_GET["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 ($_GET["w"]) {
  187.                         $charwidth = $_GET["s"];
  188.                 }
  189.                 if ($_GET["h"]) {
  190.                         $charheight = $_GET["h"];
  191.                 }
  192.                 if ($_GET["cs"]) {
  193.                         $colorize = explode(".",$_GET["cs"]);
  194.                 }
  195. }
  196.  
  197.  
  198.                 // We don't need any fonts larger than 6x, do we?
  199. if ($doublesize > 6) {
  200.         $doublesize = 6;
  201. }
  202.  
  203. /* ------------------------------------------------ */
  204. /*         Sanitize the string, for safety!         */
  205. /* ------------------------------------------------ */
  206.  
  207.                 // First, check for chars outside ASCII range 32-126 (20-7E in hex).
  208. $string = preg_replace('/[^(\x20-\x7E)]*/','', $string);
  209.                 // Limit string to the number of chars specified in config:
  210. $string = substr($string, 0, $stringlimit);
  211.  
  212. /* ------------------------------------------------ */
  213. /*   Now, generate a filename and check the cache   */
  214. /* ------------------------------------------------ */
  215.  
  216.                 // Create the filename:
  217. $stringpure = urlencode($string);
  218. $current = "./cache/".$fontchoice.$charcolor.$doublesize.$charheight.$charwidth.$stringpure.".png";
  219.  
  220.                 // Check the cache.  If the file exists, spit it out and die:
  221.         if (file_exists($current)) {
  222.                 $contentType = 'Content-type: image/png';
  223.                 header ($contentType);
  224.                 readfile($current);
  225.                 die;
  226.         }
  227.  
  228. /* ------------------------------------------------ */
  229. /*          This is the preparation section         */
  230. /* ------------------------------------------------ */
  231.  
  232.                 //The new image width should equal the # of chars x the width of each.
  233. $newimgwidth = ($charwidth * strlen($string));
  234.                 // Since the source charset starts with SPACE, add 32 to the ASCIIno to skip unwanted chars.
  235. $newimgheight = $charheight;
  236.  
  237.  
  238. /* ------------------------------------------------ */
  239. /*     This is the new font Detect and Select!      */
  240. /*   Look for a file which matches the $fontchoice  */
  241. /* ------------------------------------------------ */
  242.  
  243.                 // Thanks Twyst for this segment!
  244.  
  245.                 // Basically opens the $dir specified in the config block
  246.                 // then loops through every file looking for one that matches the Y/Fontchoice value
  247.  
  248. if ($handle = opendir($dir)) {
  249.         while (false !== ($file = readdir($handle))) {
  250.                 $temp = explode("-",$file,2);
  251.                 if(count($temp) > 1) { // means it was split up
  252.                         if($fontchoice == $temp[0]) {
  253.                                                         // If the current file prefix matches the $fontchoice, specify the SOURCE image
  254.                                                         $srcimg = imagecreatefrompng($dir.$file);                      
  255.                                                 }
  256.                                 }
  257.         }
  258. }
  259.  
  260. /* ------------------------------------------------ */
  261. /*         Start building the new PNG image         */
  262. /* ------------------------------------------------ */
  263.  
  264.  
  265.                 // Create the image (width, height)
  266. $newimg = imagecreatetruecolor($newimgwidth, $charheight);
  267.  
  268.                 // Set up the transparent background:
  269. imagealphablending($newimg,false);
  270. $col=imagecolorallocatealpha($newimg,44,0,0,127);
  271. imagefilledrectangle($newimg,0,0,$newimgwidth,$charheight,$col);
  272.  
  273. /* ------------------------------------------------ */
  274. /*          Now the character cut/paste loop        */
  275. /* ------------------------------------------------ */
  276.  
  277.  
  278.                 // Set loop char-counter to zero
  279. $charcount = 0;
  280.  
  281.                 // Start Loop, repeat for each character in the string:
  282. foreach(str_split($string) as $char){
  283.                 // returns ASCII number
  284.         $ASCIIno = ord($char);
  285.                 // Set NEWIMG current cursor H position
  286.         $currentHpos = ($charwidth * $charcount);
  287.                 // H-position for grabbing char from SRC image (width x ASCII number + offset)
  288.         $srcimgcharpos = ($ASCIIno - $charoffset) * $charwidth;
  289.                 // Copy the letter from font image to new image
  290.         imagecopy($newimg, $srcimg, $currentHpos, 0, $srcimgcharpos, $charcolor * $charheight, $charwidth, $charheight);
  291.                 // Move to next char in string
  292.         $charcount++;
  293. }
  294.  
  295. imagesavealpha($newimg,true);
  296.  
  297. /* ------------------------------------------------ */
  298. /*              Colour Shift If Desired             */
  299. /* ------------------------------------------------ */
  300.  
  301.  if($colorize) {
  302.         imagefilter($newimg, IMG_FILTER_COLORIZE, $colorize[0],$colorize[1],$colorize[2]);
  303. }
  304.  
  305. /* ------------------------------------------------ */
  306. /*         Two save-routines: big and x size        */
  307. /* ------------------------------------------------ */
  308.  
  309.                 // Filename is the $string:
  310. $filename = $fontchoice.$charcolor.$doublesize.$charheight.$charwidth.$stringpure.".png";
  311.  
  312. if ($doublesize > 1) {
  313.                         // Create NewNewImg
  314.  
  315.         $newnewimg = imagecreatetruecolor($newimgwidth*$doublesize, $newimgheight*$doublesize);
  316.  
  317.                         // Set up the transparent background:
  318.         imagealphablending($newnewimg,false);
  319.         $col=imagecolorallocatealpha($newnewimg,44,0,0,127);
  320.         imagefilledrectangle($newnewimg,0,0,$newimgwidth*$doublesize,$charheight * $doublesize,$col);
  321.  
  322.                         // Double the size from old image to new image:
  323.         imagecopyresized($newnewimg, $newimg, 0, 0, 0, 0, $newimgwidth * $doublesize, $newimgheight * $doublesize, $newimgwidth, $newimgheight);
  324.  
  325.         // Output and free from memory:
  326.         header('Content-Type: image/png');
  327.         imagesavealpha($newnewimg,true);
  328.                         // If cache is set to yes, save the file:
  329.         if ($cacheornot == 1) {
  330.                 imagepng($newnewimg, $filepath.$filename);
  331.         }
  332.         imagepng($newnewimg);
  333.         imagedestroy($newimg);
  334.         imagedestroy($newnewimg);
  335.         imagedestroy($srcimg);
  336.  
  337. } else {
  338.  
  339.         // Output and free from memory:
  340.         header('Content-Type: image/png');
  341.         imagepng($newimg);
  342.                                 // If cache is set to yes, save the file:
  343.         if ($cacheornot == 1) {
  344.                 imagepng($newimg, $filepath.$filename);
  345.         }
  346.         imagedestroy($newimg);
  347.         imagedestroy($srcimg);
  348.  
  349. }
  350.  
  351. ?>
RAW Paste Data
Want to get better at PHP?
Learn to code PHP in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top