A super powerful image watermark class _ PHP Tutorial

Source: Internet
Author: User
Tags imagecopy transparent watermark
A super powerful image watermark class. In the afternoon, I wrote a watermark class for the image. this class adds a text watermark to the image, adds an image watermark, and implements the transparency function, I wrote a class for adding watermarks to images for users to learn and do nothing in the afternoon. this class provides the ability to add text watermarks to images, add image watermarks, and implement transparency, for your study and communication

/**

* Watermarks: supports text and image watermarks, transparency settings, and transparency of the watermark image background.

* @ Author litx date: 2011-12-05 three o'clock P.M. at the maykelong broadcast R & D center

*/

Class WaterMask

{

/**

* Watermark type

* @ Var int $ waterType 0: text watermark; 1: image watermark

*/

Private $ waterType = 1;

/**

* Watermark Position type

* @ Var int $ pos is 9 by default (bottom right corner)

*/

Private $ pos = 9;

/**

* Watermark transparency

* @ Var int $ transparency of the transparent watermark (the smaller the value, the more transparent)

*/

Private $ transparent = 20;

/**

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

* @ Var string $ waterStr default value (Li Tiexiong's portfolio)

*/

Private $ waterStr = 'portfolio of individuals ';

/**

* 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 (0, 255,255,255 );

/**

* Font files

* @ Var unknown_type

*/

Private $ fontFile = 'ahgbold. ttf ';

/**

* Watermark Image

* @ Var string $ waterImg

*/

Private $ waterImg = 'logo.png ';

/**

* Image to which a watermark is to be added

* @ Var string $ srcImg

*/

Private $ srcImg = '';

/**

* Image handle

* @ Var string $ im

*/

Private $ im = '';

/**

* Watermark Image handle

* @ Var string $ water_im

*/

Private $ water_im = '';

/**

* Image 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 = '';

/**

* X coordinate of the watermark

* @ Var int $ x

*/

Private $ x = '';

/**

* Y coordinate of the watermark

* @ Var int $ y

*/

Private $ y = '';

/**

* Constructor: imports the source image to be watermarked to initialize the source image.

* @ Param string $ img Source image 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 'source file '. $ img.' does not exist. check whether the file path is correct ';

Exit ();

}

}

/**

* Obtain the information of the image to be added with a watermark and load the image.

*/

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 Image file'. $ this-> srcImg. 'is incorrectly formatted. Currently, this function only supports the PNG, JPEG, and GIF image watermarking functions ';

Exit ();

}

}

/**

* Get the watermark image information and load the image

*/

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 'source Image file'. $ this-> srcImg. 'is incorrectly formatted. Currently, this function only supports the PNG, JPEG, and GIF image watermarking functions ';

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

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

$ This-> y = 20;

Break 1;

Case 3: // upper 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: // Medium

$ 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: // right

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

$ 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: // bottom right

$ 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: // bottom right

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

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

Break 1;

}

}

/**

* Adding an image watermark

*/

Private function waterimg ()

{

If ($ this-> srcImg_info [0] <= $ this-> waterImg_info [0] | $ this-> srcImg_info [1] <= $ this-> waterImg_info [1]) {

Echo 'The image is too small to be watermarked. please upload a large image ';

Exit ();

}

// Calculate the Watermark Position

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

// Combine the image and the watermark image

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

}

/**

* Add a 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 after image merging

Imagedestroy ($ this-> im );

Imagedestroy ($ this-> water_im );

}

}

Example:

// Instantiate the object

$ Obj = new WaterMask ('IMG/10451.jpg ');

// Type: 0 indicates a text watermark and 1 indicates an image 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 ();

It is easy to use and practical.

Author ltx851201

...

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.