Php Practical Image Watermark effect code-PHP source code

Source: Internet
Author: User
Ec (2); php Practical Image Watermark effect code & lt ;? Watermark); watermark image address define (WaterNum, 0); watermark position, 0 random, 1-9 respectively for top left top right middle left middle right bottom left bottom right define (WaterX, 81); watermark length d script ec (2); script

Php Practical Image Watermark effect code

Define ('water', 1); // watermark method (0, text watermark, 1 Image Watermark)

Define ('watermg ','./water.jpg '); // watermark image address

Define ('waternum', 0); // watermark position. 0 is random, and 1-9 is the top, left, middle, right, middle, bottom, middle, bottom, bottom, and right.

Define ('waterx', 81); // watermark length

Define ('watery ', 81); // watermark height

Define ('watertype', 'image/jpeg '); // watermark image type

Define ('watertext', ' '); // watermark text (must be converted to a UTF-8 to realize the Chinese watermark)

Define ('watertextcolor', '#000000'); // watermark text color

Define ('watertextsize', 50); // watermark text size px

Define ('watertextfont ', 'c: WINDOWSFontsSIMFANG. ttf'); // watermark text font (imitation)

Define ('waterpt', 50); // watermark transparency

/// // The watermark is set by default.

Class water

{

Private $ img; // image address

Private $ img_type; // image type

// Private $ img_mime; // actual image type

Private $ img_x; // image Length

Private $ img_y; // Image Height

Private $ is_water; // whether a watermark can be added, Boolean

Private $ water_img; // watermark image

Private $ water_text; // image text (UTF-8)

Private $ water_num; // watermark position (not required)

Private $ water_w = WaterX; // water width

Private $ water_h = WaterY; // The length of water

Private $ water_x; // The x position of the watermark

Private $ water_y; // y position of the watermark

Private $ src_x; // The length difference between the canvas and water

Private $ src_y; // the height difference between the canvas and the water

Private $ err_msg; // image address

Public function _ construct ($ img, $ img_type)

{

If (file_exists ($ img) = FALSE)

{

$ This-> Err_Img ('no _ Img ');

}

$ This-> img = $ img;

$ This-> img_type = $ img_type; // write image type

$ This-> Check_GD (); // check the GD library

$ This-> Do_Water (); // determine the watermark method

$ This-> Type_Img (); // you can specify the image type.

}

/*

* Check the GD library

*/

Public function Check_GD (){

If (function_exists ("gd_info") = FALSE)

{

$ This-> Err_Img ('no _ Gd ');

}

}

/*

* Watermark method and water file type

*/

Public function Do_Water ()

{

If (Water = 1) // Image Watermark

{

$ This-> Img_Water (); // process the water image

} Else {// text watermark

$ This-> Text_xy (); // process text watermark image

}

}

/*

* Processing water images

*/

Public function Img_Water ()

{

If (file_exists (WaterImg) = FALSE)

{

$ This-> Err_Img ('no _ WaterImg ');

// Use a text watermark

}

Switch (WaterType)

{

Case 'image/jpeg ':

$ This-> water_img = imagecreatefromjpeg (WaterImg );

Break;

}

}

/*

* Obtain the length and width of the text and calculate the length and width difference of the two graphs.

*/

Public function Text_xy ()

{

$ Str = iconv ('gb2312', 'utf-8', WaterText );

$ Temp = imagettfbbox (50, 0, WaterTextFont, $ str); // get the text range using the TrueType font

$ W = $ temp [2]-$ temp [6];

$ H = $ temp [3]-$ temp [7];

$ This-> src_x = $ this-> img_x-$ w;

$ This-> src_y = $ this-> img_y-$ h;

}

/*

* Determine whether the image can be added with a watermark and compare the size.

*/

Public function Is_Water ()

{

If (Water = 1) // Image Watermark

{

$ This-> src_x = $ this-> img_x-WaterX;

$ This-> src_y = $ this-> img_y-WaterY;

If ($ this-> src_x <= 0 & $ this-> src_y <= 0)

{

$ This-> Err_Img ('too _ small ');

}

} Else {// text watermark

If ($ this-> src_x <= 0 & $ this-> src_y <= 0)

{

$ This-> Err_Img ('too _ small ');

}

}

}

/*

* Process watermark position

* Obtain the coordinates of the watermark position.

*/

Public function Water_Num ()

{

Switch (WaterNum)

{

Case 0: // random

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

$ This-> water_x = 0;

$ This-> water_y = 0;

Break;

Case 2: // center the top

$ This-> water_x = ($ this-> img_x-$ this-> water_w)/2;

$ This-> water_y = 0;

Break;

Case 3: // top right

$ This-> water_x = $ this-> img_x-$ this-> water_w;

$ This-> water_y = 0;

Break;

Case 4: // center left

$ This-> water_x = 0;

$ This-> water_y = ($ this-> img_y-$ this-> water_h)/2;

Break;

Case 5: // center

$ This-> water_x = ($ this-> img_x-$ this-> water_w)/2;

$ This-> water_y = ($ this-> img_y-$ this-> water_h)/2;

Break;

Case 6: // right of the center

$ This-> water_x = $ this-> img_x-$ this-> water_w;

$ This-> water_y = ($ this-> img_y-$ this-> water_h)/2;

Break;

Case 7: // The bottom is left

$ This-> water_x = 0;

$ This-> water_y = $ this-> img_y-$ this-> water_h;

Break;

Case 8: // center at the bottom

$ This-> water_x = ($ this-> img_x-$ this-> water_w)/2;

$ This-> water_y = $ this-> img_y-$ this-> water_h;

Break;

Case 9: // bottom right

$ This-> water_x = $ this-> img_x-$ this-> water_w;

$ This-> water_y = $ this-> img_y-$ this-> water_h;

Break;

Default: // random

$ This-> water_x = rand (0, ($ this-> img_x-$ this-> water_w ));

$ This-> water_y = rand (0, ($ this-> img_y-$ this-> water_h ));

}

}

/*

* Determining the image type

*/

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 (); // process the water image

$ This-> img_x = imagesx ($ this-> img );

$ This-> img_y = imagesy ($ this-> img );

$ This-> Water_Num (); // you can specify the watermark position.

$ This-> Is_Water ();

$ This-> Make_img ();

}

/*

* Create an image watermark

*/

Public function Make_img (){

Imagecopymerge ($ this-> img, $ this-> water_img, $ this-> water_x, $ this-> water_y, 0, 0, WaterX, WaterY, WaterPtc );

Imagejpeg ($ this-> img );

}

/*

* Create a text watermark

*/

Public function Make_Text ()

{

Echo 'test ';

}

/*

* Error message returned

*/

Public function Err_Img ($ msg)

{

Switch ($ msg)

{

Case 'no _ Gd ':

$ This-> err_msg = 'no GD library installed ';

Break;

Case 'no _ Img ':

$ This-> err_msg = 'image does not exist ';

Break;

Case 'no _ watermg ':

$ This-> err_msg = 'water image does not exist ';

Break;

Case 'too _ small ':

$ This-> err_msg = 'image is too small ';

Break;

}

Die ($ this-> err_msg); // output error message

}

Public function _ destruct ()

{

Imagedestroy ($ this-> img );

Imagedestroy ($ this-> img_water );

}

}

$ Img = new water ('./100.jpg', 'image/jpeg '); // instantiate

?>

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.