<?

class MagicButton
{
    
var 
$_template_dir;
var 
$_ttf_file;
var 
$_font_size;
var 
$_font_color;
var 
$_padding;
var 
$_offset_left;
var 
$_offset_top;
var 
$_format;
var 
$_cache_dir;
    
function 
MagicButton($template_dir$ttf_file$font_size$font_color$padding$offset_left 0$offset_top 0$format IMAGETYPE_GIF$cache_dir '')
{
    
$this->_template_dir $template_dir;
    
$this->_ttf_file $ttf_file;
    
$this->_font_size $font_size;
    
$this->_font_color $font_color;
    
$this->_padding $padding;
    
$this->_offset_left $offset_left;
    
$this->_offset_top $offset_top;
    
$this->_format $format;
    
$this->_cache_dir $cache_dir;
}

function 
_makeFileName($str)
{
    
$hash md5(basename($this->_template_dir) . basename($this->_ttf_file) . $this->_font_size $this->_font_color $this->_padding $this->_offset_left $this->_offset_top $this->_format $str);
    return 
$hash;
}

function 
_makeDirectoryName($str)
{
    
$dir substr($this->_makeFileName($str), 02);    
    return 
$dir;
}

function 
_getCachedFileName($str)
{
    return 
$this->_cache_dir '/' $this->_makeDirectoryName($str) . '/' $this->_makeFileName($str); 
}

function 
_writeImage($file_name)
{
    
$mime_str image_type_to_mime_type($this->_format);
    
header('Content-type: ' $mime_str);
    
readfile($file_name);
}

function 
_readImage($file_name)
{
  if (
$tmp getimagesize($file_name)) {
          
    list(
$width$height$type$attr) = $tmp;
    
    switch(
$type)
    {
      case 
IMAGETYPE_GIF:
        return 
imagecreatefromgif($file_name);
        break;  
      case 
IMAGETYPE_JPEG:
        return 
imagecreatefromjpeg($file_name);
        break;
      case 
IMAGETYPE_PNG:
        return 
imagecreatefrompng($file_name);  
        break;
      default:
        return 
false;  
    } 
  } else {
    return 
false;
  }       
}

function 
fixbbox($bbox)
{
   
$xcorr=0-$bbox[6]; //northwest X
   
$ycorr=0-$bbox[7]; //northwest Y
   
$tmp_bbox['left'] = $bbox[6]+$xcorr;
   
$tmp_bbox['top'] = $bbox[7]+$ycorr;
   
$tmp_bbox['width'] = $bbox[2]+$xcorr;
   
$tmp_bbox['height'] = $bbox[3]+$ycorr;
   
   return 
$tmp_bbox;
}

function 
_makeImage($text$output false)
{
    
$font_rgb $this->_hexToRGB($this->_font_color);
    
    
$fbox MagicButton::fixbbox(imagettfbbox($this->_font_size0$this->_ttf_file$text));
                
    
$left "$this->_template_dir/left.gif";
    
$right "$this->_template_dir/right.gif";
    
$v_tile "$this->_template_dir/vertical_tile.gif";
    
    
$left $this->_readImage($left);    
    
$right $this->_readImage($right);
    
$v_tile $this->_readImage($v_tile);

    
$left_w imagesx($left);
    
$right_w imagesx($right);    
    
$v_tile_h imagesy($v_tile);
    
    
$text_width $fbox['width'];
    
$text_height $fbox['height'];    
        
    
$total_width $text_width $left_w $right_w $this->_padding;
    
    
$image imagecreate($total_width$v_tile_h);
            
    
$font_color imagecolorallocate($image$font_rgb['R'], $font_rgb['G'], $font_rgb['B']);
    
    
$trans_color imagecolortransparent($left);

    
$t_color_rgb $this->_RGBToHex($trans_color);
    
    
$t_color_dec $this->_hexToRGB($t_color_rgb);
    
    
$trans_color_a imagecolorallocate($image$t_color_dec['R'], $t_color_dec['G'], $t_color_dec['B']);
    
    
imagefill($image00$trans_color_a);    
    
    
imagecolortransparent($image$trans_color_a);

    for (
$cnt $left_w$cnt $total_width $right_w$cnt++) {
        
imagecopy($image$v_tile$cnt0001$v_tile_h);
    }
    
    
imagecopy($image$left0000$left_w$v_tile_h);
    
imagecopy($image$right$total_width $right_w000$right_w$v_tile_h);
        
    
imagettftext($image$this->_font_size0$left_w $this->_padding/$this->_offset_left, ($v_tile_h 2) + ($text_height 2) + $this->_offset_top$font_color$this->_ttf_file$text);    

    
header('Content-type: ' image_type_to_mime_type($this->_format));
    
    
$the_file $this->_getCachedFileName($text);
    
    if (
$this->_cache_dir)
      @
mkdir(dirname($the_file), 0700true);
    
      switch(
$this->_format)
      {
          case 
IMAGETYPE_GIF:
            if (
$outputimagegif($image);
            if (
$this->_cache_dirimagegif($image$the_file);
            break;  
          case 
IMAGETYPE_JPEG:
            if (
$outputimagejpeg($image);
            if (
$this->_cache_dirimagejpeg($image$the_file);
            break;
          case 
IMAGETYPE_PNG:
            if (
$outputimagepng($image);
            if (
$this->_cache_dirimagepng($image$the_file);
            break;
          default:
            return 
false;
      } 
          
    
imagedestroy($image);
    return 
true;
}

function 
_RGBToHex($rgb)
{
  return 
dechex(($rgb >> 16) & 0xFF) . dechex(($rgb >> 8) & 0xFF) . dechex($rgb 0xFF);
}

function 
_hexToRGB($color)
{
    if (
$color[0] == '#')
      
$color substr($color1);
    
    
$ret = array();
    
    
$ret['R'] = hexdec(substr($color02));
    
$ret['G'] = hexdec(substr($color22));
    
$ret['B'] = hexdec(substr($color42));
    
    return 
$ret;
}

function 
_javascriptToHtmlEntities($text)
{
    
$matches null;
    
preg_match_all('/%u([0-9A-F]{4})/i'$text$matches);
    if (!empty(
$matches)) {
      for(
$i 0$i sizeof($matches[0]); $i++)
            
$text str_replace($matches[0][$i],'&#'.hexdec($matches[1][$i]).';'$text);
    }        
    return 
$text;
}

function 
writeText($str)
{
    
$the_file $this->_getCachedFileName($str);
    if (
file_exists($the_file)) {
      
$this->_writeImage($the_file);
    } else {
      
$this->_makeImage($strtrue);
    } 
}
    
}

?>