+ -
当前位置:首页 → 问答吧 → 一个水印类[未完成]

一个水印类[未完成]

时间:2009-09-02

来源:互联网

本帖最后由 如惈 于 2009-9-2 17:47 编辑

我是菜鸟,我是菜鸟……
      前两天刚学了 图片添加水印,闲来无事,写了一个水印类。
OOP是自学的,感觉吧,快忘光了(痛苦ing)...
         各位象哥象姐别笑话我,写的不好,但是目前测试成功,赶紧拿上来分享一下……
虽然还没完善,只是实现了添加图片水印,我回去继续补充~~   等着我啊~
[php]
<?php
define('Water',1);//水印方式(0,文字水印,1图片水印)
define('WaterImg','./water.jpg');//水印图片地址
define('WaterNum',0);//水印位置,0为随机,1-9分别为顶左\顶中\顶右\中左\中中\中右\底左\底中\底右
define('WaterX',81);//水印长
define('WaterY',81);//水印高
define('WaterType','image/jpeg');//水印图片类型
define('WaterText','爱搜罗');//水印文字(必须先转换成UTF-8才能实现中文水印)
define('WaterTextColor','#000000');//水印文字颜色
define('WaterTextSize',50);//水印文字大小 px
define('WaterTextFont','C:\WINDOWS\Fonts\SIMFANG.TTF');//水印文字字体 (仿宋)
define('WaterPtc',50);//水印透明度
//////////////////////水印默认设置完毕

class water
{
    private $img;//图片地址   
    private $img_type;//图片类型   
    //private $img_mime;//图片实际类型
    private $img_x;//图片长   
    private $img_y;//图片高
    private $is_water;//是否可以添加水印,布尔
    private $water_img;//水印图片
    private $water_text;//图片文字(UTF-8)   
    private $water_num;//水印位置(可能用不着)
    private $water_w = WaterX;//water的宽
    private $water_h = WaterY ;//water的长
    private $water_x;//水印的x位置
    private $water_y;//水印的y位置
    private $src_x;//画布与water两图的长度差
    private $src_y;//画布与water两图的高度差
    private $err_msg;//图片地址

