Home php教程 php手册 一个超级强大的图片加水印的类

一个超级强大的图片加水印的类

Jun 13, 2016 am 10:46 AM
indivual add picture deal with accomplish powerful watermark of kind

 

下午没事干,就写了个图片加水印处理的类,本类实现了给图片加文字水印,加图片水印,并且实现了透明度的功能,以供各位网友学习和交流 

 

/**  

 * 加水印类,支持文字、图片水印以及对透明度的设置、水印图片背景透明。  

 * @author  litx  date:2011-12-05下午3点于迈科龙快播研发中心

 */ 

class WaterMask 

    /**

     * 水印类型

     * @var int $waterType 0为文字水印 ;1为图片水印   

     */ 

    private $waterType = 1;  

    /**

     * 水印位置 类型

     * @var int $pos  默认为9(右下角)

     */ 

    private $pos = 9;  

    /**

     * 水印透明度 

     * @var int  $transparent  水印透明度(值越小越透明)

     */ 

    private $transparent = 20;  

    /**

     * 如果是文字水印,则需要加的水印文字

     * @var string $waterStr  默认值  (李铁雄个人作品集)

     */ 

    private $waterStr = '个人作品集';     

    /**

     * 文字字体大小   

     * @var int $fontSize  字体大小

     */ 

    private $fontSize = 14;  

    /**

     * 水印文字颜色(RGB)   

     * @var array $fontColor  水印文字颜色(RGB)   

     */ 

    private $fontColor = array ( 255, 255, 255 );  

    /**

     * 字体文件   

     * @var unknown_type

     */ 

    private $fontFile = 'AHGBold.ttf';  

    /**

     * 水印图片   

     * @var string $waterImg

     */ 

    private $waterImg = 'logo.png';  

    /**

     * 需要添加水印的图片   

     * @var string $srcImg

     */ 

    private $srcImg = '';  

    /**

     * 图片句柄   

     * @var string $im

     */ 

    private $im = '';  

    /**

     * 水印图片句柄   

     * @var string $water_im  

     */ 

    private $water_im = '';  

    /**

     * 图片信息   

     * @var array  $srcImg_info

     */ 

    private $srcImg_info = '';  

    /**

     * 水印图片信息   

     * @var array $waterImg_info  

     */ 

    private $waterImg_info = '';  

    /**

     * 水印文字宽度   

     * @var int $str_w  

     */ 

    private $str_w = '';  

    /**

     * 水印文字高度   

     * @var int $str_h  

     */ 

    private $str_h = '';  

    /**

     * 水印X坐标   

     * @var int $x

     */ 

    private $x = '';  

    /**

     * 水印y坐标   

     * @var int   $y

     */ 

    private $y = ''; 

    /**

     * 构造函数,通过传入需要加水印的源图片初始化源图片

     * @param string $img  需要加水印的源图片

     */ 

    public function __construct ($img) 

    {  

        if(file_exists($img)){//源文件存在 

            $this -> srcImg = $img ; 

        }else{//源文件不存在 

            echo '源文件'.$img.'不存在,请检查看文件路径是否正确'; 

            exit(); 

        } 

         

    } 

    /**

     * 获取需要添加水印的图片的信息,并载入图片

     */ 

    public  function imginfo () 

    {    

        $this -> srcImg_info = getimagesize($this -> srcImg); 

        var_dump($this -> srcImg_info);exit(); 

        switch ($this -> srcImg_info[2]) { 

            case 3 ://png 

                $this -> im = imagecreatefrompng($this -> srcImg); 

                break 1; 

            case 2 :  //  jpeg/jpg 

                $this -> im = imagecreatefromjpeg($this -> srcImg); 

                break 1; 

            case 1 :  //gif 

                $this -> im = imagecreatefromgif($this -> srcImg); 

                break 1; 

            default : 

                echo '源图片文件'. $this -> srcImg .'格式不正确,目前本函数只支持PNG、JPEG、GIF图片水印功能'; 

                exit(); 

        } 

    } 

    /**

     * 获取水印图片的信息,并载入图片

     */ 

    private function waterimginfo () 

    {  

        $this -> waterImg_info = getimagesize($this -> waterImg); 

        switch ($this -> waterImg_info[2]) { 

            case 3 : 

                $this -> water_im = imagecreatefrompng($this -> waterImg); 

                break 1; 

            case 2 : 

                $this -> water_im = imagecreatefromjpeg($this -> waterImg); 

                break 1; 

            case 1 : 

                $this -> water_im = imagecreatefromgif($this -> waterImg); 

                break 1; 

            default : 

                echo '源图片文件'. $this -> srcImg .'格式不正确,目前本函数只支持PNG、JPEG、GIF图片水印功能'; 

                exit(); 

        } 

    } 

    /**

     * 水印位置算法   

     */ 

    private function waterpos () 

    {  

         

        switch ($this -> pos) { 

            case 0 : //随机位置    

                $this -> x = rand(0, $this -> srcImg_info[0] - $this -> waterImg_info[0]); 

                $this -> y = rand(0, $this -> srcImg_info[1] - $this -> waterImg_info[1]); 

                break 1; 

            case 1 : //上左    

                $this -> x = 20; 

                $this -> y = 20; 

                break 1; 

            case 2 : //上中    

                $this -> x = ($this -> srcImg_info[0] - $this -> waterImg_info[0]) / 2; 

                $this -> y = 20; 

                break 1; 

            case 3 : //上右    

                $this -> x = $this -> srcImg_info[0] - $this -> waterImg_info[0]; 

                $this -> y = 20; 

                break 1; 

            case 4 : //中左    

                $this -> x = 20; 

                $this -> y = ($this -> srcImg_info[1] - $this -> waterImg_info[1]) / 2; 

                break 1; 

            case 5 : //中中    

                $this -> x = ($this -> srcImg_info[0] - $this -> waterImg_info[0]) / 2; 

                $this -> y = ($this -> srcImg_info[1] - $this -> waterImg_info[1]) / 2; 

                break 1; 

            case 6 : //中右    

                $this -> x = $this -> srcImg_info[0] - $this -> waterImg_info[0] - 20; 

                $this -> y = ($this -> srcImg_info[1] - $this -> waterImg_info[1]) / 2; 

                break 1; 

            case 7 : //下左    

                $this -> x = 20; 

                $this -> y = $this -> srcImg_info[1] - $this -> waterImg_info[1] - 20; 

                break 1; 

            case 8 : //下中    www.2cto.com

                $this -> x = ($this -> srcImg_info[0] - $this -> waterImg_info[0]) / 2; 

                $this -> y = $this -> srcImg_info[1] - $this -> waterImg_info[1] - 20; 

                break 1; 

            case 9 : //下右    

                $this -> x = $this -> srcImg_info[0] - $this -> waterImg_info[0] - 20; 

                $this -> y = $this -> srcImg_info[1] - $this -> waterImg_info[1] - 20; 

                break 1; 

            default : //下右    

                $this -> x = $this -> srcImg_info[0] - $this -> waterImg_info[0] - 20; 

                $this -> y = $this -> srcImg_info[1] - $this -> waterImg_info[1] - 20; 

                break 1; 

        } 

    } 

    /**

     * 加图片水印

     */ 

    private function waterimg () 

    { 

        if ($this -> srcImg_info[0] waterImg_info[0] || $this -> srcImg_info[1] waterImg_info[1]) { 

            echo '图片尺寸太小,无法加水印,请上传一张大图片'; 

            exit(); 

        } 

        //计算水印位置 

        $this->waterpos(); 

        $cut = imagecreatetruecolor($this -> waterImg_info[0], $this -> waterImg_info[1]); 

        imagecopy($cut, $this -> im, 0, 0, $this -> x, $this -> y, $this -> waterImg_info[0],  

        $this -> waterImg_info[1]); 

        $pct = $this -> transparent; 

        imagecopy($cut, $this -> water_im, 0, 0, 0, 0, $this -> waterImg_info[0],  

        $this -> waterImg_info[1]); 

        //将图片与水印图片合成 

        imagecopymerge($this -> im, $cut, $this -> x, $this -> y, 0, 0, $this -> waterImg_info[0], $this -> waterImg_info[1], $pct); 

    } 

    /**

     * 加文字水印

     */ 

    private function waterstr () 

    { 

        $rect = imagettfbbox($this -> fontSize, 0, $this -> fontFile, $this -> waterStr); 

        $w = abs($rect[2] - $rect[6]); 

        $h = abs($rect[3] - $rect[7]); 

        $fontHeight = $this -> fontSize; 

        $this -> water_im = imagecreatetruecolor($w, $h); 

        imagealphablending($this -> water_im, false); 

        imagesavealpha($this -> water_im, true); 

        $white_alpha = imagecolorallocatealpha($this -> water_im, 255, 255, 255, 127); 

        imagefill($this -> water_im, 0, 0, $white_alpha); 

        $color = imagecolorallocate($this -> water_im, $this -> fontColor[0], $this -> fontColor[1],  

        $this -> fontColor[2]); 

        imagettftext($this -> water_im, $this -> fontSize, 0, 0, $this -> fontSize, $color,  

        $this -> fontFile, $this -> waterStr); 

        $this -> waterImg_info = array ( 

            0 => $w, 1 => $h 

        ); 

        $this->waterimg(); 

    } 

    /**

     * 水印图片输出

     */ 

    public function output () 

    { 

        $this->imginfo(); 

        if ($this -> waterType == 0) { 

            $this->waterstr(); 

        } else { 

            $this->waterimginfo(); 

            $this->waterimg(); 

        } 

        switch ($this -> srcImg_info[2]) { 

            case 3 : 

                imagepng($this -> im, $this -> srcImg); 

                break 1; 

            case 2 : 

                imagejpeg($this -> im, $this -> srcImg); 

                break 1; 

            case 1 : 

                imagegif($this -> im, $this -> srcImg); 

                break 1; 

            default : 

                die('添加水印失败!'); 

                break; 

        } 

        //图片合成后的后续销毁处理 

        imagedestroy($this -> im); 

        imagedestroy($this -> water_im); 

    } 

 

