Home  >  Article  >  Backend Development  >  PHP adds watermark to pictures (applicable to png/jpg/gif format)

PHP adds watermark to pictures (applicable to png/jpg/gif format)

WBOY
WBOYOriginal
2016-07-25 08:48:52836browse
  1. /**
  2. * Add watermark to picture (applicable to png/jpg/gif format)
  3. *
  4. * @author flynetcn
  5. *
  6. * @param $srcImg original picture
  7. * @param $waterImg watermarked picture
  8. * @param $savepath save path
  9. * @param $savename Save name
  10. * @param $positon Watermark position
  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 adding watermark
  15. * Failure -- -1: The original file does not exist, -2: The watermark image does not exist, -3: The original file image object failed to be created
  16. * -4: The watermark file image object failed to be created -5: The new image after watermarking failed to be saved
  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; //The creation of the original file image object failed
  38. }
  39. $waterImgObj = image_create_from_ext($waterImg);
  40. if (!$waterImgObj) {
  41. return -4; //The establishment of the watermark file image object failed
  42. }
  43. switch ($positon) {
  44. //1 top is on the left
  45. case 1: $x=$y=0; break;
  46. //2 is top on the right
  47. case 2: $x = $srcinfo[0]-$waterinfo[0]; $y = 0; break;
  48. //3 is centered
  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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn