<?
/***
* This script is a part of txtimage package
*
* Param syntax:
    text        string text to draw
        OR (only if text is empty)
        base_text - base64_encoded text to draw
    face         arial|times|frutiger|verdata
    bold         0|1
    italic         0|1
    font_size    int size pixels
    width        int image width pixels
    height        int image height pixels
    transparent    yes|no
    font_color    RGB
    bg_color    RGB
    valign        bottom|top
    align        left|center|right
    bottom        int pixels from bottom
    top        int pixels from top
    left        int pixels from left
    right        int pixels from right
    alias        off - turn off antialisasing
* 
* @author Reigo KÜngas <reigo -AT- reigo.net>
* @copyright (c) 2003 Reigo Küngas. All rights reserved.
**/
    
if (!empty($_REQUEST["help"]))
    {
        
show_source(__FILE__);
        exit;
    }
    if (!
extension_loaded("gd"))
        
dl("gd.so");

    if (!empty(
$_REQUEST["debug"])) define("debug","1");

    if (
defined("debug"))
        
error_reporting(E_ALL);
    else
        
error_reporting(E_ERROR);


    if (!
defined("debug"))
    {
        
Header("Expires: ".gmdate("D, d M Y H:i:s",time()+10*3600)." GMT");
        
Header("Cache-Control: max-stale");
        
Header("Pragma: max-stale");
    }

    
/** check gd version and gif supprt **/
/**    if (function_exists("imagegif"))
        Header("Content-type: image/gif");
    else**/
        
Header("Content-type: image/png");

    if (
defined("debug"))
        
Header("Content-Type: text/plain");
    
/**
    * Definitions
    **/
    
define("fonts_dir",dirname(__FILE__)."/fonts/");
    
$fonts = array(
        
"arial", "times", "verdana", "frutiger",
        
"clarendon","tahoma","danube",
        );
    
$font_type = array(
        
"ttf"=>array("arial","times","verdana","clarendon","tahoma","danube"),
        );

    
$myfont = fonts_dir."frutiger/frutiger_";
    
$type = "ttf";

    if (empty(
$GLOBALS["face"]) && !empty($_REQUEST["face"]))
        
$GLOBALS["face"] = $_REQUEST["face"];
    if (!empty(
$GLOBALS["face"]) && in_array($GLOBALS["face"],$fonts))
    {
        if (
in_array($GLOBALS["face"],$font_type["ttf"]))
            
$type = "ttf";
        
$myfont = fonts_dir.$GLOBALS["face"]."/".$GLOBALS["face"]."_";
    }
    

    if (!empty(
$_REQUEST["bold"]) || !empty($GLOBALS["bold"])) $myfont .= "b";
    if (!empty(
$_REQUEST["italic"]) || !empty($GLOBALS["italic"])) $myfont .= "i";
    
$myfont .= ".".$type;

    
/***
    * SIZE
    ***/
    
$myfont_size = 16;
    
    if (!empty(
$GLOBALS["font_size"])) $myfont_size = intval($GLOBALS["font_size"]);
    else if (!empty(
$_REQUEST["font_size"])) $myfont_size = intval($_REQUEST["font_size"]);

    
/***
    * COLOR
    ***/
    
$myr = false;
    
$myg = false;
    
$myb = false;
    if (!empty(
$GLOBALS["font_color"]))
    {
        list(
$myr,$myg,$myb) = explode(",",$GLOBALS["font_color"]);

    }else if (!empty(
$_REQUEST["font_color"]))
    {
        list(
$myr,$myg,$myb) = explode(",",$_REQUEST["font_color"]);
    }
    
## DEFAULT 163586
    
if (is_bool($myr) && $myr == false) $myr = 22;
    if (
is_bool($myg) && $myg == false) $myg = 53;
    if (
is_bool($myb) && $myb == false) $myb = 134;

    
/***
    * Antialias
    **/
    
if (empty($_REQUEST["alias"]) && empty($GLOBALS["alias"])) $alias = "on";

    
/***
    * BACKGROUND COLOR
    ***/
    
$mybgr = false;
    
$mybgg = false;
    
$mybgb = false;
    if (!empty(
$GLOBALS["bg_color"]))
    {
        list(
$mybgr,$mybgg,$mybgb) = explode(",",$GLOBALS["bg_color"]);
    }else if (!empty(
$_REQUEST["bg_color"]))
    {
        list(
$mybgr,$mybgg,$mybgb) = explode(",",$_REQUEST["bg_color"]);
    }

    
## DEFAULT 163586
    
if (is_bool($mybgr) && $mybgr == false) $mybgr = 255;
    if (
is_bool($mybgg) && $mybgg == false) $mybgg = 255;
    if (
is_bool($mybgb) && $mybgb == false) $mybgb = 255;

    
/***
    * No align by default
    ***/
    
$myvalign = false;
    if (!empty(
$_REQUEST["valign"]))
        
