検索
ホームページ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 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません