<?php
function macro_EMailAddress($formatter, $value, $options='')
{
    global $DBInfo;

    $out = "";
    $prefix = $DBInfo->url_prefix."/";

    if ($value)
    {
        $dir = $DBInfo->upload_dir;
        if (!file_exists($dir))
            mkdir($dir, 0777);

        $dir = $dir."/EMailAddressMacro";
        if (!file_exists($dir))
            mkdir($dir, 0777);

        $filename = md5($value).".png";
        $filepath = $dir."/".$filename;
        if (!file_exists($filepath))
        {
            $fontsize = 10;
            $font = "gulim.ttc";
            $angle = 0;

            $size = imageftbbox($fontsize, $angle, $font, $value);

            if ($angle > 0)
            {
                $width = $size[2] - $size[6] + 1;
                $height = $size[1] - $size[5] + 1;
                $sx = -$size[6];
                $sy = -$size[5];
            }
            else
            {
                $width = $size[4] - $size[0] + 1;
                $height = $size[3] - $size[7] + 1;
                $sx = -$size[0];
                $sy = -$size[7];
            }

            $im = ImageCreate ($width, $height);
            $white = imagecolorallocate($im, 255, 255, 255);
            $black = imagecolorallocate($im, 0, 0, 0);
            imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $white);
            imagefttext($im, $fontsize, $angle, $sx, $sy, $black, $font, $value);

            imagecolortransparent($im, $white);
            ImagePng($im, $filepath);
            ImageDestroy($im);
        }

        $filepath = $prefix.$filepath;
        $out = "<img src='$filepath' />";
    }

    return $out;
}
?>