PHP Image Add Watermark function Simple example

Source: Internet
Author: User
  1. /**

  2. * Image plus watermark (for png/jpg/gif format)
  3. *
  4. * @author FLYNETCN
  5. *
  6. * @param $srcImg Original picture
  7. * @param $waterImg watermark Picture
  8. * @param $savepath Save path
  9. * @param $savename Save name
  10. * @param $positon Watermark Location
  11. * 1: Top left, 2: Top right, 3: center, 4: Bottom station left, 5: Bottom of Right
  12. * @param $alpha PHP watermark Transparency--0: Fully transparent, 100: completely opaque
  13. *
  14. * @return Success--The new image address after adding watermark
  15. * Failure---1: The original file does not exist,-2: Watermark picture does not exist,-3: Original file image object failed to build
  16. *-4: Watermark file Image Object build failed-5: New picture saved after Watermark failed
  17. * * Reference: PHP text watermark, PHP watermark class
  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; Watermark picture does not exist
  34. }
  35. $SRCIMGOBJ = Image_create_from_ext ($SRCIMG);
  36. if (! $srcImgObj) {
  37. return-3; The original file image object failed to build
  38. }
  39. $WATERIMGOBJ = Image_create_from_ext ($WATERIMG);
  40. if (! $waterImgObj) {
  41. return-4; Failed to build Watermark file image Object
  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 centering
  49. Case 3: $x = ($srcinfo [0]-$waterinfo [0])/2; $y = ($srcinfo [1]-$waterinfo [1])/2; Break
  50. 4 bottom 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. }

Copy Code
  • Related Article

    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.