<?php
// CHXO labelgen -- converts a string to a PNG image, mostly to camoflage email addys on websites
// Homepage: http://chxo.com/berylium/software/labelgen.html
//
// 2003-04-14 -- initial version
// 2003-04-15 -- added GPL and showsource, no antialiasing at 10 pixels or less
// 2004-10-01 -- converted to our PHP version and modified to use TTF and JPEG output - Glenn Schiferl
// TODO:
//  i18n testing and a unicode font
//  image cache subsystem
//  convert to free fonts??? help!

/*
labelgen.php -- text-to-PNG-image converter using PHP
Copyright (C) 2003 by Chris Snyder (csnyder@chxo.com)

This program 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.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

// setup
$textval= $_REQUEST['textval'];
$domain= $_REQUEST['domain'];

// defaults
if ($textval=="") 
   $textval="<restricted>";
  
else
  {
    $temp="";
    for ($index=0; $index <strlen($textval); $index++) {
        $temp=$temp.chr(ord(substr($textval, $index, 1))-1);
    }
    $textval=$temp;
  };


if ($domain=="unsupplied") {
   $textval=$textval."@physics.ucsb.edu";
}

$size= 12;
$padding= 2;
$bgcolor= "f8f8f8";
$textcolor= "000000";

// setup pt. 2
$fontfile= "/home/httpd/html/People/THCN.TTF";

// geometry
$box= imagettfbbox( $size, 0, $fontfile, $textval);
$boxwidth= $box[4];
$boxheight= abs($box[3]) + abs($box[5]);
$width= $boxwidth + ($padding*2) + 1;
$height= $boxheight + ($padding*2) + 0;
$textx= $padding;
$texty= ($boxheight - abs($box[3])) + $padding;
//print "$width x $height ($box[4] x ($box[3] - $box[5]))"; exit;

// create the image
$im= imagecreate($width, $height);

// colors
function mkcolor($color){
    // courtesy simon: http://www.php.net/manual/en/function.imagecolorallocate.php#19576
    global $im;
    $color = str_replace("#","",$color);
    $red = hexdec(substr($color,0,2));
    $green = hexdec(substr($color,2,2));
    $blue = hexdec(substr($color,4,2));
    $out = imagecolorallocate($im, $red, $green, $blue);
    return($out);
    }
$bg= mkcolor($bgcolor);
$tx= mkcolor($textcolor);


// add text
  imageTTFText( $im, $size, 0, $textx, $texty, $tx, $fontfile, $textval);


// send the image
    header("content-type: image/jpeg");
    imagejpeg($im);
    imagedestroy($im);

?>