$myvalign = $_REQUEST["valign"];
    
$myalign = false;
    if (!empty(
$_REQUEST["align"]))
        
$myalign = $_REQUEST["align"];

    
/** pixels from top, bottom, left and right **/
    
$mytop = false;
    
$mybottom = false;
    
$myleft = false;
    
$myright = false;

    if (!empty(
$GLOBALS["bottom"]))
        
$mybottom = $GLOBALS["bottom"];
    else if (!empty(
$_REQUEST["bottom"]))
        
$mybottom = $_REQUEST["bottom"];

    if (!empty(
$GLOBALS["top"]))
        
$mytop = $GLOBALS["top"];
    else if (!empty(
$_REQUEST["top"]))
        
$mytop = $_REQUEST["top"];

    if (!empty(
$GLOBALS["left"]))
        
$myleft = $GLOBALS["left"];
    else if (!empty(
$_REQUEST["left"]))
        
$myleft = $_REQUEST["left"];

    if (!empty(
$GLOBALS["right"]))
        
$myright = $GLOBALS["right"];
    else if (!empty(
$_REQUEST["right"]))
        
$myright = $_REQUEST["right"];

    
$mytext = "";
    
    if (!empty(
$GLOBALS["text"]) && chop($GLOBALS["text"]) != "") $mytext = $GLOBALS["text"];
    else if (!empty(
$_REQUEST["text"]) && chop($_REQUEST["text"]) != "") $mytext = $_REQUEST["text"];
    else if (!empty(
$_REQUEST["base_text"]))
    {
        
$tmp = base64_decode($_REQUEST["base_text"]);
        if (
chop($tmp) != "") 
            
$mytext = $tmp;
    }

    
/***
    * Font def
    *********************************************/

    /********************************************
    * Image def
    ***/
    
$mywidth = false;
    
$myheight = false;
    if (!empty(
$GLOBALS["width"])) $mywidth = intval($GLOBALS["width"]);
    else if (!empty(
$_REQUEST["width"])) $mywidth = intval($_REQUEST["width"]);
    if (!empty(
$GLOBALS["height"])) $myheight = intval($GLOBALS["height"]);
    else if (!empty(
$_REQUEST["height"])) $myheight = intval($_REQUEST["height"]);
    
    
/****
    * TRANSPARENCY 
    *****/
    
$mytrans = 1;
    if (!empty(
$_REQUEST["transparent"]))
    {
        if (
$_REQUEST["transparent"] == "no") $mytrans = 0;
        if (
$_REQUEST["transparent"] == "yes") $mytrans = 1;
    }

    
/***
    * Image def
    ********************************************/

    
$font = $myfont;
    

    
/***
    * Truetype font stuff
    ***/
    