    public function __construct($img,$img_type)
    {
        if (file_exists($img) == FALSE)
        {
            $this->Err_Img('No_Img');
        }
        $this->img = $img;
        $this->img_type = $img_type;//写入图片类型
        $this->Check_GD();//检查GD库
        $this->Do_Water();//判断水印方式
        
        $this->Type_Img();//判断图片类型
    }
    /*
     * 检查GD库
     */
    public function Check_GD(){
        if (function_exists("gd_info") == FALSE)
        {
            $this->Err_Img('No_Gd');
        }
    }
    /*
     * 处理水印方式及water文件类型
     */
    public function Do_Water()
    {
        if (Water == 1)//图片水印
        {
            $this->Img_Water();//处理water图片
        }else{//文字水印
            $this->Text_xy();//处理文字水印图片
        }
    }
    /*
     * 处理water图片
     */
    public function Img_Water()
    {
        if (file_exists(WaterImg) == FALSE)
        {
            $this->Err_Img('No_WaterImg');
            //使用文字水印
        }
        switch (WaterType)
        {
            case 'image/jpeg':
                $this->water_img = imagecreatefromjpeg(WaterImg);
                break;
        }
    }
    /*
     * 获得文字的长宽 并 计算 两图的长宽差
     */
    public function Text_xy()
    {
        $str   = iconv('GB2312','UTF-8',WaterText);
        $temp = imagettfbbox(50,0,WaterTextFont,$str);//取得使用 TrueType 字体的文本的范围
        $w = $temp[2] - $temp[6];
        $h = $temp[3] - $temp[7];
        $this->src_x = $this->img_x - $w;
        $this->src_y = $this->img_y - $h;
    }
    /*
     * 判断图片是否能够添加水印,比较大小
     */
    public function Is_Water()
    {
        if (Water == 1)//图片水印
        {
            $this->src_x = $this->img_x - WaterX;
            $this->src_y = $this->img_y - WaterY;
            if ($this->src_x <= 0 && $this->src_y <= 0)
            {
                $this->Err_Img('Too_Small');
            }
        }else{//文字水印
            if ($this->src_x <= 0 && $this->src_y <=0)
            {
                $this->Err_Img('Too_Small');
            }
        }
    }
    /*
     * 处理水印位置
     *   得到 水印位置的坐标
     */
    public function Water_Num()
    {
        switch(WaterNum)
        {
            case 0://随机
                $this->water_x = rand(0,($this->img_x - $this->water_w));
                $this->water_y = rand(0,($this->img_y - $this->water_h));
                break;
            case 1://顶端居左
                $this->water_x = 0;
                $this->water_y = 0;
                break;
            case 2://顶端居中
                $this->water_x = ($this->img_x - $this->water_w)/2;
                $this->water_y = 0;
                break;
            case 3://顶端居右
                $this->water_x = $this->img_x - $this->water_w;
                $this->water_y = 0;
                break;
            case 4://中间居左
                $this->water_x = 0;
                $this->water_y = ($this->img_y - $this->water_h)/2;
                break;
            case 5://中间居中
                $this->water_x = ($this->img_x - $this->water_w)/2;
                $this->water_y = ($this->img_y - $this->water_h)/2;
                break;
            case 6://中间居右
                $this->water_x = $this->img_x - $this->water_w;
                $this->water_y = ($this->img_y - $this->water_h)/2;
                break;
            case 7://底部居左
                $this->water_x = 0;
                $this->water_y = $this->img_y - $this->water_h;
                break;
            case 8://底部居中
                $this->water_x = ($this->img_x - $this->water_w)/2;
                $this->water_y = $this->img_y - $this->water_h;
                break;
            case 9://底部居右
                $this->water_x = $this->img_x - $this->water_w;
                $this->water_y = $this->img_y - $this->water_h;
                break;
            default://随机
                $this->water_x = rand(0,($this->img_x - $this->water_w));
                $this->water_y = rand(0,($this->img_y - $this->water_h));
        }
    }
    /*
     * 判断图片类型
     */
    public function Type_Img()
    {
        switch ($this->img_type)
            {
                case 'image/jpeg':
                    header("Content-type: image/jpeg");
                    $this->img = imagecreatefromjpeg($this->img);
                    break;
            }
            $this->Img_Water();//处理water图片
            $this->img_x = imagesx($this->img);
            $this->img_y = imagesy($this->img);
            $this->Water_Num();//判断水印位置
            $this->Is_Water();
            $this->Make_img();
    }
    /*
     * 制作图片水印
     */
    public function Make_img(){
                imagecopymerge($this->img,$this->water_img,$this->water_x,$this->water_y,0,0,WaterX,WaterY,WaterPtc);
                imagejpeg($this->img);
    }
    /*
     * 制作文字水印
     */
    public function Make_Text()
    {
                echo '测试';
    }
    /*
     * 返回错误信息
     */
    public function Err_Img($msg)
    {
        switch($msg)
        {
            case 'No_Gd':
                $this->err_msg = '没有安装GD库';
                break;
            case 'No_Img':
                $this->err_msg = '图片不存在';
                break;
            case 'No_WaterImg':
                $this->err_msg = 'water图片不存在';
                break;
            case 'Too_Small':
                $this->err_msg = '图片过小';
                break;
        }
        die($this->err_msg);//输出错误信息
    }
    public function __destruct()
    {
        imagedestroy($this->img);
        imagedestroy($this->img_water);
    }
}

$img = new water('./100.jpg','image/jpeg');//实例化

?>
[/php]

-----------------华丽的分割线-----------------

晚上回去再补充补充.......

这个是我的PHP处女原创帖.....激动ing

帅哥美女们~  给指点指点呗~  

作者: 如惈   发布时间: 2009-09-02

顶你处女座哦

作者: ahsxw   发布时间: 2009-09-02

ahsxw
感动啊~  哥们~   终于来了个人了~  哎

作者: 如惈   发布时间: 2009-09-02

很不错, 支持一个.

作者: 冯.于安   发布时间: 2009-09-02

不错来着 ,代码很工整

作者: 古嗣小井   发布时间: 2009-09-09

收藏 学习

作者: silasoni   发布时间: 2009-09-09

不错,支持原创

作者: mailangel123   发布时间: 2009-09-22

一向都喜欢顶处女贴的..

作者: ieliwb   发布时间: 2009-09-22

各位,继续等着。。。  忙啊~~  哎!

作者: 如惈   发布时间: 2009-10-05