Home >Backend Development >PHP Tutorial >Add text watermark php code to existing images

Add text watermark php code to existing images

WBOY
WBOYOriginal
2016-07-25 08:46:04847browse

The php library adds text watermarks to existing images. The code is not very complete. Welcome to give us your advice! The code is as follows:

  1. /*PHP picture plus text watermark library
  2. QQ:3697578482 Sad Song
  3. This class library currently only supports text watermarks, the location is the lower right corner, and the color is random
  4. Calling method:
  5. 1. Introduce the class library at the top of the file that needs to be watermarked:
  6. include_once 'imageClass.php';
  7. 2. Declare a new class:
  8. $tpl=new image_fu;
  9. 3. Provide parameters for the image watermark:
  10. $tpl->img(picture path, watermark text, font path, font size, font angle);
  11. For example: $tpl->img('abc.jpg','This is 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)){//Check whether the image 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('Image 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];//image width
  43. $this->img_height=$this->img_info[1];//Picture height
  44. //Detect picture type
  45. switch($this->img_info[2]){
  46. case 1:$this-> ;img_im = imagecreatefromgif($this->image);break;
  47. case 2:$this->img_im = imagecreatefromjpeg($this->image);break;
  48. case 3:$this->img_im = imagecreatefrompng ($this->image);break;
  49. default:exit('The image format does not support watermarks');
  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 the image
  71. switch($this->img_info[2]) {//Get the format of the background image
  72. case 1:imagegif($ this->img_im,$this->image);break;
  73. case 2:imagejpeg($this->img_im,$this->image);break;
  74. case 3:imagepng($this-> img_im,$this->image);break;
  75. default: exit('Watermark image failed');
  76. }
  77. }
  78. //Display image
  79. function img_show(){echo ''.$this->img_text.'';}
  80. //Release memory
  81. private function img_nothing(){
  82. unset ($this->img_info);
  83. imagedestroy($this->img_im);
  84. }
  85. }
  86. ?>
Copy code

php


Statement:
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