Example of adding a watermark to a PHP image

Source: Internet
Author: User
Tags imagejpeg

example, PHP adds a watermark code to a picture.

  1. /*
  2. Example
  3. $image = new Gimage ();
  4. $image->limit = 600;//length and width limit
  5. $image->wm_text= "www.linuxlaptop.cn";//Watermark Text
  6. $image->wm_fontfile= "Font/xsuni.ttf";//font file
  7. $image->wm_color= "#ff0000 ″;
  8. $image->save_file = "ltcn.jpg";//Save to XX file
  9. $image->create ("linuxlaptop.jpg");//created from XX file
  10. */
  11. /*
  12. +------------------------------
  13. | Create thumbnails & watermark-like pictures
  14. +------------------------------
  15. */
  16. Class gimage{
  17. var $input _type = ""; Enter the format of the picture
  18. var $output _type = "jpg"; Format of the output picture
  19. var $limit = 0; Picture size limit
  20. var $filename = ""; Enter the file name of the picture (it can also be the image data directly)
  21. var $jpeg _quality = 90; JPEG picture quality
  22. var $save _file = "; Output file name
  23. var $wm _text = ""; Watermark text (not supported in Chinese: ' ()
  24. var $wm _size = 12; Watermark Text Size
  25. var $wm _angle = 0; Watermark Text Angle
  26. var $wm _x = 30; Watermark X Coordinate
  27. var $wm _y = 30; Watermark y-coordinate
  28. var $wm _color = "#cccccc"; Watermark Color
  29. var $wm _fontfile = "Geodesic.ttf";//Watermark font file
  30. function Create ($filename = "")
  31. {
  32. if ($filename) $this->filename = $filename;
  33. if (! $this->input_type) $this->get_type ();
  34. if (! $this->output_type) $this->output_type = $this->input_type;
  35. if ($this->input_type = = "jpg") $this->input_type = "jpeg";
  36. if ($this->output_type = = "jpg") $this->output_type = "jpeg";
  37. Switch ($this->input_type) {
  38. Case ' gif ':
  39. $src _img=imagecreatefromgif ($this->filename);
  40. Break
  41. Case ' JPEG ':
  42. $src _img=imagecreatefromjpeg ($this->filename);
  43. Break
  44. Case ' PNG ':
  45. $src _img=imagecreatefrompng ($this->filename);
  46. Break
  47. Default
  48. $src _img=imagecreatefromstring ($this->filename);
  49. Break
  50. }
  51. $src _w=imagesx ($src _img);
  52. $src _h=imagesy ($src _img);
  53. if ($src _w>= $src _h) {
  54. if ($src _w> $this->limit) {
  55. $new _w= $this->limit;
  56. $new _h= ($this->limit/$src _w) * $SRC _h;
  57. }
  58. }
  59. else{
  60. if ($src _h> $this->limit) {
  61. $new _h= $this->limit;
  62. $new _w= ($this->limit/$src _h) * $SRC _w;
  63. }
  64. }
  65. if ($new _h) {
  66. $DST _img=imagecreatetruecolor ($new _w, $new _h);
  67. Imagecopyresampled ($dst _img, $src _img,0,0,0,0, $new _w, $new _h,imagesx ($src _img), Imagesy ($src _img));
  68. }
  69. else{
  70. $DST _img = $src _img;
  71. }
  72. if ($this->wm_text)
  73. {
  74. if (Preg_match ("/([a-f0-9][a-f0-9]) ([a-f0-9][a-f0-9]) ([a-f0-9][a-f0-9])/i", $this->wm_color, $color))
  75. {
  76. $red = Hexdec ($color [1]);
  77. $green = Hexdec ($color [2]);
  78. $blue = Hexdec ($color [3]);
  79. }
  80. $WM _color = Imagecolorallocatealpha ($dst _img, $red, $green, $blue, 90);
  81. Imagettftext ($dst _img, $this->wm_size, $this->wm_angle, $this->wm_x, $this->wm_y, $wm _color, $this- Wm_fontfile, $this->wm_text);
  82. }
  83. if ($this->save_file)
  84. {
  85. Switch ($this->output_type) {
  86. Case ' gif ':
  87. $src _img=imagepng ($dst _img, $this->save_file);
  88. Break
  89. Case ' JPEG ':
  90. $src _img=imagejpeg ($dst _img, $this->save_file, $this->jpeg_quality);
  91. Break
  92. Case ' PNG ':
  93. $src _img=imagepng ($dst _img, $this->save_file);
  94. Break
  95. Default
  96. $src _img=imagejpeg ($dst _img, $this->save_file, $this->jpeg_quality);
  97. Break
  98. }
  99. }
  100. Else
  101. {
  102. Header ("Content-type:image/{$this->output_type}");
  103. Switch ($this->output_type) {
  104. Case ' gif ':
  105. $src _img=imagepng ($dst _img);
  106. Break
  107. Case ' JPEG ':
  108. $src _img=imagejpeg ($dst _img, "", $this->jpeg_quality);
  109. Break
  110. Case ' PNG ':
  111. $src _img=imagepng ($dst _img);
  112. Break
  113. Default
  114. $src _img=imagejpeg ($dst _img, "", $this->jpeg_quality);
  115. Break
  116. }
  117. }
  118. Imagedestroy ($dst _img);
  119. }
  120. function Get_type ()//Get image file type
  121. {
  122. $name _array = Explode (".", $this->filename);
  123. if (Preg_match ("/\." ( Jpg|jpeg|gif|png) $/", $this->filename, $matches))
  124. {
  125. $this->input_type = Strtolower ($matches [1]);
  126. }
  127. Else
  128. {
  129. $this->input_type = "string";
  130. }
  131. }
  132. }
Copy Code
  • 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.