Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Lynezx - 22/04/2014
- lynezx.transformice@gmail.com
- http://area801.com/
- */
- $_Fonts = Array();
- putenv('GDFONTPATH=' . realpath('.'));
- function putFont($name, $file) {
- global $_Fonts;
- if (!file_exists($file)) {
- throw new Exception('File "' . $file . '" not found.');
- }
- $_Fonts[$name] = $file;
- }
- putFont('Soopafresh' ,'fonts/soopafre.ttf');
- putFont('Verdana' ,'fonts/verdana.ttf');
- putFont('Verdana Bold' ,'fonts/verdanab.ttf');
- putFont('Segoe UI' ,'fonts/segoeui.ttf');
- putFont('Segoe UI Bold' ,'fonts/segoeuib.ttf');
- function hexToRgb($hex) {
- $hex = str_replace("#", "", $hex);
- if(strlen($hex) == 3) {
- $r = hexdec(substr($hex,0,1).substr($hex,0,1));
- $g = hexdec(substr($hex,1,1).substr($hex,1,1));
- $b = hexdec(substr($hex,2,1).substr($hex,2,1));
- } else {
- $r = hexdec(substr($hex,0,2));
- $g = hexdec(substr($hex,2,2));
- $b = hexdec(substr($hex,4,2));
- }
- $rgb = array($r, $g, $b);
- return $rgb;
- }
- function fonteArquivo($nome) {
- global $_Fonts;
- return $_Fonts[$nome];
- }
- function colorirTexto($imagem, $rgb) {
- return imagecolorallocate($imagem, $rgb[0], $rgb[1], $rgb[2]);
- }
- function fonteAltura($fonte, $tamanho) {
- $arSize = imagettfbbox($tamanho, 0, fonteArquivo($fonte), 'p');
- return abs($arSize[7] - $arSize[1]);
- }
- function fonteLargura($fonte, $tamanho, $text) {
- $arSize = imagettfbbox($tamanho, 0, fonteArquivo($fonte), $text);
- return abs($arSize[2] - $arSize[0]);
- }
- class Line {
- public $content;
- public $marginLeft = 0;
- public $marginTop = 0;
- public function __construct() {
- $this->content = Array();
- }
- public function addText($text, $fontName, $fontSize, $fontColor, $marginLeft = 0, $marginTop = 0) {
- $this->content[] = (object) Array('text' => $text, 'font' => (object) Array('name' => $fontName, 'size' => $fontSize, 'color' => $fontColor), 'incX' => $marginLeft, 'incY' => $marginTop);
- }
- public function addLineBreak() {
- $this->content[] = 'linebreak';
- }
- }
- class Image {
- public $posX;
- public $poxY;
- private $img;
- private $font;
- private $height;
- private $width;
- public function __construct($height, $width, $bg = 'alpha') {
- $this->posX = 2;
- $this->posY = 0;
- $this->img = imagecreate($width, $height);
- $this->height = $height;
- $this->width = $width;
- if ($bg == 'alpha') {
- imagecolorallocatealpha($this->img, 0, 0, 0, 0);
- } else {
- $rgb = hexToRgb($bg);
- imagecolorallocate($this->img, $rgb[0], $rgb[1], $rgb[2]);
- }
- $this->font = (object) Array();
- }
- public function jumpLine($increment = null) {
- if ($increment) {
- $this->posY = $this->posY + $increment;
- } else {
- $this->posY = $this->posY + fonteAltura($this->font->name, $this->font->size);
- }
- }
- public function drawTextAt($x, $y, $text) {
- $charHeight = fonteAltura($this->font->name, $this->font->size);
- $charWidth = fonteLargura($this->font->name, $this->font->size, 'p');
- $textWidth = fonteLargura($this->font->name, $this->font->size, $text);
- if ($y <= 2) {
- $y = $charHeight + $y;
- }
- if ($this->posY <= 2) {
- $this->posY = $charHeight + $this->posY;
- }
- if ($x + $textWidth > $this->width) {
- $this->posY = $y + 2;
- $this->jumpLine();
- $y = $this->posY;
- $x = 2;
- }
- if ($textWidth > $this->width) {
- $text = wordwrap($text, round($this->width / $textWidth));
- }
- imagettftext($this->img, $this->font->size, 0, $x, $y, colorirTexto($this->img, $this->font->color), fonteArquivo($this->font->name), $text);
- }
- public function setFont($name, $size, $color) {
- global $_Fonts;
- if (!isset($_Fonts[$name])) {
- throw new Exception('Unknow font "' . $name . '". Please use putFont(fontName, fileName).');
- }
- $this->font->color = hexToRgb($color);
- $this->font->name = $name;
- $this->font->size = $size;
- }
- public function drawTitle($title) {
- $x = round(($this->width / 2) - (fonteLargura($this->font->name, $this->font->size, $title) / 2));
- $y = 2;
- $this->drawTextAt($x, $y, $title);
- }
- public function drawLine($line) {
- $x = 2;
- $y = $this->posY + fonteAltura($this->font->name, $this->font->size, 'p') + 2;
- $y = $y + $line->marginTop;
- $x = $x + $line->marginLeft;
- foreach($line->content as $item) {
- if ($item == 'linebreak') {
- $this->posY = $y + 2;
- $this->jumpLine();
- $y = $this->posY;
- $x = $line->marginLeft;
- continue;
- }
- $this->setFont($item->font->name, $item->font->size, $item->font->color);
- $y = $y + $item->incY;
- $x = $x + $item->incX;
- $this->drawTextAt($x, $y, $item->text);
- $x = $x + fonteLargura($this->font->name, $this->font->size, $item->text);
- }
- $this->posY = $y;
- }
- public function getImg() {
- return $this->img;
- }
- public function flushImg($format = 'png') {
- header('Content-type: image/' . $format);
- switch($format) {
- case 'png':
- imagepng($this->img);
- break;
- case 'jpeg':
- imagejpeg($this->img);
- break;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement