Home >Backend Development >PHP Tutorial >A super powerful class for adding watermarks to images_PHP Tutorial

A super powerful class for adding watermarks to images_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:47:21928browse

I had nothing to do in the afternoon, so I wrote a class for adding watermarks to pictures. This class implements adding text watermarks to pictures, adding picture watermarks, and also implements the transparency function for all netizens to learn and communicate

/**

* Add watermark category, support text, picture watermark and transparency setting, watermark picture background is transparent.

* @author litx date:2011-12-05 at 3 pm at Micron Qvod R&D Center

​*/

class WaterMask

{

/**

* Watermark type

* @var int $waterType 0 is text watermark; 1 is picture watermark

​​*/

Private $waterType = 1;

/**

* Watermark position type

* @var int $pos Default is 9 (lower right corner)

​​*/

Private $pos = 9;

/**

* Watermark transparency

* @var int $transparent Watermark transparency (the smaller the value, the more transparent)

​​*/

private $transparent = 20;

/**

* If it is a text watermark, you need to add the watermark text

* @var string $waterStr Default value (Li Tiexiong’s personal collection)

​​*/

private $waterStr = 'Personal portfolio';

/**

* Text font size

* @var int $fontSize Font size

​​*/

private $fontSize = 14;

/**

* Watermark text color (RGB)

* @var array $fontColor Watermark text color (RGB)

​​*/

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

/**

* Font file

* @var unknown_type

​​*/

Private $fontFile = 'AHGBold.ttf';

/**

* Watermark image

* @var string $waterImg

​​*/

Private $waterImg = 'logo.png';

/**

* Pictures that need to be watermarked

* @var string $srcImg

​​*/

Private $srcImg = '';

/**

* Image handle 

* @var string $im

​​*/

private $im = '';

/**

* Watermark image handle

* @var string $water_im

​​*/

private $water_im = '';

/**

* Picture information

* @var array $srcImg_info

​​*/

Private $srcImg_info = '';

/**

* Watermark image information

* @var array $waterImg_info

​​*/

Private $waterImg_info = '';

/**

* Watermark text width

* @var int $str_w

​​*/

private $str_w = '';

/**

* Watermark text height

* @var int $str_h

​​*/

​ private $str_h = '';  

/**

* Watermark X coordinates

* @var int $x

​​*/

private $x = '';

/**

* Watermark y coordinates

* @var int $y

​​*/

private $y = '';

/**

* Constructor, initialize the source image by passing in the source image that needs to be watermarked

* @param string $img Source image that needs to be watermarked

​​*/

Public function __construct ($img)

If(file_exists($img)){//The source file exists

                $this -> srcImg = $img;

          }else{//The source file does not exist

echo 'The source file'.$img.' does not exist, please check whether the file path is correct';

exit();

         } 

                                                       

}  

/**

* Get the information of the picture that needs to be watermarked, and load the picture

​​*/

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 'source picture file'. $ This -& gt; srcimg. 'Format is incorrect. At present, this function only supports PNG, JPEG, GIF picture watermark function';

exit();

         } 

}  

/**

* Get the information of the watermark image and load the image

​​*/

Private function waterimginfo ()

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

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

case 3:

$ This -& gt; water_im = ImageCreateFrompng ($ this -& gt; 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 'source picture file'. $ This -& gt; srcimg. 'Format is incorrect. At present, this function only supports PNG, JPEG, GIF picture watermark function';

exit();

         } 

}  

/**

* Watermark position algorithm 

​​*/

private function waterpos ()

                                                       

switch ($this -> pos) {

             case 0: //Random position                                      

                            $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: // Upper left

$this -> x = 20;

$this -> y = 20;

                   break 1;

case 2: //upper middle

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

$this -> y = 20;

Break 1;

Case 3: //Top right

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

$this -> y = 20;

                   break 1;

case 4: //center left

$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; 

        } 

    } 

    /**

* Add image watermark

​​*/ 

    private function waterimg () 

    { 

        if ($this -> srcImg_info[0] <= $this -> waterImg_info[0] || $this -> srcImg_info[1] <= $this -> 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); 

    } 

    /**

* Add text watermark

​​*/ 

    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(); 

    } 

    /**

* * Watermark image output

​​*/ 

    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('Failed to add watermark!');

break;

         } 

//Subsequent destruction processing after image synthesis

imagedestroy($this -> im);

imagedestroy($this -> water_im);

}  

}

Usage example:

//instantiate object

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

//Type: 0 is text watermark, 1 is picture watermark

$obj->waterType = 0;

//Watermark transparency, the smaller the value, the higher the transparency

$obj->transparent = 15;

//Watermark text

//$obj->waterStr = 'Happy Birthday';

//Watermark image

//$obj->waterImg = '';//Watermark image

//Text font size

$obj->fontSize = 14;

//Watermark text color (RGB)

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

//Font file

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

//The output watermark image file overwrites the input image file

$obj->output();

The usage is relatively simple and very practical.

Author ltx851201

http://www.bkjia.com/PHPjc/478502.html

www.bkjia.com

true

TechArticleI had nothing to do in the afternoon, so I wrote a class for adding watermarks to images. This class implements adding text watermarks to images. , add image watermarks, and implement transparency functions for netizens to learn and...
Statement:
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