Example code for adding text watermark to php images

WBOY
Release: 2016-07-25 08:51:52
Original
1152 people have browsed it
  1. $img='22.jpg';

  2. $arr=getimagesize($img);
  3. //print_r($arr); //$arr[0] is the width of the image, $arr[1] is the height of the image, $arr[2] is the type of the image, that is, the extension of the image,
  4. switch($arr[2]){
  5. case 1:
  6. $imgn = imagecreatefromgif ($img);
  7. break;
  8. case 2:
  9. $imgn = imagecreatefromjpeg($img);
  10. break;
  11. case 3:
  12. $imgn = imagecreatefrompng($img);
  13. break;
  14. case 6:
  15. $imgn = imagecreatefromwbmp($img);
  16. break;
  17. default:
  18. die("Unsupported file type");
  19. exit;
  20. }

  21. //Start watermarking. The text to be printed (php text watermark)

  22. $str = "Text to be printed";
  23. $str1 = "Text to be printed";

  24. //$str = iconv("gb2312 ","utf-8","Text to be printed"); //(The last description of this statement);

  25. //Then use the palette to set the color of the text:
  26. //$dest=imagecreatetruecolor(100,100);
  27. $bg = imagecolorallocate($imgn,255,255,255);

  28. //$bg = imagecolorallocate($imgn,0,0,0 );

  29. //php5 cannot know the font of the text, so Load the text font, here load the simhei.ttf boldface that comes with Windows (copy the font file to the project folder before loading) as follows:
  30. imagettftext($imgn,10,0,20,10 ,$bg,'simhei.ttf',$str); //This will type the $str text onto $imgn
  31. imagettftext($imgn,10,0,20,46,$bg,'simhei.ttf', $str1);
  32. header('content-type:image/jpeg');
  33. $uploaddir = './image/';
  34. $thumb_path = './image/'.date("Ymd").'/' ;
  35. if(!is_dir($uploaddir)){
  36. mkdir($uploaddir,0777);
  37. }
  38. if(!is_dir($thumb_path)){
  39. mkdir($thumb_path,0777);
  40. }
  41. $imgs = $ thumb_path.time().'.jpg';
  42. imagejpeg($imgn,$imgs,'80');
  43. echo "";
  44. //: GetImageSize, $ str = iconv("gbk","utf-8","text to be typed"); indicates changing the page's default encoding attribute gbk to utf-8 international standard encoding. $bg = imagecolorallocate() sets the foreground text of the image to white, where 0, 0, 0 respectively represent the red green blue color components, the imagettftext function 12 represents the font size 9 indicates the tilt 10, 10 is the pixel position of the watermark color header('content-type:image/jpeg'); imagejpeg($imgn); indicates that the file type output by the php5 page is an image type.
  45. ?>

Copy code


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!