Laien's

成功 自在 健康 好状态

【原创】图片Email——安全共享你的联系方式

without comments

  相信很多朋友都有这样的遭遇:因为在论坛或贴吧共享过自己的Email等联系方式,结果导致自己的邮箱被垃圾邮件填满。更有甚者自己的手机号被机器人收集,导致常常半夜被叫醒~
  鉴于很多朋友的痛苦经历和我本人长期以来的想法,我做了个图片Email生成程序。今后大家在论坛发帖共享自己的联系方式时可以考虑使用该程序。使用方法很简单,访问http://i.isclab.org/e.php。输入Email地址后程序会为你生成动态的图片地址。然后拿着这个地址到论坛当图片插入到帖子里就行了!如图:
图片Email-安全你的联系方式
  完整程序代码如下:

<?
/**
 *@desc 将Email地址转换成图片,防止被机器人爬到。
 *关键技术:
 *1.PHP GD(Graphics Draw)的使用
 *2.CURL调用ISGD短网址API
 *3.如果图片生成失败,请确认同级目录是否有字体文件cambriab.ttf
 *@auth shadu###foxmail.com
 *@version 2010-01-24 16:19
 *
 **/
 
$example = base64_encode('user@example.com');
 
// POST数据,用户要生成图片
if(isset($_POST['tag']) && "1" == $_POST['tag']){
    if(isset($_POST['emailInput']) && 0 < strlen($_POST['emailInput'])){
        $example = base64_encode($_POST['emailInput']) ;
    }
    showInput($example) ;
    exit(0);
}
 
// GET数据,论坛在调用图片
if(isset($_GET['e'])){
    if(255 < strlen($_GET['e']) || 2 > strlen($_GET['e'])){ // 恶意提交
        showInput($example) ;
        exit(0);
    }
    $str = base64_decode($_GET['e']) ;
 
    if(255 < strlen($str) || 2 > strlen($str)){ // 恶意提交
        showInput($example) ;
        exit(0);
    }
    showImage($str) ;
    exit(0);
}else{
    showInput($example) ;
    exit(0);
}
 
function showInput($str = ''){
    $result = '' ;
    if('' != $str){
        $str = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["SCRIPT_NAME"] . "?e=$str" ;
        $result = "<div style=\"width:500px;\" align=\"left\">" ;
        $result .= "论坛博客贴图地址:<br/>" ;
        $result .= "<input name=\"secemail\" type=\"text\" readonly value=\"$str#e.png\" size=\"80\" />" ;
        $result .= "<br/><br/><a href=\"http://is.gd/\" target=\"_blank\" >ISGD</a>短网址:<br/>" ;
        $result .= "<input name=\"secemail\" type=\"text\" readonly value=\"" . isGod($str) . "\" size=\"40\" /><br/>" ;
        $result .= "部分网站(如百度)不支持此类URL,可以使用<br/><font color=\"red\">http://http://is.gd/6UzzI#.png</font><br/>的方式贴图:)" ;
 
        $result .= "<br/><br/>HTML插图代码:" ;
        $result .= htmlentities("<img src=\"" . isGod($str) . "\" />") ;
        $result .= "<div name=\"preview\" style=\"margin-top:30px;\">" ;
        $result .= "安全联系方式效果图(效果不理想的话请在字符前后多加几个空格):<br/><img src=\"$str\" alt=\"反垃圾邮件,安全你的Email\" /></dvi>" ;
        $result .= "</div>" ;
    }
 
    $strHtml = <<<EOT
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>创建安全联系方式-图片Email</title>
<meta name="keywords" content="安全联系方式,图片Email,email防护,隐私保护,安全Email" />
<meta name="description" content="在网络上可以安全使用的联系方式" />
</head>
<body>
<div name="create" align="center" style="margin-top:20px;">
    <font color="blue" size="10"><b>Sec your email!</b></font><br /><br />
    <form name="createimge" method="post" action="e.php">请输入您的Email:
        <input name="emailInput" value="user@example.com" type="text" />&nbsp;&nbsp;&nbsp;&nbsp;
        <input name="creatButton" value="生成安全Email" type="submit" />
        <input name="tag" value="1" type="hidden" />
    </form>
</div>
 
<div name="result" align="center" style="margin-top:50px;">
    $result
</div>
</body>
</html>
EOT;
    echo $strHtml ;
}
 
