A small example of PHP images and watermarks

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 Transparency--0: Completely 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. * @ Edit: bbs.it-home.org
  18. */
  19. function Img_water_mark ($SRCIMG, $WATERIMG, $savepath =null, $savename =null, $positon =5, $alpha =30)
  20. {
  21. $temp = PathInfo ($SRCIMG);
  22. $name = $temp [' basename '];
  23. $path = $temp [' dirname '];
  24. $exte = $temp [' extension '];
  25. $savename = $savename? $savename: $name;
  26. $savepath = $savepath? $savepath: $path;
  27. $savefile = $savepath. ' /'. $savename;
  28. $srcinfo = @getimagesize ($SRCIMG);
  29. if (! $srcinfo) {
  30. return-1; The original file does not exist
  31. }
  32. $waterinfo = @getimagesize ($WATERIMG);
  33. if (! $waterinfo) {
  34. Return-2; Watermark picture does not exist
  35. }
  36. $SRCIMGOBJ = Image_create_from_ext ($SRCIMG);
  37. if (! $srcImgObj) {
  38. return-3; The original file image object failed to build
  39. }
  40. $WATERIMGOBJ = Image_create_from_ext ($WATERIMG);
  41. if (! $waterImgObj) {
  42. return-4; Failed to build Watermark file image Object
  43. }
  44. Switch ($positon) {
  45. 1 Top Left
  46. Case 1: $x = $y = 0; Break
  47. 2 top Right
  48. Case 2: $x = $srcinfo [0]-$waterinfo [0]; $y = 0; Break
  49. 3 centering
  50. Case 3: $x = ($srcinfo [0]-$waterinfo [0])/2; $y = ($srcinfo [1]-$waterinfo [1])/2; Break
  51. 4 bottom Left
  52. Case 4: $x = 0; $y = $srcinfo [1]-$waterinfo [1]; Break
  53. 5 bottom Right
  54. Case 5: $x = $srcinfo [0]-$waterinfo [0]; $y = $srcinfo [1]-$waterinfo [1]; Break
  55. Default: $x = $y = 0;
  56. }
  57. Imagecopymerge ($SRCIMGOBJ, $WATERIMGOBJ, $x, $y, 0, 0, $waterinfo [0], $waterinfo [1], $alpha);
  58. Switch ($srcinfo [2]) {
  59. Case 1:imagegif ($SRCIMGOBJ, $savefile); Break
  60. Case 2:imagejpeg ($SRCIMGOBJ, $savefile); Break
  61. Case 3:imagepng ($SRCIMGOBJ, $savefile); Break
  62. default:return-5; Save failed
  63. }
  64. Imagedestroy ($SRCIMGOBJ);
  65. Imagedestroy ($WATERIMGOBJ);
  66. return $savefile;
  67. }
  68. function Image_create_from_ext ($imgfile)
  69. {
  70. $info = getimagesize ($imgfile);
  71. $im = null;
  72. Switch ($info [2]) {
  73. Case 1: $im =imagecreatefromgif ($imgfile); Break
  74. Case 2: $im =imagecreatefromjpeg ($imgfile); Break
  75. Case 3: $im =imagecreatefrompng ($imgfile); Break
  76. }
  77. return $im;
  78. }
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.