PHP watermark class, supports adding images, text, fill color areas,

Source: Internet
Author: User

PHP watermark class, supports adding images, text, fill color areas,

* A self-organized watermark class *

Supports Adding image, text, and fill color areas

<? Php/*** supports text watermarks, transparency settings, and custom watermark positions. * Example: * $ obj = new WaterMask ($ imgFileName); // instantiate the object * $ obj-> $ waterType = 1; // type: 0 is a text watermark, 1 is an image watermark * $ obj-> $ transparent = 45; // watermark transparency * $ obj-> $ waterStr = 'icp .niufee.com '; // watermark text * $ obj-> $ fontSize = 18; // text font size * $ obj-> $ fontColor = array (255,255,255); // watermark text color (RGB) * $ obj-> $ fontFile = 'ahgbold. ttf'; // font file *...... * $ Obj-> output (); // The output watermark image file overwrites the input image file * @ modify liuzp111 */class WaterMask {public $ waterTypeImage = false; // watermark type: Enable the image watermark public $ waterTypeStr = false; // watermark type: enable the text watermark public $ pos = 0; // watermark position public $ transparent = 45; // The greater the watermark transparency value (0---100), the more opaque the value. public $ waterStr = 'icp .niufee.com '; // The watermark text is public $ fontSize = 14; // text font size public $ fontColor = array (0, 0, 0); // default black public $ fontFile = '. /font/si Mfang. ttf'; // The font file public $ waterImg = 'logo.png '; // The watermark image private $ srcImg = ''; // The image private $ im = ''; // image handle private $ water_im = ''; // watermark image handle private $ srcImg_info =''; // image information private $ waterImg_info = ''; // watermark image information private $ str_w = ''; // watermark text width private $ str_h =''; // watermark text height private $ x = ''; // watermark X coordinate private $ y = ''; // watermark y coordinate public $ output_img =''; // Where to store the output image public $ is_draw_rectangle = fals E; // whether to draw a rectangle area (custom position is not supported currently) // public $ rectange_color = ''; // the color of the rectangle area is private $ result_array = array (); // result array public function _ construct ($ img) {// destructor // $ this-> srcImg = file_exists ($ img )? $ Img: die ('"'. $ img. '" source file does not exist! '); If (file_exists ($ img) {$ this-> srcImg = $ img;} else {return array ('data' => '', 'info' => 'the source file does not exist! ', 'Status' => 0) ;}} private function imginfo () {// obtain the information of the image to be added with the watermark and load the image. $ This-> srcImg_info = getimagesize ($ this-> srcImg); switch ($ this-> srcImg_info [2]) {case 3: $ this-> im = imagecreatefrompng ($ this-> srcImg); break 1; case 2: $ this-> im = imagecreatefromjpeg ($ this-> srcImg); break 1; case 1: $ this-> im = imagecreatefromgif ($ this-> srcImg); break 1; default: // die ('original image ('. $ this-> srcImg. ') format is incorrect. Only PNG, JPEG, and GIF formats are supported. '); Return array ('data' => '', 'info' => 'original image ('. $ this-> srcImg. ') format is incorrect. Only PNG, JPEG, and GIF formats are supported. ', 'Status' => 0) ;}} private function waterimginfo () {// obtain the watermark image information and load the image. $ 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: // die ('watermark image ('. $ this-> srcImg. ') format is incorrect. Only PNG, JPEG, and GIF formats are supported. '); Return array ('data' => '', 'info' => 'watermark image ('. $ this-> srcImg. ') format is incorrect. Only PNG, JPEG, and GIF formats are supported. ', 'Status' => 0) ;}} private function waterpos () {// watermark position algorithm 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 = 0; $ this-> y = 0; break 1; case 2: // upper $ this-> x = ($ this-> srcImg_info [0]-$ this-> waterImg_info [0])/2; $ this-> y = 0; break 1; case 3: // upper right $ th Is-> x = $ this-> srcImg_info [0]-$ this-> waterImg_info [0]; $ this-> y = 0; break 1; case 4: // center left $ this-> x = 0; $ 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: // right $ this-> x = $ this-> srcImg_info [0]-$ this-> waterImg_info [0]; $ this-> Y = ($ this-> srcImg_info [1]-$ this-> waterImg_info [1])/2; break 1; case 7: // lower left $ this-> x = 0; $ this-> y = $ this-> srcImg_info [1]-$ this-> waterImg_info [1]; break 1; case 8: // $ this-> x = ($ this-> srcImg_info [0]-$ this-> waterImg_info [0])/2; $ this-> y = $ this-> srcImg_info [1]-$ this-> waterImg_info [1]; break 1; case 9: // 100px $ this-> x = ($ this-> srcImg_info [0]-$ this-> waterImg_info [0])/2; $ this-> y = $ this-> SrcImg_info [1]-$ this-> waterImg_info [1]-100; break 1; default: // bottom right $ this-> x = $ this-> srcImg_info [0]-$ this-> waterImg_info [0]; $ this-> y = $ this-> srcImg_info [1]-$ this-> waterImg_info [1]; break 1 ;}/ *** position of the watermark text image, adjust */private function waterposStr () {$ this-> x = ($ this-> srcImg_info [0]-$ this-> waterImg_info [0])/2 as needed; $ this-> y = $ this-> srcImg_info [1]-$ this-> waterImg_info [1]-3;} private function w Aterimg ($ type = '') {if ($ this-> srcImg_info [0] <= $ this-> waterImg_info [0] | $ this-> srcImg_info [1] <= $ this-> waterImg_info [1 ]) {// die ('the watermark is larger than the source image! '); Return array ('data' => '', 'info' =>' watermark is larger than the source image! ', 'Status' => 0);} if ($ type = 'waterstr') {$ this-> waterposStr ();} else {$ 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, $ this-> waterImg_info [0], $ this-> waterImg_info [1]); imagecopymerge ($ t His-> 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-> wat Er_im, true); $ white_alpha = imagecolorallocatealpha ($ this-> water_im, 255,255,255,127); imagefill ($ this-> water_im, $ 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-> waterim G ($ type = 'waterstr');}/*** draw a rectangle * bool imagefilledrectangle (resource $ image, int $ x1, int $ y1, int $ x2, int $ y2, int $ color) * bool imagerectangle (resource $ image, int $ x1, int $ y1, int $ x2, int $ y2, int $ col) * @ author liuzp111 */public function drawRectangle () {// imagefill ($ im, $ gray); // fill in the resource, fill in the coordinates (similar to the PS magic wand ), color/** 1 -------------- draw a rectangle -------------- * bool imagerectangle (r Esource $ image, int $ x1, int $ y1, int $ x2, int $ y2, int $ col) * parameter: canvas resource, x coordinate in the upper left corner, y coordinate in the upper left corner, bottom right x coordinate, bottom right y coordinate, color */$ color = imagecolorallocate ($ this-> im, 255,255,255 ); // create the border color and fill color of the rectangle. // ================================ ========================================================== ======/// draw and fill the rectangular area // parameter description: // bool imagefilledrectangle (resource $ image, int $ x1, int $ y1, int $ x2, int $ y2, int $ color) // im: to load an image as an image resource // $ x1: X coordinate in the upper left corner of the rectangle // $ y1: Y coordinate in the upper left corner of the rectangle // $ x2: X coordinate in the lower right corner of the rectangle // $ y2: Y coordinate in the bottom right corner of the rectangle // $ color: Fill the RGB color // imagefilledrectangle ($ this-> im, 3, $ this-> srcImg_info [1]-20, $ this-> srcImg_info [0]-3, $ this-> srcImg_info [1]-3, $ color); // do not fill in with the function below, the filling function below is filled with magic wand, which may easily lead to incomplete filling. // imagefill ($ this-> im, $ this-> srcImg_info [0]/2, $ this-> srcImg_info [1]-8, $ color); // fill in the resource, fill in the coordinates (magic wand), color} function output () {$ this-> imginfo (); // whether to create a rectangle if ($ t His-> is_draw_rectangle) {$ this-> drawRectangle ();} if ($ this-> waterTypeStr) {$ this-> waterstr ();} if ($ this-> waterTypeImage) {$ this-> waterimginfo (); $ this-> waterimg ();} switch ($ this-> srcImg_info [2]) {case 3: $ res_output = imagepng ($ this-> im, $ this-> output_img); break 1; case 2: $ res_output = imagejpeg ($ this-> im, $ this-> output_img); break 1; case 1: $ res_output = imagegif ($ this-> im, $ this-> outpu T_img); break 1; default: // die ('failed to add watermark! '); Return array ('data' => '', 'info' =>' failed to add the watermark! ', 'Status' => 0); break;} imagedestroy ($ this-> im); imagedestroy ($ this-> water_im ); return array ('data' => $ res_output, 'info' => 'watermark added successfully! ', 'Status' => 1 );}}

Usage:

$ File = '581_dddc8c51 _ 22'; // image to be watermarked $ file_ext = '.jpeg '; // extension $ imgFileName = '. /'. $ file. $ file_ext; // watermark image path $ obj = new WaterMask ($ imgFileName); // instantiate object $ obj-> waterTypeStr = true; // enable the text watermark $ obj-> waterTypeImage = true; // enable the image watermark $ obj-> pos = 9; // define the watermark image position $ obj-> waterImg = '. /water.png '; // watermark image $ obj-> transparent = 100; // watermark transparency $ obj-> waterStr = 'Insurance BROKER: Liu testtel: 8080 '; // watermark text $ obj-> fontSize = 9; // text font size $ obj-> fontColor = array (0, 0); // watermark text color (RGB) $ obj-> fontFile = '. /font/msyh. ttc '; // font file. Here is $ obj-> is_draw_rectangle = TRUE; // enable to draw the rectangle area $ obj-> output_img = '. /'. $ file. '_ n '. $ file_ext; // path of the output image $ obj-> output ();

The above PHP watermark class supports adding images, text, and fill color areas. It is all the content shared by the editor. I hope you can give it a reference, we also hope that you can support the customer's home.

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.