Advertisement
Guest User

tmfcolorparser.inc.php

a guest
Apr 9th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.41 KB | None | 0 0
  1. <?php
  2. /**
  3.  * TMFColorParser v1.1 by oorf|fuckfish (fish@stabb.de)
  4.  *
  5.  */
  6. class TMFColorParser{
  7.  
  8.     var $links, $manialinks;
  9.    
  10.     function getStyledString($string, $match, $col, $wide, $narrow, $caps, $italic, $stripColors){
  11.         $string = substr($string, strlen($match));
  12.         $string = trim ($string);
  13.  
  14.         if ($caps) $string = strtoupper($string);
  15.  
  16.         if (($col||$wide||$narrow||$italic)&&($string)){
  17.             $start1 = "<span style='";
  18.             $start2 = "'>";
  19.             $styles = "";
  20.             $end = "</span>";
  21.             if ($col && !$stripColors){
  22.                 $styles.="color:#".$col.";";
  23.             }
  24.             if ($italic){
  25.                 $styles.="font-style:italic;";
  26.             }
  27.             if ($wide){
  28.                 $styles.="font-weight:bold;";
  29.             }
  30.             if ($narrow){
  31.                 $styles.="letter-spacing: -0.1em;font-size:smaller";
  32.             }
  33.             $string = $start1.$styles.$start2.$string.$end;
  34.         }
  35.         return $string;
  36.     }
  37.  
  38.     function parseLinks($str, $showLinks = true){
  39.         $str = $this->parseManialinks($str, $showLinks);
  40.         $str = str_replace('$L', '$l', $str);
  41.         $this->links=array();
  42.         $linkidx = 0;
  43.         $chunks = explode('$l', $str);
  44.         for ($i=0; $i<count($chunks); $i++){
  45.             $text = $chunks[$i];
  46.             if ($i%2==1){
  47.                 $id = '{LINK'.$linkidx.'}';
  48.                 $linkidx++;
  49.                 if (preg_match('/\A\[(.*)\](.*)\Z/', $text)) {
  50.                     $url = substr($text, strpos($text, '[')+1);
  51.                     $url = substr($url, 0, strpos($url, ']'));
  52.                     $this->links[$id] = $url;
  53.                     if ($showLinks) $chunks[$i] = '$a'.$id.'$x'.substr($text, strpos($text, ']')+1).'$a{/LINK}$x';
  54.                     else $chunks[$i] = substr($text, strpos($text, ']')+1);
  55.                 } else {
  56.                     $this->links[$id]= $text;
  57.                     if ($showLinks) $chunks[$i] = '$a'.$id.'$x'.$text.'$a{/LINK}$x';
  58.                 }
  59.             }
  60.         }
  61.         $this->fixWWWLinks();
  62.         return implode('', $chunks);
  63.     }
  64.    
  65.     function fixWWWLinks(){
  66.         foreach ($this->links as $key => $value){
  67.             $value = trim($value);
  68.             if (substr(strtolower($value), 0, 4) == 'www.'){
  69.                 $value = 'http://'.$value;
  70.                 $this->links[$key] = $value;
  71.             }
  72.         }
  73.     }
  74.    
  75.     function parseManialinks($str, $showLinks = true){
  76.         $str = str_replace('$H', '$h', $str);
  77.         $this->manialinks=array();
  78.         $linkidx = 0;
  79.         $chunks = explode('$h', $str);
  80.         for ($i=0; $i<count($chunks); $i++){
  81.             $text = $chunks[$i];
  82.             if ($i%2==1){
  83.                 $id = '{MLINK'.$linkidx.'}';
  84.                 $linkidx++;
  85.                 if (preg_match('/\A\[(.*)\](.*)\Z/', $text)) {
  86.                     $url = substr($text, strpos($text, '[')+1);
  87.                     $url = substr($url, 0, strpos($url, ']'));
  88.                     $this->manialinks[$id] = $url;
  89.                     if ($showLinks) $chunks[$i] = '$a'.$id.'$x'.substr($text, strpos($text, ']')+1).'$a{/LINK}$x';
  90.                     else $chunks[$i] = substr($text, strpos($text, ']')+1);
  91.                 } else {
  92.                     $this->manialinks[$id]= $text;
  93.                     if ($showLinks) $chunks[$i] = '$a'.$id.'$x'.$text.'$a{/LINK}$x';
  94.                 }
  95.             }
  96.         }
  97.         return implode('', $chunks);
  98.     }
  99.    
  100.    
  101.     function insertLinks($str){
  102.         foreach ($this->manialinks as $key => $value){
  103.             $str = str_replace($key,  '<a href ="tmtp:///:'.$value.'"', $str);
  104.         }
  105.         foreach ($this->links as $key => $value){
  106.             $str = str_replace($key, '<a href="'.$value.'">', $str);
  107.         }
  108.         $str = str_replace('{/LINK}', '</a>', $str);
  109.         return $str;
  110.     }
  111.  
  112.     function toHTML($str, $stripColors = false, $stripLinks = false){
  113.         $col = false;
  114.         $wide = false;
  115.         $narrow = false;
  116.         $caps = false;
  117.         $italic = false;
  118.  
  119.         $str = str_replace('$$','[DOLLAR]',$str);
  120.         $str = str_replace(' ','&nbsp;',$str);
  121.         $str = $this->parseLinks($str, !$stripLinks);
  122.         $chunks = explode('$',$str);
  123.  
  124.         for ($i = 1; $i < count($chunks); $i++){
  125.             if (preg_match("/^[0-9a-f]{2,3}/i",$chunks[$i], $matches)){
  126.                 $col = $matches[0];
  127.                 if (strlen($col)<3) $col.="8";
  128.                 $col = $col[0].$col[0].$col[1].$col[1].$col[2].$col[2];
  129.                 $chunks[$i] = $this->getStyledString($chunks[$i], $matches[0], $col, $wide, $narrow, $caps, $italic, $stripColors);
  130.             } else if (preg_match("/^(i)/i",$chunks[$i], $matches)){
  131.                 $italic = true;
  132.                 $chunks[$i] = $this->getStyledString($chunks[$i], $matches[0], $col, $wide, $narrow, $caps, $italic, $stripColors);
  133.             } else if (preg_match("/^(w)/i",$chunks[$i], $matches)){
  134.                 $narrow = false;
  135.                 $wide = true;
  136.                 $chunks[$i] = $this->getStyledString($chunks[$i], $matches[0], $col, $wide, $narrow, $caps, $italic, $stripColors);
  137.             } else if (preg_match("/^(o)/i",$chunks[$i], $matches)){
  138.                 $narrow = false;
  139.                 $wide = true;
  140.                 $chunks[$i] = $this->getStyledString($chunks[$i], $matches[0], $col, $wide, $narrow, $caps, $italic, $stripColors);
  141.             } else if (preg_match("/^(n)/i",$chunks[$i], $matches)){
  142.                 $wide = false;
  143.                 $narrow = true;
  144.                 $chunks[$i] = $this->getStyledString($chunks[$i], $matches[0], $col, $wide, $narrow, $caps, $italic, $stripColors);
  145.             } else if (preg_match("/^(s)/i",$chunks[$i], $matches)){
  146.                 $chunks[$i] = $this->getStyledString($chunks[$i], $matches[0], $col, $wide, $narrow, $caps, $italic, $stripColors);
  147.             } else if (preg_match("/^(t)/i",$chunks[$i], $matches)){
  148.                 $caps = true;
  149.                 $chunks[$i] = $this->getStyledString($chunks[$i], $matches[0], $col, $wide, $narrow, $caps, $italic, $stripColors);
  150.             } else if (preg_match("/^(m)/i",$chunks[$i], $matches)){
  151.                 $wide = false;
  152.                 $bold = false;
  153.                 $chunks[$i] = $this->getStyledString($chunks[$i], $matches[0], $col, $wide, $narrow, $caps, $italic, $stripColors);
  154.             }  else if (preg_match("/^(g)/i",$chunks[$i], $matches)){
  155.                 $col = false;
  156.                 $chunks[$i] = $this->getStyledString($chunks[$i], $matches[0], $col, $wide, $narrow, $caps, $italic, $stripColors);
  157.             }  else if (preg_match("/^(a)/i",$chunks[$i], $matches)){
  158.                
  159.                 $colSave= $col;
  160.                 $wideSave = $wide;
  161.                 $narrowSave = $narrow;
  162.                 $capsSave = $caps;
  163.                 $italicSave = $italic;
  164.                 $col = false;
  165.                 $wide = false;
  166.                 $narrow = false;
  167.                 $caps = false;
  168.                 $italic = false;
  169.                 $chunks[$i] = $this->getStyledString($chunks[$i], $matches[0], $col, $wide, $narrow, $caps, $italic, $stripColors);
  170.             }  else if (preg_match("/^(x)/i",$chunks[$i], $matches)){
  171.  
  172.                 $col = $colSave;
  173.                 $wide = $wideSave;
  174.                 $narrow = $narrowSave;
  175.                 $caps = $capsSave;
  176.                 $italic = $italicSave;
  177.                
  178.                 $chunks[$i] = $this->getStyledString($chunks[$i], $matches[0], $col, $wide, $narrow, $caps, $italic, $stripColors);
  179.             }  else if (preg_match("/^(z)/i",$chunks[$i], $matches)){
  180.                 $col = false;
  181.                 $wide = false;
  182.                 $narrow = false;
  183.                 $caps = false;
  184.                 $italic = false;
  185.                 $chunks[$i] = $this->getStyledString($chunks[$i], $matches[0], $col, $wide, $narrow, $caps, $italic, $stripColors);
  186.             }
  187.  
  188.         }
  189.  
  190.         for ($i = 1; $i < count($chunks); $i++){
  191.             $chunks[$i] = str_replace('[DOLLAR]','$',$chunks[$i]);
  192.             $chunks[$i] = str_replace('&NBSP;','&nbsp;',$chunks[$i]);
  193.            
  194.         }
  195.  
  196.  
  197.         $str = implode($chunks);
  198.         $str = $this->insertLinks($str);
  199.         return $str;
  200.     }
  201.     function toArray($str){
  202.         $col = false;
  203.         $wide = false;
  204.         $narrow = false;
  205.         $caps = false;
  206.         $italic = false;
  207.         $shadow = false;
  208.  
  209.         $str = str_replace('$$','[DOLLAR]',$str);
  210.         $str = $this->parseLinks($str, false);
  211.         $chunks = explode('$',$str);
  212.  
  213.         $result = array();
  214.         if ($chunks[0]) $result[0]["text"]=str_replace('[DOLLAR]','$',$chunks[0]);
  215.  
  216.  
  217.         for ($i = 1; $i < count($chunks); $i++){
  218.             $match = "";
  219.             if (preg_match("/^[0-9a-f]{2,3}/i",$chunks[$i], $matches)){
  220.                 $col = $matches[0];
  221.                 $match = $col;
  222.                 if (strlen($col)<3) $col.="8";
  223.                 $col = $col[0].$col[0].$col[1].$col[1].$col[2].$col[2];
  224.             } else if (preg_match("/^(i)/i",$chunks[$i], $matches)){
  225.                 $match = $matches[0];
  226.                 $italic = true;
  227.             } else if (preg_match("/^(w)/i",$chunks[$i], $matches)){
  228.                 $match = $matches[0];
  229.                 $narrow = false;
  230.                 $wide = true;
  231.             } else if (preg_match("/^(o)/i",$chunks[$i], $matches)){
  232.                 $match = $matches[0];
  233.                 $narrow = false;
  234.                 $wide = true;
  235.             } else if (preg_match("/^(n)/i",$chunks[$i], $matches)){
  236.                 $match = $matches[0];
  237.                 $wide = false;
  238.                 $narrow = true;
  239.             } else if (preg_match("/^(s)/i",$chunks[$i], $matches)){
  240.                 $match = $matches[0];
  241.                 $shadow = true;
  242.             } else if (preg_match("/^(t)/i",$chunks[$i], $matches)){
  243.                 $match = $matches[0];
  244.                 $caps = true;
  245.             } else if (preg_match("/^(m)/i",$chunks[$i], $matches)){
  246.                 $match = $matches[0];
  247.                 $wide = false;
  248.                 $bold = false;
  249.             }  else if (preg_match("/^(g)/i",$chunks[$i], $matches)){
  250.                 $match = $matches[0];
  251.                 $col = false;
  252.             }  else if (preg_match("/^(x)/i",$chunks[$i], $matches)){
  253.                 $match = $matches[0];
  254.             }  else if (preg_match("/^(z)/i",$chunks[$i], $matches)){
  255.                 $match = $matches[0];
  256.                 $shadow = false;
  257.                 $col = false;
  258.                 $wide = false;
  259.                 $narrow = false;
  260.                 $caps = false;
  261.                 $italic = false;
  262.             }
  263.  
  264.             $chunks[$i] = substr($chunks[$i], strlen($match));
  265.             if ($chunks[$i]){
  266.                 $a = count($result);
  267.                 $result[$a]["text"]=str_replace('[DOLLAR]','$',$chunks[$i]);
  268.                 $result[$a]["italic"]=$italic;
  269.                 $result[$a]["narrow"]=$narrow;
  270.                 $result[$a]["wide"]=$wide;
  271.                 $result[$a]["caps"]=$caps;
  272.                 $result[$a]["shadow"]=$shadow;
  273.                 $result[$a]["color"]=$col;
  274.             }
  275.  
  276.         }
  277.  
  278.         return $result;
  279.     }
  280.  
  281.     function get_rgb($hex) {
  282.         $hex_array = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  283.         'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14,
  284.         'F' => 15);
  285.         $hex = str_replace('#', '', strtoupper($hex));
  286.         if (($length = strlen($hex)) == 3) {
  287.             $hex = $hex{0}.$hex{0}.$hex{1}.$hex{1}.$hex{2}.$hex{2};
  288.             $length = 6;
  289.         }
  290.         if ($length != 6 or strlen(str_replace(array_keys($hex_array), '', $hex)))
  291.         return NULL;
  292.         $rgb['r'] = $hex_array[$hex{0}] * 16 + $hex_array[$hex{1}];
  293.         $rgb['g'] = $hex_array[$hex{2}] * 16 + $hex_array[$hex{3}];
  294.         $rgb['b']= $hex_array[$hex{4}] * 16 + $hex_array[$hex{5}];
  295.         return $rgb;
  296.     }
  297.  
  298.  
  299.     /**
  300.      * draws a TMN color coded string onto the given picture
  301.      *
  302.      * @param imageHandle $src_img The picture to be drawn on
  303.      * @param int $size The fontsize
  304.      * @param int $x The X-Position
  305.      * @param int $y The Y-Position
  306.      * @param color $color The color for letters that are not otherwise color coded (default color)
  307.      * @param String $font The used font
  308.      *                     Attention: You need 4 fonts for the different styles like
  309.      *                              tahoma.ttf      -- normal style
  310.      *                              tahomait.ttf    -- italic style
  311.      *                              tahomawd.ttf    -- wide style
  312.      *                              tahomawdit.ttf  -- wide and italic style
  313.      *                     (narrow style not implemented yet)
  314.      * @param String $text The TMN color coded Text you want to display
  315.      * @param boolean $stripcolors Strip out the colors or not.
  316.      */
  317.     function drawStyledString($src_img,$size, $x, $y , $color, $font, $text, $stripcolors = false){
  318.  
  319.         $chunks = $this->toArray($text);
  320.         //var_dump($text);
  321.         $x_offset = 0;
  322.         $black = imagecolorallocate($src_img, 50,50,50);
  323.  
  324.         for ($i = 0; $i < count ($chunks); $i++){
  325.  
  326.  
  327.             $fontUsed = "./".$font;
  328.             if ($chunks[$i]["wide"]) $fontUsed .= "wd";
  329.             if ($chunks[$i]["italic"]) $fontUsed .= "it";
  330.             if ($chunks[$i]["caps"]) $chunks[$i]["text"] = strtoupper($chunks[$i]["text"]);
  331.             $fontUsed.=".ttf";
  332.  
  333.             if ($chunks[$i]["color"] && (!$stripcolors)){
  334.                 $colRGB = $this->get_rgb("#".$chunks[$i]["color"]);
  335.                 $usedCol = imagecolorallocate($src_img, $colRGB['r'], $colRGB['g'], $colRGB['b']);
  336.             } else {
  337.                 $usedCol = $color;
  338.             }
  339.  
  340.             imagettftext($src_img, $size, 0, $x + $x_offset + 1, $y + 1, $black, $fontUsed, $chunks[$i]["text"]);
  341.             imagettftext($src_img, $size, 0, $x + $x_offset, $y, $usedCol, $fontUsed, $chunks[$i]["text"]);
  342.  
  343.             $bbox = imagettfbbox($size, 0, $fontUsed, $chunks[$i]["text"]);
  344.             $x_offset += $bbox[2]+2;
  345.  
  346.         }
  347.     }
  348.  
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement