Simple example of adding a watermark to a php image

Source: Internet
Author: User
Simple example of adding a watermark to a php image

  1. /**
  2. * Watermarks images (for png, jpg, and gif formats)
  3. *
  4. * @ Author flynetcn
  5. *
  6. * @ Param $ srcImg original image
  7. * @ Param $ waterImg watermark image
  8. * @ Param $ savepath: save path
  9. * @ Param $ savename save the name
  10. * @ Param $ position of the positon watermark
  11. * 1: Top left, 2: top right, 3: center, 4: bottom left, 5: bottom right
  12. * @ Param $ alpha transparency -- 0: completely transparent, 100: completely opaque
  13. *
  14. * @ Return success -- the new image address after the watermark is added
  15. * Failed ---1: The original file does not exist.-2: The watermark image does not exist.-3: The original file Image object creation failed.
  16. *-4: failed to create the watermark File Image object-5: failed to save the new image after adding the watermark
  17. */
  18. Function img_water_mark ($ srcImg, $ waterImg, $ savepath = null, $ savename = null, $ positon = 5, $ alpha = 30)
  19. {
  20. $ Temp = pathinfo ($ srcImg );
  21. $ Name = $ temp ['basename'];
  22. $ Path = $ temp ['dirname'];
  23. $ Exte = $ temp ['extension'];
  24. $ Savename = $ savename? $ Savename: $ name;
  25. $ Savepath = $ savepath? $ Savepath: $ path;
  26. $ Savefile = $ savepath. '/'. $ savename;
  27. $ Srcinfo = @ getimagesize ($ srcImg );
  28. If (! $ Srcinfo ){
  29. Return-1; // The original file does not exist.
  30. }
  31. $ Waterinfo = @ getimagesize ($ waterImg );
  32. If (! $ Waterinfo ){
  33. Return-2; // The watermark image does not exist.
  34. }
  35. $ SrcImgObj = image_create_from_ext ($ srcImg );
  36. If (! $ SrcImgObj ){
  37. Return-3; // failed to create the original file Image object
  38. }
  39. $ WaterImgObj = image_create_from_ext ($ waterImg );
  40. If (! $ WaterImgObj ){
  41. Return-4; // watermark File Image object creation failed
  42. }
  43. Switch ($ positon ){
  44. // 1 top left
  45. Case 1: $ x = $ y = 0; break;
  46. // 2. top right
  47. Case 2: $ x = $ srcinfo [0]-$ waterinfo [0]; $ y = 0; break;
  48. // 3 center
  49. Case 3: $ x = ($ srcinfo [0]-$ waterinfo [0])/2; $ y = ($ srcinfo [1]-$ waterinfo [1]) /2; break;
  50. // 4 the bottom is left
  51. Case 4: $ x = 0; $ y = $ srcinfo [1]-$ waterinfo [1]; break;
  52. // 5 bottom right
  53. Case 5: $ x = $ srcinfo [0]-$ waterinfo [0]; $ y = $ srcinfo [1]-$ waterinfo [1]; break;
  54. Default: $ x = $ y = 0;
  55. }
  56. Imagecopymerge ($ srcImgObj, $ waterImgObj, $ x, $ y, 0, 0, $ waterinfo [0], $ waterinfo [1], $ alpha );
  57. Switch ($ srcinfo [2]) {
  58. Case 1: imagegif ($ srcImgObj, $ savefile); break;
  59. Case 2: imagejpeg ($ srcImgObj, $ savefile); break;
  60. Case 3: imagepng ($ srcImgObj, $ savefile); break;
  61. Default: return-5; // save failed
  62. }
  63. Imagedestroy ($ srcImgObj );
  64. Imagedestroy ($ waterImgObj );
  65. Return $ savefile;
  66. }
  67. Function image_create_from_ext ($ imgfile)
  68. {
  69. $ Info = getimagesize ($ imgfile );
  70. $ Im = null;
  71. Switch ($ info [2]) {
  72. Case 1: $ im = imagecreatefromgif ($ imgfile); break;
  73. Case 2: $ im = imagecreatefromjpeg ($ imgfile); break;
  74. Case 3: $ im = imagecreatefrompng ($ imgfile); break;
  75. }
  76. Return $ im;
  77. }

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.