<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


// this scripts requires a 'includes/fonts' folder in the same directory, containing one or more .ttf file(s)
// Define content-type
header("Content-type: image/png");

class MyColor {
  var $red;
  var $green;
  var $blue;

  function MyColor($color) {
    if (substr($color, 0, 1) == "#") {
      $color = substr($color, 1, 6);
    }

    $this->red    = hexdec(substr($color, 0, 2));
    $this->green  = hexdec(substr($color, 2, 2));
    $this->blue   = hexdec(substr($color, 4, 2));
  }

  function getRed() {
    return $this->red;
  }

  function getGreen() {
    return $this->green;
  }

  function getBlue() {
    return $this->blue;
  }
}

/**
 * GET parameters :
 *
 * text_color   xxxxxx (color hexa value without the #) the color of the text
 * bg_color     xxxxxx (color hexa value without the #) the color of the background
 * shadow_color xxxxxx (color hexa value without the #) the color of the text shadow
 * transparent  1 | 0   if the background is transparent or not
 * shadow       1 | 0   if the text shadow must be displayed
 * text_size         size of the text in px or pt (depending on the version of GD)
 * text         text to display
 * width        width of the resulting image
 * height       height of the resulting image
 * left         position of the begining of the text from the left side of the image
 * top          position of the bottom of the text from the top of the image
 * font         font to use (correspondong .ttf file must be in ./includes/fonts folder)
 */

// sample query string : display_text.php?text_color=ff0000&bg_color=ffffff&shadow_color=ffffff&transparent=1&shadow=0&text_size=20&text=hello world&width=200&height=25&font=Dakota.ttf

// pre-processing : rebuild the GET array (detect html entities)
$queryString = $_SERVER['QUERY_STRING'];

$htmlSpecialChars = array("&eacute;", "&egrave;", "&ecirc;", "&euml;", "&ograve;", "&ugrave;", "&agrave;", "&acirc;", "&auml;", "&ccedil;", "&icirc;", "&iuml;", "&ocirc;", "'", "!", "?", " ");
$urlSpecialChars  = array("%E9", "%E8", "%EA", "%EB", "%F2", "%F9", "%E0", "%E2", "%E4", "%E7", "%EE", "%EF", "%F4", "%B4", "%21", "%3F", "%20");

$queryString = str_replace($htmlSpecialChars, $urlSpecialChars, $queryString);

$queryArray = explode("&", $queryString);

unset($_GET);
foreach($queryArray as $assign) {
	list($variable, $value) = explode("=", $assign);
	$_GET[$variable] = $value;
}

if(isset($_GET['text_color'])) {
  $textColor = $_GET['text_color'];
}
else {
  // default text color
  $textColor = "#000000";
}

if(isset($_GET['bg_color'])) {
  $bgColor = $_GET['bg_color'];
}
else {
  // default background color
  $bgColor = "#808080";
}

if(isset($_GET['shadow_color'])) {
  $shadowColor = $_GET['shadow_color'];
}
else {
  // default text shadow color
  $shadowColor = "#000000";
}

if(isset($_GET['transparent'])) {
  $transparent = (bool) $_GET['transparent'];
}
else {
  $transparent = true;
}

if(isset($_GET['shadow'])) {
  $displayShadow = (bool) $_GET['shadow'];
}
else {
  $displayShadow = false;
}

if(isset($_GET['text_size'])) {
  $size = $_GET['text_size'];
}
else {
  // default text size
  $size = 12;
}

if(isset($_GET['text'])) {
  $text = urldecode($_GET['text']);
}
else {
  $text = '';
}

if(isset($_GET['height'])) {
  $height = $_GET['height'];
}
else {
  // default height
  $height = 20;
}

if(isset($_GET['width'])) {
  $width = $_GET['width'];
}
else {
  // default width
  $width = 150;
}

if(isset($_GET['left'])) {
  $left = $_GET['left'];
}
else {
  // default left text position
  $left = 10;
}

if(isset($_GET['top'])) {
  $top = $_GET['top'];
}
else {
  // default top text position
  $top = ($height/2)+($size/2);
}

if(isset($_GET['font'])) {
	if(stristr($_GET['font'],'.ttf')) {
		$font = substr($_GET['font'], 0, strpos($_GET['font'],'.'));
	}
	else {
		$font = $_GET['font'];
	}
}
else {
  // default font name;
  $font = 'arial';
}



$fileName = "./Repositories/Cache/".md5($textColor.$bgColor.$shadowColor.$transparent.$displayShadow.$size.$text.$height.$width.$left.$top.$font).".png";

// create the image
if(!is_file($fileName)) {
  // the directory where the fonts reside
  $folder = dir(realpath('./includes/fonts'));

  while($fontFile = $folder->read()) {
    if(stristr($fontFile,'.ttf')) {
      $fontName = substr($fontFile,0,strpos($fontFile,'.'));
      if(!strcasecmp($fontName, $font)) {
        $font = $fontName;
        break;
      }
    }
  }
  if(!$fontFile) {
    // font file not found : exit
    die();
  }

  $folder->close();

  putenv('GDFONTPATH='.realpath('./includes/fonts'));

  // image object creation
  $im = imagecreatetruecolor($width, $height);

  // translation of colors
  $objTextColor   = new MyColor($textColor);
  $objShadowColor = new MyColor($shadowColor);
  $objBgColor     = new MyColor($bgColor);

  $txtImgAlloc    = imagecolorallocate($im, $objTextColor->getRed(), $objTextColor->getGreen(), $objTextColor->getBlue());
  $shadowImgAlloc = imagecolorallocate($im, $objShadowColor->getRed(), $objShadowColor->getGreen(), $objShadowColor->getBlue());
  $bgImgAlloc     = imagecolorallocate($im, $objBgColor->getRed(), $objBgColor->getGreen(), $objBgColor->getBlue());

  // create the transparent background
  imagefilledrectangle($im, 0, 0, $width-1, $height-1, $bgImgAlloc);
  if($transparent) {
    imagecolortransparent($im, $bgImgAlloc);
  }

  // Add the text
  if($displayShadow) {
    imagettftext($im, $size, 0, $left+1, $top+1, $shadowImgAlloc, $font, $text);
    imagettftext($im, $size, 0, $left+2, $top+2, $shadowImgAlloc, $font, $text);
  }
  imagettftext($im, $size, 0, $left, $top, $txtImgAlloc, $font, $text);

  imagepng($im, $fileName);
  imagedestroy($im);
}

print(file_get_contents($fileName));
?>