PHP image Add text watermark instance code

Source: Internet
Author: User
Tags imagejpeg
  1. $img = ' 22.jpg ';

  2. $arr =getimagesize ($IMG);
  3. Print_r ($arr); $arr [0] for the width of the picture, $arr [1] for the height of the picture, $arr [2] for the type of the picture, that is, the extension of the picture,
  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 the watermark. Ready to play text (PHP text watermark)

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

  24. $str = Iconv ("gb2312", "Utf-8", "Text to be printed"); (This statement is explained at the end);

  25. Then set the color of the text in the palette:
  26. $dest =imagecreatetruecolor (100,100);
  27. $BG = Imagecolorallocate ($IMGN, 255,255,255);

  28. $BG = Imagecolorallocate ($IMGN, 0,0,0);

  29. PHP5 is unable to know the font of the text, so load the text font, where the Windows comes with the Simhei.ttf bold Word, (before loading the font file copied to the project folder below) as follows:
  30. Imagettftext ($IMGN, 10,0,20,10, $BG, ' Simhei.ttf ', $str); So you can hit the $str text on the $IMGN.
  31. Imagettftext ($IMGN, 10,0,20,46, $BG, ' Simhei.ttf ', $str 1);
  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 hit"); Describes changing the page's default encoding attribute GBK to UTF-8 International standard encoding. $BG = Imagecolorallocate () sets the foreground text of the picture to White where 0,0,0 represents the component of the red Green blue color, Imagettftext function 12 means the font size 9 indicates the tilt 10,10 the header (' Content-type:image/jpeg ') for the pixel location of the watermark color; Imagejpeg ($IMGN); indicates that the file type of the PHP5 page output is a picture type.
  45. ?>

Copy Code
  • 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.