Php image text watermark instance code

Source: Internet
Author: User
Tags imagejpeg
Php image text watermark instance code

  1. Using imgw.'22.jpg ';

  2. $ Arr = getimagesize ($ img );
  3. // Print_r ($ arr); // $ arr [0] indicates the image width, $ arr [1] indicates the image height, and $ arr [2] indicates the image type, that is, the image extension,
  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 types ");
  19. Exit;
  20. }

  21. // Start watermarking. Prepared text (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"); // (This statement is described at the end );

  25. // Set the text color with the color palette:
  26. // $ Dest = imagecreatetruecolor (100,100 );
  27. $ Bg = imagecolorallocate ($ imgn, 255,255,255 );

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

  29. // Php5 cannot know the font of the text, so load the text font. here, load the simhei. ttf italic 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); // in this way, the $ str text is hit to $ 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 = effecthumb_path.time().'.jpg ';
  42. Imagejpeg ($ imgn, $ imgs, '80 ');
  43. Echo "";
  44. //: GetImageSize, $ str = iconv ("gbk", "UTF-8", "text to be marked"); this indicates that the default encoding attribute gbk of the page is changed to UTF-8 international standard encoding. $ Bg = imagecolorallocate (): Set the foreground text of the image to white, where 0, 0, and 0 represent the components of the red green blue color, respectively, imagettftext function 12 indicates the font size. 9 indicates a 10-bit tilt. 10 indicates the watermark color. the pixel position header ('content-type: image/jpeg '); imagejpeg ($ imgn ); indicates that the output file type of the php5 page is image.
  45. ?>

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.