function showImage($msg){
    // 声明生成PNG图片
    Header("Content-type: image/PNG");
    // 生成一个图片
    $img = new MSG2PNG($msg);
    //$img->strFontName = "simhei.ttf" ; // 设置中文字体
    $img->createImage();
}
/**
 *@desc 这是一个类:用于将指定字符串写入图片
 *@auth shadu###foxmail.com
 *@version 2010-01-24 10:25
 *@useage $img = new MSG2PNG("user@example.com") ;
 *        $img->createImage();
 */
class MSG2PNG{
    public  $strMessageBody = "user@example.com" ; // 要写入的字符串
    public  $strFontName = "cambriab.ttf" ;
    public  $intFontSize = 20 ;
    public  $intSize = array(260, 70) ;
    public  $rgbBackGroundColor = array(0xFF, 0xFF, 0xFF) ;
    public  $rgbFontColor = array(0x28, 0x00, 0x00) ;
    public  $rgbNoisePointColor = array(0x20, 0x20, 0x20) ;
    public  $rgbNoiseLinesColor = array(0xA0, 0xA0, 0xA0) ;
    public  $intNoiseNum = 3000 ;
    public  $intLinesNum = 50 ;
    public  $intAngle = 10 ;
    public  $intPosition = array(2, 60) ;
    private $imgImage = NULL ;
 
    public function __construct($strMessage = "user@example.com"){
        $this->strMessageBody = $strMessage ;
        //以下数行仅供参考,我是用来设置图片大小和噪音数量的,不是很准确。
        $this->intSize[0] = strlen($this->strMessageBody) * 15 ;
        $this->intSize[1] = strlen($this->strMessageBody) * 15 * 70 /260 ;
        $this->intPosition[1] = $this->intSize[1] * 60 / 70 ;
        $this->intNoiseNum = 3000 * $this->intSize[0] / 260 ;
        $this->intLinesNum = 50 * $this->intSize[0] / 260 ;
    }
 
    public function setColor(){
        if( NULL == $this->imgImage) return ; 
        imagecolorallocate($this->imgImage,
                           $this->rgbBackGroundColor[0],
                           $this->rgbBackGroundColor[1],
                           $this->rgbBackGroundColor[2]) ;
    }
 
    public function createNoise(){
        //Create noise points and lines
        if( NULL == $this->imgImage) return ;        
        $imgNoisePointColor = imagecolorallocate($this->imgImage,
                                                 $this->rgbNoisePointColor[0],
                                                 $this->rgbNoisePointColor[1],
                                                 $this->rgbNoisePointColor[2]) ;
        $imgNoiseLinesColor = imagecolorallocate($this->imgImage,
                                                 $this->rgbNoiseLinesColor[0],
                                                 $this->rgbNoiseLinesColor[1],
                                                 $this->rgbNoiseLinesColor[2]) ;
        for($i=0; $i<$this->intNoiseNum; $i++)
            imagesetpixel($this->imgImage,
                          mt_rand(0, $this->intSize[0]),
                          mt_rand(0,$this->intSize[1]),
                          $imgNoisePointColor);
 
        for($i=0; $i<$this->intLinesNum; $i++)
            imageline($this->imgImage,
                      mt_rand(0, $this->intSize[0]), mt_rand(0, $this->intSize[1]), 
                      mt_rand(0, $this->intSize[0]), mt_rand(0, $this->intSize[1]), 
                      $imgNoiseLinesColor);
    }
 
    public function createFont(){
        if( NULL == $this->imgImage) return ;
        $imgFontColor = imagecolorallocate($this->imgImage,
                                           $this->rgbFontColor[0],
                                           $this->rgbFontColor[1],
                                           $this->rgbFontColor[2]) ;
        ImageTTFText($this->imgImage, $this->intFontSize, $this->intAngle,
                     $this->intPosition[0], $this->intPosition[1],
                     $imgFontColor, $this->strFontName,
                     $this->strMessageBody) ;
    }
    public function createImage(){
        $this->imgImage = imagecreate($this->intSize[0], $this->intSize[1]) ;
        $this->setColor() ;
        $this->createNoise();
        $this->createFont() ;
        ImagePNG($this->imgImage) ;
    }
 
    public function __destruct(){
        if( NULL == $this->imgImage) return ;
        ImageDestroy($this->imgImage) ;
    }
}
 
/**
 *@desc 从isgod网站获取指定URL的短路径
 **/
function isGod($str){
    $content = '' ;
    $url = "http://is.gd/api.php?longurl=$str" ;
    $ch = curl_init() ;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch,  CURLOPT_RETURNTRANSFER, TRUE) ;
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}
?>

  本程序使用的字体文件cambriab.ttf

Written by Laien

January 24th, 2010 at 8:46 am