[ Index ]

PHP Cross Reference of phpwcms V1.4.3 _r380 (23.11.09)

title

Body

[close]

/include/inc_module/mod_graphical_text/inc_front/ -> gt.func.inc.php (source)

   1  <?php
   2  // MOD Title: phpwcms Graphical Text MOD 
   3  // MOD Author: Jerome < spam@jerome-gamez.de > (Jerome Gamez) http://jerome-gamez.de/ 
   4  // MOD Description: Adds the possibilty to create dynamic graphical text 
   5  //                  with replacement tags 
   6  // 
   7  // MOD Version: 2.0 
   8  
   9  
  10  require_once  (PHPWCMS_ROOT.'/include/inc_module/mod_graphical_text/inc_lib/functions.general.inc.php');
  11  
  12  // Path for caching purposes
  13  define('CACHE_PREFIX', PHPWCMS_ROOT.'/'.$phpwcms["content_path"].'gt/' );
  14  
  15  // Path for display
  16  define('DISPLAY_PREFIX', $phpwcms["content_path"].'gt/' );
  17  
  18  function hex2dec($hex)
  19  {
  20      // Converts HEX-values to RGB-arrays
  21      $color = str_replace('#', '', $hex);
  22      $ret = array(
  23          'r' => hexdec(substr($color, 0, 2)),
  24          'g' => hexdec(substr($color, 2, 2)),
  25          'b' => hexdec(substr($color, 4, 2))
  26          );
  27          
  28    return $ret;
  29  }
  30  
  31  
  32  function create_picture($cachefile, $font, $text, $antialiasing, $size, $fgcolor, $fgtransparency, $bgcolor, $bgtransparency, $line_width, $format, $x=0, $y=0, $h=5, $rotation='default')
  33  {
  34      // Creates a picture in the cache folder
  35      $fgcolor        = hex2dec($fgcolor);
  36      $bgcolor        = hex2dec($bgcolor);
  37      
  38      $_fval_x = intval($x);
  39      $_fval_y = intval($y);
  40      $_fval_h = intval($h);
  41      
  42      // Font properties
  43      $fontfile = PHPWCMS_ROOT.'/include/inc_module/mod_graphical_text/inc_fonts/'.$font;
  44      $bbox = imagettfbbox($size, 0, $fontfile, $text);
  45      $font_left=($bbox[0]>$bbox[6])?$bbox[6]:$bbox[0];
  46      $font_right=($bbox[2]>$bbox[4])?$bbox[2]:$bbox[4];
  47      
  48      // Check height with letters like 'pbl' etc. to center the text correctly
  49      $bbox = imagettfbbox($size, 0, $fontfile, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!\"§$%&/()=?");
  50      $font_top=($bbox[1]>$bbox[7])?$bbox[7]:$bbox[1];
  51      $font_bottom=($bbox[3]>$bbox[5])?$bbox[3]:$bbox[5];
  52      
  53      $font_width=$font_right-$font_left;
  54      $font_height=$font_bottom-$font_top;
  55          
  56      $im = imagecreate($font_width + 5, $font_height + $_fval_h);
  57      
  58      if (intval($bgtransparency) == 1 && $format != "jpg") {
  59          $background = imagecolorallocatealpha($im, $bgcolor['r'], $bgcolor['g'], $bgcolor['b'], 127);
  60      } else{
  61          $background = imagecolorallocate($im, $bgcolor['r'], $bgcolor['g'], $bgcolor['b']);
  62      }    
  63  
  64      if (intval($fgtransparency) == 1 && $format != "jpg") {
  65          $color = imagecolorallocatealpha($im, $fgcolor['r'], $fgcolor['g'], $fgcolor['b'], 127);
  66      } else {
  67          $color = imagecolorallocate($im, $fgcolor['r'], $fgcolor['g'], $fgcolor['b']);
  68      }
  69          
  70      // Check antialiasing
  71      if (intval($antialiasing) == 0) $color = "-".$color;
  72      
  73      
  74      $im_width = imagesx($im);
  75      $im_height = imagesy($im);
  76      
  77      $font_start_x = (($im_width - $font_width)/2) + $_fval_x; 
  78      $font_start_y = ($im_height/2) + ($size/2) + $_fval_y;
  79          
  80      if( ! seems_utf8($text) ) {
  81          $text = utf8_encode($text);
  82      }
  83  
  84      imagettftext($im, $size, 0, $font_start_x, $font_start_y, $color, $fontfile, $text);
  85      
  86      // Create underline
  87      for ($i = 0;$i < $line_width; $i++)
  88      {
  89          imageline($im, $font_start_x, $font_height+$i, $font_start_x+$font_width, $font_height+$i, $color);
  90      }
  91      
  92      // rotate image
  93      if($rotation == 'hcw' || $rotation == 'cw' || $rotation == 'ccw') {
  94          $im = rotateImage($im, $rotation);
  95      }
  96      
  97      // Create cached image
  98      switch ($format)
  99      {
 100          case "jpg":
 101              imagejpeg($im, $cachefile, 100);
 102              break;
 103              
 104          case "gif":
 105              imagegif($im, $cachefile, 100);
 106              break;
 107          
 108          case "png":
 109              imagepng($im, $cachefile, 9);
 110              break;
 111      }
 112      imagedestroy($im);
 113  }
 114  
 115  function show_picture ($font, $text, $antialiasing, $size, $fgcolor, $fgtransparency, $bgcolor, $bgtransparency, $line_width, $format, $x=0, $y=0, $h=5, $rotation='default') 
 116  {
 117      // This function checks if the image with the above parameters has already been created and cached
 118      // If so, it creates a direct image link to the cached file (this should solve sooner problems with
 119      // some older browsers
 120      
 121      $md5 = md5($font.$text.$antialiasing.$size.$fgcolor.$fgtransparency.$bgcolor.$bgtransparency.$line_width.$format.$x.$y.$h.$rotation);
 122      $cachefile = CACHE_PREFIX . $md5 . '.' . $format;    
 123      
 124      if(!file_exists($cachefile)) {
 125          // Call the fontizer-script to generate the image. This will happen only one time
 126          create_picture($cachefile, $font, $text, $antialiasing, $size, $fgcolor, $fgtransparency, $bgcolor, $bgtransparency, $line_width, $format, $x, $y, $h, $rotation);
 127      }
 128      
 129      $displayfile    = DISPLAY_PREFIX . $md5 . '.' .$format;
 130      $text            = html_specialchars($text);
 131      
 132      $display_image_HxW = @getimagesize($cachefile);
 133      if($display_image_HxW) {
 134          $gt_replace  = '<img src="'.$displayfile.'" alt="'.$text.'" title="'.$text.'" '.$display_image_HxW[3].' border="0" />';
 135      } else {
 136          $gt_replace .= $text;
 137      }
 138      
 139      return $gt_replace;
 140  }
 141  
 142  function get_gt_by_style ($matches) {
 143      // This functions get the image per style {GT:style}Text{/GT}
 144      
 145      $style_name = $matches[1];
 146      $text        = $matches[2];
 147      
 148      global $gt;
 149      if(empty($gt)) {
 150          $gt = gt2array($GLOBALS['db']);
 151      }
 152      
 153      $a_href_before         = '';
 154      $a_href_end            = '';
 155      $result                = $text;
 156      
 157      
 158      $style_name = '"'.$style_name.'"';
 159      $style = isset($gt["styles_name"][$style_name]) ? $gt["styles_name"][$style_name] : false;
 160          
 161      if (!empty($style)) {    
 162          $font            = $gt["fonts_id"][$style["font"]]["filename"];
 163          $fgcolor        = $gt["colors_id"][$style["fgcolor"]]["value"];
 164          $bgcolor        = $gt["colors_id"][$style["bgcolor"]]["value"];
 165          
 166          if(is_file(PHPWCMS_ROOT.'/include/inc_module/mod_graphical_text/inc_fonts/'.$font)) {
 167          
 168              preg_match('/(<a[^>]*?>)(.*?)(<\/a>)/i', $text, $aparts);
 169              if(is_array($aparts) && count($aparts)) {
 170                  $a_href_before         = $aparts[1];
 171                  $text                 = $aparts[2];
 172                  $a_href_end            = $aparts[3];
 173              }
 174              $text    = stripslashes($text);
 175              $text    = str_replace('[br]', "\n", $text);
 176              $text    = str_replace(array('<br','<BR'), "\n<br", $text);
 177              $text    = strip_tags($text);
 178              $text    = decode_entities($text);
 179              $text    = cleanUpSpecialHtmlEntities($text);
 180  
 181              if(strpos($text, "\n")) {
 182                  $gt_splitted = explode("\n", $text);
 183              } else {
 184                  $gt_splitted = array($text);
 185              }
 186              
 187              $text = '';
 188              foreach($gt_splitted as $value) {
 189  
 190                  $value = rtrim($value);
 191                  
 192                  if(!empty($value)) {
 193                      $temp = show_picture(    $font,                         $value, 
 194                                              $style["antialiasing"],        $style["size"],
 195                                              $fgcolor,                    $style["fgtransparency"], 
 196                                              $bgcolor,                    $style["bgtransparency"],
 197                                              $style["line_width"],        $style["format"], 
 198                                              $style["start_x"],            $style["start_y"],
 199                                              $style["height"],            $style["rotation"]
 200                                              );
 201                      if($text && $temp) {
 202                          $text .= '<br />';
 203                      }
 204                      $text .= $temp;
 205                  }
 206              }
 207              
 208              if($text) $result = $text;
 209          }
 210      
 211      }
 212      
 213      return $a_href_before . $result . $a_href_end;
 214  }
 215  
 216  if(isset($content["all"]) && !(strpos($content["all"],'{GT')===false)) {
 217      // Style
 218      $content["all"] = preg_replace_callback('/\{GT:(.+?)\}(.*?)\{\/GT\}/is', 'get_gt_by_style', $content["all"]);
 219  }
 220  
 221  function better_imagettfbbox($size, $angle, $font, $text) {
 222      $dummy = imagecreate(1, 1);
 223      $black = imagecolorallocate($dummy, 0, 0, 0);
 224      $bbox = imagettftext($dummy, $size, $angle, 0, 0, $black, $font, $text);
 225      imagedestroy($dummy);
 226      return $bbox;
 227  }
 228  
 229  function rotateImage($image, $direction) {
 230      $direction    = strtolower($direction);
 231      $is_php51    = version_compare(PHP_VERSION, '5.1.0', '>=');
 232      switch($direction) {
 233          case 'cw':    $degrees = 270;
 234                      break;
 235          case 'ccw':    $degrees = 90;
 236                      break;
 237          case 'hcw':    return $is_php51 ? imagerotate($image, 180, 0, -1) : imagerotate($image, 180, 0);
 238                      break;
 239          default:    return $image;
 240      }
 241      $width            = imagesx($image);
 242      $height            = imagesy($image);
 243      $side            = $width > $height ? $width : $height;
 244      $imageSquare    = imagecreatetruecolor($side, $side);
 245      imagecopy($imageSquare, $image, 0, 0, 0, 0, $width, $height);
 246      imagedestroy($image);
 247      $imageSquare    = $is_php51 ? imagerotate($imageSquare, $degrees, 0, -1) : imagerotate($imageSquare, $degrees, 0);
 248      $image            = imagecreatetruecolor($height, $width);
 249      
 250      if($direction == 'cw') {
 251          $x = $height > $width ? 0 : $side - $height;
 252          $y = 0;
 253      } elseif($direction == 'ccw') {
 254          $x = 0;
 255          $y = $height < $width ? 0 : $side - $width;
 256      }
 257      imagecopy($image, $imageSquare, 0, 0, $x, $y, $height, $width);
 258      imagedestroy($imageSquare);
 259      return $image;
 260  }
 261  
 262  ?>


Generated: Wed Dec 30 05:55:15 2009 Cross-referenced by PHPXref 0.7