<?php
/*
    Mailimage - Inline image from email address
    Copyright (C) 2008  A. Bram Neijt <bram@neijt.nl>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
    Example code: Output an email address as an image
    Hello my mail is <?php echo mailimage('some@somewhere') ?>, you can contact me there.
*/

// This requires libgd
if (!extension_loaded('gd'))
{
    
$prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' '';
    
dl($prefix 'gd.' PHP_SHLIB_SUFFIX);
}

function 
mailimage($address$nohtml false)
{
    
$w 300;
    
$h 300;
    
$ftype 5;
    
$w strlen($address) * imagefontwidth($ftype) + 2;
    
$h imagefontheight($ftype) + 2;
    
$rc imagecreate($w$h);
    
$bgcolor imagecolorallocate($rc255255255);
    
imagecolortransparent($rc$bgcolor);
    
$tcolor imagecolorallocate($rc100);
    
imagestring($rc$ftype00$address$tcolor);
    
ob_start();
    
imagepng($rc);
    
$imgdata ob_get_contents();
    
ob_end_clean();
    
    
//Create image text
    
$img base64_encode($imgdata);
    
imagedestroy($rc);
    if(
$nohtml)
        return 
$img;
    
$img '<img width="'.$w.'" height="'.$h.'"  style="vertical-align: middle" src="data:image/png;base64,'.htmlentities($img).'" alt="Email address as image">';
    return 
$img;
}