PHP Plus Watermark Code support text and image Watermark _php Tutorial

Source: Internet
Author: User
Tags imagecopy
PHP plus picture watermark, Text watermark Class code, PHP plus watermark class, support the text image watermark Transparency settings, watermark picture background transparent. Write a class of their own, because the development of a set of CMS to use, the total feeling of the internet is not comfortable, I hope you also like this class, followed by the use of class methods.

001

002class watermask{

003 Public $waterType = 1; Watermark Type: 0 for text watermark, 1 for picture watermark

004 public $pos = 0; Watermark Location

005 Public $transparent = 45; Watermark Transparency

006 Public $waterStr = ' www.codefans.net '; Watermark Text

007 public $fontSize = 16; Text font size

008 Public $fontColor = Array (255,0,255); Watermark text color (RGB)

009 Public $fontFile = ' ahgbold.ttf ';//font file

010 Public $waterImg = ' logo.png ';//Watermark Picture

011 Private $SRCIMG = ";//images that need to be watermarked

012 Private $im = ";//Picture handle

013 private $water _im = ";//Watermark picture handle

014 private $SRCIMG _info = ";//Picture information

015 private $WATERIMG _info = ";//Watermark picture information

016 private $str _w = ";//Watermark Text width

017 private $str _h = ";//Watermark Text Height

018 private $x = ";//watermark x-coordinate

019 Private $y = ';//watermark y-coordinate

020 function __construct ($img) {

021 $this->srcimg = file_exists ($img) $img: Die (' ". $img. '" Sorry, watermark file does not exist! ');

022}

023 Private Function Imginfo () {//Get watermark picture information and load.

024 $this->srcimg_info = getimagesize ($this->srcimg);

025 switch ($this->srcimg_info[2]) {

026 Case 3:

027 $this->im = imagecreatefrompng ($this->srcimg);

028 break 1;

029 Case 2:

030 $this->im = imagecreatefromjpeg ($this->srcimg);

031 Break 1;

032 Case 1:

033 $this->im = imagecreatefromgif ($this->srcimg);

034 break 1;

035 Default:

036 die (' Watermark picture ('. $this->srcimg. ') Watermark picture is not formatted, please select a PNG, JPEG, GIF format picture. ');

037}

038}

039 private Function Waterimginfo () {//Get watermark picture and load.

040 $this->waterimg_info = getimagesize ($this->waterimg);

041 switch ($this->waterimg_info[2]) {

042 Case 3:

043 $this->water_im = imagecreatefrompng ($this->waterimg);

044 break 1;

045 Case 2:

046 $this->water_im = imagecreatefromjpeg ($this->waterimg);

047 break 1;

048 Case 1:

049 $this->water_im = imagecreatefromgif ($this->waterimg);

050 Break 1;

051 Default:

052 die (' Watermark picture ('. $this->srcimg. ') The format is not correct, only PNG, JPEG, GIF are supported. ');

053}

054}

055 Private Function Waterpos () {//Watermark position algorithm

056 switch ($this->pos) {

057 Case 0://Random position

058 $this->x = rand (0, $this->srcimg_info[0]-$this->waterimg_info[0]);

059 $this->y = rand (0, $this->srcimg_info[1]-$this->waterimg_info[1]);

060 break 1;

061 Case 1://upper Left

062 $this->x = 0;

063 $this->y = 0;

064 break 1;

065 Case 2://Kaminaka

066 $this->x = ($this->srcimg_info[0]-$this->waterimg_info[0])/2;

067 $this->y = 0;

068 break 1;

069 Case 3://upper Right

070 $this->x = $this->srcimg_info[0]-$this->waterimg_info[0];

071 $this->y = 0;

072 break 1;

073 Case 4://Middle Left

074 $this->x = 0;

075 $this->y = ($this->srcimg_info[1]-$this->waterimg_info[1])/2;

076 break 1;

077 Case 5://Middle

078 $this->x = ($this->srcimg_info[0]-$this->waterimg_info[0])/2;

079 $this->y = ($this->srcimg_info[1]-$this->waterimg_info[1])/2;

080 break 1;

081 Case 6://Middle Right

082 $this->x = $this->srcimg_info[0]-$this->waterimg_info[0];

083 $this->y = ($this->srcimg_info[1]-$this->waterimg_info[1])/2;

084 break 1;

085 Case 7://Lower Left

086 $this->x = 0;

087 $this->y = $this->srcimg_info[1]-$this->waterimg_info[1];

088 break 1;

089 Case 8://Down

090 $this->x = ($this->srcimg_info[0]-$this->waterimg_info[0])/2;

091 $this->y = $this->srcimg_info[1]-$this->waterimg_info[1];

092 break 1;

093 default://Down Right

094 $this->x = $this->srcimg_info[0]-$this->waterimg_info[0];

095 $this->y = $this->srcimg_info[1]-$this->waterimg_info[1];

096 break 1;

097}

098}

099 Private Function waterimg () {

if ($this->srcimg_info[0] <= $this->waterimg_info[0] | | $this->srcimg_info[1] <= $this->waterimg _info[1]) {

101 Die (' The watermark is bigger than the original! ');

102}

103 $this->waterpos ();

104 $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]);

106 $pct = $this->transparent;

107 Imagecopy ($cut, $this->water_im,0,0,0,0, $this->waterimg_info[0], $this->waterimg_info[1]);

108 Imagecopymerge ($this->im, $cut, $this->x, $this->y,0,0, $this->waterimg_info[0], $this->waterimg_ INFO[1], $pct);

109}

The Private Function Waterstr () {

111 $rect = Imagettfbbox ($this->fontsize,0, $this->fontfile, $this->waterstr);

$W = ABS ($rect [2]-$rect [6]);

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

$fontHeight = $this->fontsize;

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

Imagealphablending ($this->water_im,false);

117 Imagesavealpha ($this->water_im,true);

118 $white _alpha = Imagecolorallocatealpha ($this->water_im,255,255,255,127);

119 Imagefill ($this->water_im,0,0, $white _alpha);

$color = Imagecolorallocate ($this->water_im, $this->fontcolor[0], $this->fontcolor[1], $this FONTCOLOR[2]);

121 Imagettftext ($this->water_im, $this->fontsize,0,0, $this->fontsize, $color, $this->fontfile, $this- >WATERSTR);

122 $this->waterimg_info = Array (0=> $w,1=> $h);

123 $this->waterimg ();

124}

function output () {

126 $this->imginfo ();

127 if ($this->watertype = = 0) {

$this->waterstr ();

129}else {

->waterimginfo $this ();

131 $this->waterimg ();

132}

133 switch ($this->srcimg_info[2]) {

134 Case 3:

135 imagepng ($this->im, $this->srcimg);

136 Break 1;

137 Case 2:

138 imagejpeg ($this->im, $this->srcimg);

139 Break 1;

1:

141 Imagegif ($this->im, $this->srcimg);

142 Break 1;

143 Default:

144 die (' Reason unknown: Watermark added failed! ');

145 break;

146}

147 Imagedestroy ($this->im);

148 Imagedestroy ($this->water_im);

149}

150}

151?>

PHP generated watermark class usage:

View SOURCEPRINT?01

02$obj = new Watermask ($imgFileName);

03$obj-> $waterType = 1;//watermark Type: 0 for text watermark, 1 for picture watermark

04$obj-> $transparent = 45;//watermark Transparency

05$obj-> $waterStr = ' www.codefans.net ';//Watermark Text

06$obj-> $fontSize = 16;//font Size

07$obj-> $fontColor = Array (255,0,255);//Watermark text color (RGB value)

08$obj-> $fontFile = = ' Ahgbold.ttf '; Font Name

09$obj->output (); Output watermark Image

10?>

http://www.bkjia.com/PHPjc/755612.html www.bkjia.com true http://www.bkjia.com/PHPjc/755612.html techarticle php plus picture watermark, Text watermark Class code, PHP plus watermark class, support the text image watermark Transparency settings, watermark picture background transparent. Write yourself a class, because one developed by yourself ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.