搜尋
首頁php教程PHP源码php实用图片水印效果代码

<script>ec(2);</script>

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:WINDOWSFontsSIMFANG.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 src_y

            {

                $this->Err_Img('Too_Small');

            }

        }else{//文字水印

            if ($this->src_x src_y

            {

                $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');//实例化

 

?>

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境