A small example of adding watermark to php images

WBOY
Release: 2016-07-25 08:57:53
Original
860 people have browsed it
  1. /**
  2. * Add watermark to image (applicable to png/jpg/gif format)
  3. *
  4. * @author flynetcn
  5. *
  6. * @param $srcImg Original image
  7. * @param $waterImg Watermark image
  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 watermarked 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. * @Editor: 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; //The watermark image does not exist
  35. }
  36. $srcImgObj = image_create_from_ext($srcImg);
  37. if (!$srcImgObj) {
  38. return -3; //Failed to create the original file image object
  39. }
  40. $waterImgObj = image_create_from_ext($waterImg);
  41. if (!$waterImgObj) {
  42. return -4; //Creation of watermark file image object failed
  43. }
  44. switch ($positon) {
  45. //1 top to the left
  46. case 1: $x=$y=0; break;
  47. //2 top to the right
  48. case 2: $x = $srcinfo[0]-$waterinfo[0]; $y = 0; break;
  49. //3 is centered
  50. case 3: $x = ($srcinfo[0]-$waterinfo[0] )/2; $y = ($srcinfo[1]-$waterinfo[1])/2; break;
  51. //4 bottom is on the left
  52. case 4: $x = 0; $y = $srcinfo[1]-$ waterinfo[1]; break;
  53. //5 bottom is on the 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 labels:
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!