Advertisement
Lynezx

PHP Texto em imagem

Mar 22nd, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.09 KB | None | 0 0
  1. <?php
  2.     /*
  3.         Lynezx - 22/04/2014
  4.         lynezx.transformice@gmail.com
  5.         http://area801.com/
  6.     */
  7.    
  8.     $_Fonts = Array();
  9.     putenv('GDFONTPATH=' . realpath('.'));
  10.    
  11.     function putFont($name, $file) {
  12.         global $_Fonts;
  13.         if (!file_exists($file)) {
  14.             throw new Exception('File "' . $file . '" not found.');
  15.         }
  16.         $_Fonts[$name] = $file;
  17.     }
  18.    
  19.     putFont('Soopafresh' ,'fonts/soopafre.ttf');
  20.     putFont('Verdana' ,'fonts/verdana.ttf');
  21.     putFont('Verdana Bold' ,'fonts/verdanab.ttf');
  22.     putFont('Segoe UI' ,'fonts/segoeui.ttf');
  23.     putFont('Segoe UI Bold' ,'fonts/segoeuib.ttf');
  24.  
  25.     function hexToRgb($hex) {
  26.         $hex = str_replace("#", "", $hex);
  27.  
  28.         if(strlen($hex) == 3) {
  29.             $r = hexdec(substr($hex,0,1).substr($hex,0,1));
  30.             $g = hexdec(substr($hex,1,1).substr($hex,1,1));
  31.             $b = hexdec(substr($hex,2,1).substr($hex,2,1));
  32.         } else {
  33.             $r = hexdec(substr($hex,0,2));
  34.             $g = hexdec(substr($hex,2,2));
  35.             $b = hexdec(substr($hex,4,2));
  36.         }
  37.         $rgb = array($r, $g, $b);
  38.         return $rgb;
  39.     }
  40.    
  41.     function fonteArquivo($nome) {
  42.         global $_Fonts;
  43.         return $_Fonts[$nome];
  44.     }
  45.    
  46.     function colorirTexto($imagem, $rgb) {
  47.         return imagecolorallocate($imagem, $rgb[0], $rgb[1], $rgb[2]);
  48.     }
  49.  
  50.     function fonteAltura($fonte, $tamanho) {
  51.         $arSize = imagettfbbox($tamanho, 0, fonteArquivo($fonte), 'p');
  52.         return abs($arSize[7] - $arSize[1]);
  53.     }
  54.  
  55.     function fonteLargura($fonte, $tamanho, $text) {
  56.         $arSize = imagettfbbox($tamanho, 0, fonteArquivo($fonte), $text);
  57.         return abs($arSize[2] - $arSize[0]);
  58.     }
  59.    
  60.     class Line {
  61.         public $content;
  62.         public $marginLeft = 0;
  63.         public $marginTop = 0;
  64.        
  65.         public function __construct() {
  66.             $this->content = Array();
  67.         }
  68.        
  69.         public function addText($text, $fontName, $fontSize, $fontColor, $marginLeft = 0, $marginTop = 0) {
  70.             $this->content[] = (object) Array('text' => $text, 'font' => (object) Array('name' => $fontName, 'size' => $fontSize, 'color' => $fontColor), 'incX' => $marginLeft, 'incY' => $marginTop);
  71.         }
  72.        
  73.         public function addLineBreak() {
  74.             $this->content[] = 'linebreak';
  75.         }
  76.     }
  77.    
  78.     class Image {
  79.         public $posX;
  80.         public $poxY;
  81.         private $img;
  82.         private $font;
  83.         private $height;
  84.         private $width;
  85.        
  86.         public function __construct($height, $width, $bg = 'alpha') {
  87.             $this->posX = 2;
  88.             $this->posY = 0;
  89.             $this->img = imagecreate($width, $height);
  90.             $this->height = $height;
  91.             $this->width = $width;
  92.             if ($bg == 'alpha') {
  93.                 imagecolorallocatealpha($this->img, 0, 0, 0, 0);
  94.             } else {
  95.                 $rgb = hexToRgb($bg);
  96.                 imagecolorallocate($this->img, $rgb[0], $rgb[1], $rgb[2]);
  97.             }
  98.             $this->font = (object) Array();
  99.         }
  100.        
  101.         public function jumpLine($increment = null) {
  102.             if ($increment) {
  103.                 $this->posY = $this->posY + $increment;
  104.             } else {
  105.                 $this->posY = $this->posY + fonteAltura($this->font->name, $this->font->size);
  106.             }
  107.         }
  108.        
  109.         public function drawTextAt($x, $y, $text) {
  110.             $charHeight = fonteAltura($this->font->name, $this->font->size);
  111.             $charWidth = fonteLargura($this->font->name, $this->font->size, 'p');
  112.             $textWidth = fonteLargura($this->font->name, $this->font->size, $text);
  113.             if ($y <= 2) {
  114.                 $y = $charHeight + $y;
  115.             }
  116.             if ($this->posY <= 2) {
  117.                 $this->posY = $charHeight + $this->posY;
  118.             }
  119.             if ($x + $textWidth > $this->width) {
  120.                 $this->posY = $y + 2;
  121.                 $this->jumpLine();
  122.                 $y = $this->posY;
  123.                 $x = 2;
  124.             }
  125.             if ($textWidth > $this->width) {
  126.                 $text = wordwrap($text, round($this->width / $textWidth));
  127.             }
  128.             imagettftext($this->img, $this->font->size, 0, $x, $y, colorirTexto($this->img, $this->font->color), fonteArquivo($this->font->name), $text);
  129.         }
  130.        
  131.         public function setFont($name, $size, $color) {
  132.             global $_Fonts;
  133.             if (!isset($_Fonts[$name])) {
  134.                 throw new Exception('Unknow font "' . $name . '". Please use putFont(fontName, fileName).');
  135.             }
  136.             $this->font->color = hexToRgb($color);
  137.             $this->font->name = $name;
  138.             $this->font->size = $size;
  139.         }
  140.        
  141.         public function drawTitle($title) {
  142.             $x = round(($this->width / 2) - (fonteLargura($this->font->name, $this->font->size, $title) / 2));
  143.             $y = 2;
  144.             $this->drawTextAt($x, $y, $title);
  145.         }
  146.        
  147.         public function drawLine($line) {
  148.             $x = 2;
  149.             $y = $this->posY + fonteAltura($this->font->name, $this->font->size, 'p') + 2;
  150.             $y = $y + $line->marginTop;
  151.             $x = $x + $line->marginLeft;
  152.             foreach($line->content as $item) {
  153.                 if ($item == 'linebreak') {
  154.                     $this->posY = $y + 2;
  155.                     $this->jumpLine();
  156.                     $y = $this->posY;
  157.                     $x = $line->marginLeft;
  158.                     continue;
  159.                 }
  160.                 $this->setFont($item->font->name, $item->font->size, $item->font->color);
  161.                 $y = $y + $item->incY;
  162.                 $x = $x + $item->incX;
  163.                 $this->drawTextAt($x, $y, $item->text);
  164.                 $x = $x + fonteLargura($this->font->name, $this->font->size, $item->text);
  165.             }
  166.             $this->posY = $y;
  167.         }
  168.        
  169.         public function getImg() {
  170.             return $this->img;
  171.         }
  172.        
  173.         public function flushImg($format = 'png') {
  174.             header('Content-type: image/' . $format);
  175.             switch($format) {
  176.                 case 'png':
  177.                     imagepng($this->img);
  178.                     break;
  179.                 case 'jpeg':
  180.                     imagejpeg($this->img);
  181.                     break;
  182.             }
  183.         }
  184.     }
  185. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement