PHP function: simple image watermarking function code

Source: Internet
Author: User
Work needs, write a simple PHP image watermark function. & Lt ;? Php & nbsp;/* & nbsp; Image watermarking function & nbsp; $ imag_url indicates the path of the image to be watermarked & nbsp; $ str indicates the string to be added to the image & nbsp; $ route: the storage path after adding a watermark & nbsp; */& nbsp; function & nbsp required for work. a simple function is to add a watermark to a PHP image.
  1. /*
  2. Image watermarking function
  3. $ Imag_url is the path of the image to be watermarked
  4. $ Str: string to be added to the image
  5. $ Path for storing the route watermark
  6. */
  7. Function watermark ($ imag_url, $ str, $ route ){
  8. $ Type_array = explode (., $ imag_url );
  9. $ Imag_type = $ type_array [count ($ type_array)-1];
  10. Switch ($ imag_type ){
  11. Case gif: $ img = imagecreatefromgif ($ imag_url); break;
  12. Case jpeg: $ img = imagecreatefromjpeg ($ imag_url); break;
  13. Case jpg: $ img = imagecreatefromjpeg ($ imag_url); break;
  14. Case png: $ img = imagecreatefrompng ($ imag_url); break;
  15. Default: $ img = imagecreatefromgif ($ imag_url); break;
  16. }
  17. // Font color
  18. $ Gray = imagecolorallocate ($ img, 235,235,235 );
  19. $ Pink = imagecolorallocate ($ img, 255,128,255 );
  20. /* $ Fontfile font path, depending on the operating system, can be simhei. ttf (), SIMKAI. TTF (entity body), SIMFANG. TTF (Imitation Song), SIMSUN. chinese fonts supported by GD, such as TTC (& */
  21. $ Fontfile = "C: WINDOWSFontsSIMHEI. TTF ";
  22. $ Str = iconv (GB2312, UTF-8, $ str);/* convert the character set of gb2312 to the character set of the UTF-8 */
  23. Imagettftext ($ img, 30, 0, 0,200, $ pink, $ fontfile, $ str );
  24. /* Add a Chinese watermark */
  25. Imagepng ($ img, $ route );
  26. Imagedestroy ($ img );
  27. }
  28. ?>

The above function is expected to benefit everyone.

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.