使用方法示例:

//实例化对象  

$obj = new WaterMask('img/10451.jpg');        

//类型:0为文字水印、1为图片水印

$obj->waterType = 0;                      

//水印透明度,值 越小透明度越高

$obj->transparent = 15;                   

//水印文字 

//$obj->waterStr = '生日快乐';

//水印图片        

//$obj->waterImg = '';//水印图片

//文字字体大小 

$obj->fontSize = 14;                      

//水印文字颜色(RGB) 

$obj->fontColor = array(255,255,100);        

//字体文件 

$obj->fontFile = 'STCAIYUN.ttf';         

//输出水印图片文件覆盖到输入的图片文件 

$obj->output();      

 

使用方式比较简单,也很实用。

 

作者 ltx851201

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add watermark to Meitu Xiuxiu? Share how to add watermark to beautiful photos! How to add watermark to Meitu Xiuxiu? Share how to add watermark to beautiful photos! Mar 16, 2024 pm 09:55 PM

Want to know how to add watermark to MeituXiuXiu? Meitu Xiuxiu is a very easy-to-use photo editing software. It provides functions such as cutting out pictures and placing them on another picture, changing the picture size by kb, removing watermarks, changing the background color of ID photos, and adding time, date and location watermarks to the full screen. Help users quickly complete the production of pictures. Some users have created their own pictures and don’t want others to steal them. They want to cover them with their own watermarks, but don’t know how to do it? The editor will now share with you how to add watermarks to beautiful photos! If you like it, come and download it! 1. How to add watermark to beautiful pictures? Share how to add watermark to beautiful photos! 1. Open the 2023 version of Meitu Xiu Xiu downloaded from this site. Meitu Xiu Xiu 2023 version Category: Shooting and beautification Download Meitu Xiu Xiu 2023 version is a feature-rich picture beautification and editing software

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the "My" button in the lower right corner; (2) On the personal center page, find "Settings" and click it; (3) Scroll down and find the "Clear Cache" option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? Mar 21, 2024 pm 09:12 PM