if ($type == "ttf")
    {
        if (!empty(
$mytext))
        {
            
$info = imagettfbbox($myfont_size,0,$font,$mytext);

            if (
defined("debug"))
            {
                echo 
"** Mida meil info sisaldab
            0    lower left corner, X position
            1    lower left corner, Y position
            2    lower right corner, X position
            3    lower right corner, Y position
            4    upper right corner, X position
            5    upper right corner, Y position
            6    upper left corner, X position
            7    upper left corner, Y position

            x,y    

            6,7       4,5
            -------------
            |        |
            |        |
            -------------
            0,1       2,3

            text=Shahh&font_size=16&bold=1&font_color=190,95,85
            Array
            (
                [0] => -1
                [1] => -1
                [2] => 60
                [3] => -1
                [4] => 60
                [5] => -16
                [6] => -1
                [7] => -16
            )
            **"
;
            
print_r($info);
            }
            
        }else
            
$info = array(1,1,0,0,2,0,0,2);

        
$xstart = $info[0];
        
$ystart = abs($info[5]);
        
$real_width = abs($info[0])+ abs($info[2])+1;
        
$real_height = abs($info[7])+abs($info[1]);
        
        if (
$mywidth === false || $mywidth < $real_width) 
            
$mywidth = $real_width;

        if (
$myheight === false || $myheight < $real_height) 
            
$myheight = $real_height;

        
myalign($xstart,$ystart,$mywidth,$myheight,
            array(
                
"rwidth"=>$real_width,
                
"rheight"=>$real_height,
                
"align"=>$myalign,
                
"valign"=>$myvalign,
                
"top"=>$mytop,
                
"bottom"=>$mybottom,
                
"left"=>$myleft,
                
"right"=>$myright,
                )
            );

        list(
$im,$bgcolor,$text_color) = myCreateImage(array(
                        
"width"=>$mywidth,
                        
"height"=>$myheight,
                        
"real_width"=>$real_width,
                        
"real_height"=>$real_height,
                        
"align"=>$myalign,
                        
"valign"=>$myvalign,
                        
"fgr"=>$myr,
                        
"fgg"=>$myg,
                        
"fgb"=>$myb,
                        
"bgr"=>$mybgr,
                        
"bgg"=>$mybgg,
                        
"bgb"=>$mybgb,
                        
"transparent"=>$mytrans,
                        
"alias"=>$alias,
                        )
                    );

        
// antialiasing
        
if ($alias == "off") $text_color = 0 - $text_color;

        if (!empty(
$mytext))
            
imagettftext ($im, $myfont_size, 0, $xstart,$ystart, $text_color,$font, $mytext);

    
/***
    * ps font stuff deprecated
    ***/
    
}else
    {
        
$font = imagepsloadfont($myfont);
        
imagepsencodefont($font,"PSLatin1.enc");

        if (!empty(
$mytext))
        {
            
$info = imagepsbbox($mytext,$font,$myfont_size);
            if (
defined("debug"))
            {
                echo 
"** Mida meil info sisaldab
            0    lower left x-coordinate
            1    lower left y-coordinate
            2    upper right x-coordinate
            3    upper right y-coordinate

            x,y    
                   2,3
            -------------
            |        |
            |        |
            -------------
            0,1

            text=Shahh&font_size=16
            Array
            (
                [0] => 1
                [1] => 0
                [2] => 42
                [3] => 12
            )
            Coordinates starting:
                x upper left corner
                y upper left corner
            **"
;
            
print_r($info);
            }
        }else
            
$info = array(0,0,1,1);

        
$xstart = 0;
        
$ystart = abs($info[3])-1;
        
$real_width = abs($info[2]);
        
$real_height = abs($info[3])+abs($info[1]);

        if (
$mywidth === false || $mywidth < abs($info[2])) 
            
$mywidth = $real_width;
            
        if (
$myheight === false || $myheight < abs($info[3])+abs($info[1]))
            
$myheight = $real_height;

        
myalign($xstart,$ystart,$mywidth,$myheight,
            array(
                
"rwidth"=>$real_width,
                
"rheight"=>$real_height,
                
"align"=>$myalign,
                
"valign"=>$myvalign,
                
"top"=>$mytop,
                
"bottom"=>$mybottom,
                
"left"=>$myleft,
                
"right"=>$myright,
                )
            );

        list(
$im,$bgcolor,$text_color) = myCreateImage(array(
                        
"width"=>$mywidth,
                        
"height"=>$myheight,
                        
"fgr"=>$myr,
                        
"fgg"=>$myg,
                        
"fgb"=>$myb,
                        
"bgr"=>$mybgr,
                        
"bgg"=>$mybgg,
                        
"bgb"=>$mybgb,
                        
"transparent"=>$mytrans,
                        )
                    );


        if (!empty(
$mytext))
            
imagepstext ($im, $mytext, $font, $myfont_size, $text_color,$bgcolor, $xstart,$ystart+1);
        
imagepsfreefont($font);
    }
    
/**    if (function_exists("imagegif"))
        imagegif ($im);
    else**/
        
imagepng ($im);
    
imagedestroy($im);
    exit;

/***
* Create image
*
* par = array(
    width
    height
    fgr
    fgg
    fgb
    bgr
    bgg
    bgb
    transparent
    )
**/
function myCreateImage($par=array())
{
    
extract($par,EXTR_OVERWRITE);

    
$im = imagecreate ($width, $height);

    
$bgcolor = imagecolorallocate ($im, $bgr,$bgg,$bgb);

    
/*** transparent ***/
    
if (!empty($transparent))
        
imagecolortransparent($im,$bgcolor);

    
$text_color = imagecolorallocate ($im, $fgr, $fgg, $fgb);
    return(array(
$im,$bgcolor,$text_color));
}

/***
* Paneb siis pildi paika
***/
function myalign(&$xstart,&$ystart,&$width,&$height,$par=array())
{
    if (!empty(
$par))
        
extract($par,EXTR_OVERWRITE);


    
/** left | center | right **/
    
if ($align !== false && $width >= $rwidth)
    {
        if (
$align == "center")
            
$xstart = $xstart + intval((abs($width)-abs($rwidth))/2);
        else if (
$align == "right")
            
$xstart = $xstart + abs(abs($width)-abs($rwidth));
    }

    
/** top | middle | bottom **/
    
if ($valign !== false && $height > $rheight)
    {
        if (
$valign == "middle")
            
$ystart = $ystart + intval((abs($height)-abs($rheight))/2);
        else if (
$valign == "bottom")
            
$ystart = $ystart + abs(abs($height)-abs($rheight));
    }

    if (
$left !== false)
    {
        
$width += $left;
        
$xstart += $left;
    }
    if (
$right !== false)
    {
        
$width += $right;
    }

    if (
$top !== false)
    {
        
$height += $top;
        
$ystart += $top;
    }
    if (
$bottom !== false)
    {
        
$height += $bottom;
    }

}
?>