Simple example of adding watermark function to php images

WBOY
Release: 2016-07-25 08:51:54
Original
917 people have browsed it
  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 php watermark Transparency -- 0: completely transparent, 100: completely opaque
  13. *
  14. * @return Success -- the new image address after watermarking
  15. * Failure -- -1: The original file does not exist, -2: The watermarked image does not exist, - 3: Failed to create the original file image object
  16. * -4: Failed to create the watermarked file image object -5: Failed to save the new image after adding watermark
  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; //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; //Failed to create 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 Centered
  49. case 3: $ x = ($srcinfo[0]-$waterinfo[0])/2; $y = ($srcinfo[1]-$waterinfo[1])/2; break;
  50. //4The bottom is on the 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. }< ;/p>
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!