Example of adding watermark to php image

WBOY
Release: 2016-07-25 09:12:55
Original
832 people have browsed it

Example, PHP code to add watermark to images.

  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");//Create from xx file
  10. */
  11. /*
  12. +------ -----------------------
  13. | Generate thumbnails & watermarked image classes
  14. +-------------- ----------------
  15. */
  16. Class Gimage{
  17. var $input_type = ""; //Input image format
  18. var $output_type = "jpg"; //Output Image format
  19. var $limit = 0; // Image size limit
  20. var $filename = ""; // Enter the file name of the image (can also be image data directly)
  21. var $jpeg_quality = 90; // jpeg image quality
  22. var $save_file = ''; //Output file name
  23. var $wm_text = ""; //Watermark text (Chinese is not supported:'( )
  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 the 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 labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!