With the popularity of Douyin short videos, user interactions in the comment area have become more colorful. Some users wish to share images in comments to better express their opinions or emotions. So, how to post pictures in TikTok comments? This article will answer this question in detail and provide you with some related tips and precautions. 1. How to post pictures in Douyin comments? 1. Open Douyin: First, you need to open Douyin APP and log in to your account. 2. Find the comment area: When browsing or posting a short video, find the place where you want to comment and click the "Comment" button. 3. Enter your comment content: Enter your comment content in the comment area. 4. Choose to send a picture: In the interface for entering comment content, you will see a "picture" button or a "+" button, click

How to set photo watermark on Xiaomi Mi 14? How to set photo watermark on Xiaomi Mi 14? Mar 18, 2024 am 11:00 AM

In order to make the photos taken more personalized and unique, Xiaomi Mi 14 provides photo watermark settings. By setting photo watermarks, users can add patterns, text and logos to the photos they take, so that each photo can better record precious moments and memories. Next, we will introduce how to set a photo watermark in Xiaomi 14 to make your photos more personalized and vivid. How to set photo watermark on Xiaomi Mi 14? 1. First click “Camera”. 2. Then click "Settings". 3. Then find the watermark, and then you can start shooting.

The operation process of WIN10 service host occupying too much CPU The operation process of WIN10 service host occupying too much CPU Mar 27, 2024 pm 02:41 PM

