Add text watermark to existing image PHP code

Source: Internet
Author: User
Tags php class

PHP class Library to the existing image plus text watermark, the code is not very perfect, welcome everyone to advise! The code is as follows:

  1. /*php Picture Plus text Watermark class Library
  2. qq:3697578482 sad song.
  3. This kind of library temporarily supports only text watermark, the position is the bottom right corner, color random
  4. Call Method:
  5. 1, in the need to watermark the top of the file to introduce the class library:
  6. Include_once ' imageclass.php ';
  7. 2. Declaring a new class:
  8. $tpl =new Image_fu;
  9. 3. Provide parameters for image watermark:
  10. $TPL->img (image path, watermark text, font path, font size, font angle);
  11. For example: $tpl->img (' abc.jpg ', ' This is the watermark text ', ' Ziti.ttf ', 30,0)
  12. */
  13. Class image_fu{
  14. Private $image;
  15. Private $img _info;
  16. Private $img _width;
  17. Private $img _height;
  18. Private $img _im;
  19. Private $img _text;
  20. Private $img _ttf= ';
  21. Private $img _new;
  22. Private $img _text_size;
  23. Private $img _JD;
  24. function img ($img = ', $txt = ', $ttf = ', $size =12, $jiaodu =0) {
  25. if (Isset ($img) &&file_exists ($img)) {//detect if a picture exists
  26. $this->image = $img;
  27. $this->img_text= $txt;
  28. $this->img_text_size= $size;
  29. $this->img_jd= $jiaodu;
  30. if (file_exists ($ttf)) {
  31. $this->img_ttf= $ttf;
  32. }else{
  33. Exit (' Font file: '. $ttf. ' does not exist! ');
  34. }
  35. $this->imgyesno ();
  36. }else{
  37. Exit (' picture file: '. $img. ' does not exist ');
  38. }
  39. }
  40. Private Function Imgyesno () {
  41. $this->img_info =getimagesize ($this->image);
  42. $this->img_width = $this->img_info[0];//picture width
  43. $this->img_height= $this->img_info[1];//Pictures High
  44. Detecting picture types
  45. Switch ($this->img_info[2]) {
  46. Case 1: $this->img_im = imagecreatefromgif ($this->image);
  47. Case 2: $this->img_im = imagecreatefromjpeg ($this->image);
  48. Case 3: $this->img_im = imagecreatefrompng ($this->image);
  49. Default:exit (' image format does not support Watermark ');
  50. }
  51. $this->img_text ();
  52. }
  53. Private Function Img_text () {
  54. Imagealphablending ($this->img_im,true);
  55. Set color
  56. $color =imagecolorallocate ($this->img_im,rand (0,255), Rand (0,255), Rand (0,255));
  57. $txt _height= $this->img_text_size;
  58. $txt _jiaodu= $this->img_jd;
  59. $ttf _im=imagettfbbox ($txt _height, $txt _jiaodu, $this->img_ttf, $this->img_text);
  60. $w = $ttf _im[2]-$ttf _im[6];
  61. $h = $ttf _im[3]-$ttf _im[7];
  62. $w = $ttf _im[7];
  63. $h = $ttf _im[8];
  64. unset ($ttf _im);
  65. $txt _y = $this->img_height-$h;
  66. $txt _x = $this->img_width-$w;
  67. $txt _y = 0;
  68. $txt _x = 0;
  69. $this->img_new= @imagettftext ($this->img_im, $txt _height, $txt _jiaodu, $txt _x, $txt _y, $color, $this->img_ TTF, $this->img_text);
  70. @unlink ($this->image);//Delete picture
  71. Switch ($this->img_info[2]) {//Get the format of the background picture
  72. Case 1:imagegif ($this->img_im, $this->image);
  73. Case 2:imagejpeg ($this->img_im, $this->image);
  74. Case 3:imagepng ($this->img_im, $this->image);
  75. Default:exit (' watermark image failed ');
  76. }
  77. }
  78. Show pictures
  79. function Img_show () {echo ' image. ' border= ' 0 "alt=" '. $this->img_text. '/> ';}
  80. Freeing memory
  81. Private Function img_nothing () {
  82. unset ($this->img_info);
  83. Imagedestroy ($this->img_im);
  84. }
  85. }
  86. ?>
Copy Code

Php
  • 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.