1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

How to remove the evaluation copy watermark in the lower right corner of win11 24H2? Tips for removing the evaluation copy in the lower right corner of win11 How to remove the evaluation copy watermark in the lower right corner of win11 24H2? Tips for removing the evaluation copy in the lower right corner of win11 Jun 01, 2024 pm 09:52 PM

How to remove the evaluation copy text in the lower right corner of win1124H2? When we use the system, sometimes the desktop will display a transparent watermark on the lower right corner of the screen. So how do we remove this transparent watermark? Users can directly use third-party software to operate. Let this site carefully introduce to users how to remove the watermark on the win1124H2 evaluation copy. To remove the watermark on the win1124H2 evaluation copy, download the UniversalWatermarkDisabler tool. After running it, the current system version and watermark status will be displayed. If "Ready for installation" is displayed in "Status", it can be removed.

How to make ppt pictures appear one by one How to make ppt pictures appear one by one Mar 25, 2024 pm 04:00 PM

In PowerPoint, it is a common technique to display pictures one by one, which can be achieved by setting animation effects. This guide details the steps to implement this technique, including basic setup, image insertion, adding animation, and adjusting animation order and timing. Additionally, advanced settings and adjustments are provided, such as using triggers, adjusting animation speed and order, and previewing animation effects. By following these steps and tips, users can easily set up pictures to appear one after another in PowerPoint, thereby enhancing the visual impact of the presentation and grabbing the attention of the audience.

Introduction to the method of editing watermark with WPS Introduction to the method of editing watermark with WPS Mar 27, 2024 pm 02:06 PM

1. We use WPS to open a document. There is a watermark in it. It looks messy. How to remove it? Look down. 2. Find the Insert tab in the menu bar, select the header and footer icons under this tab, and click on them with the left mouse button. 3. At this time, the text on the page becomes gray and cannot be edited, but the watermark on the back of the text can be edited at this time. 4. Click on the watermark, you can see that this is a picture watermark, because there are several small squares around the picture, and the picture can be edited at this time. 5. Use the delete key on the keyboard to delete the picture, and you can see that the watermark is gone. 6. Double-click the mouse on the page to exit the header and footer editing mode. The text on the page returns to normal color and can be edited, but at this time the watermark on the page has disappeared. 7.

See all articles