PHP adds watermark to png/jpg/gif format images

WBOY
Release: 2016-07-25 08:53:05
Original
983 people have browsed it
  1. /**
  2. * Add watermark to image (applicable to png/jpg/gif format)
  3. * * @author flynetcn
  4. * * @param $srcImg Original image
  5. * @param $waterImg Watermark image
  6. * @param $savepath Save path
  7. * @param $savename Save name
  8. * @param $positon Watermark position
  9. * 1: Top left, 2: Top right, 3: Center, 4: Bottom left, 5: Bottom right
  10. * @param $alpha Transparency -- 0 : Completely transparent, 100: Completely opaque*
  11. * @return Success--new image address after adding watermark
  12. * Failure---1: ​​The original file does not exist, -2: The watermark image does not exist, -3: The original file image Object creation failed -4: Watermark file image object creation failed, -5: New watermarked image failed to save
  13. */
  14. function img_water_mark($srcImg, $waterImg, $savepath=null, $savename=null, $positon=5, $alpha=30) {
  15. $temp = pathinfo($srcImg);
  16. $name = $temp['basename'];
  17. $path = $temp['dirname'];
  18. $exte = $temp['extension'];
  19. $savename = $ savename ? $savename : $name;
  20. $savepath = $savepath ? $savepath : $path;
  21. $savefile = $savepath .'/'. $savename;
  22. $srcinfo = @getimagesize($srcImg);
  23. if (! $srcinfo) {
  24. return -1; //The original file does not exist
  25. }
  26. $waterinfo = @getimagesize($waterImg);
  27. if (!$waterinfo) {
  28. return -2; //The watermark image does not exist
  29. }
  30. $srcImgObj = image_create_from_ext($srcImg);
  31. if (!$srcImgObj) {
  32. return -3; //Failed to create original file image object
  33. }
  34. $waterImgObj = image_create_from_ext($waterImg);
  35. if (!$waterImgObj) {
  36. return -4; //Creation of watermark file image object failed
  37. } switch ($positon) {
  38. //1 Top is on the left
  39. case 1: $x=$y=0; break;
  40. //2 Top is on the right
  41. case 2: $x = $srcinfo[0]-$waterinfo[0]; $y = 0; break;
  42. //3 centered
  43. case 3: $x = ($srcinfo[0]-$waterinfo[0])/ 2; $y = ($srcinfo[1]-$waterinfo[1])/2; break; //4 bottom left case 4: $x = 0; $y = $srcinfo[1]-$waterinfo[1] ; break; //5 bottom right case 5: $x = $srcinfo[0]-$waterinfo[0]; $y = $srcinfo[1]-$waterinfo[1]; break; default: $x=$ y=0; } imagecopymerge($srcImgObj, $waterImgObj, $x, $y, 0, 0, $waterinfo[0], $waterinfo[1], $alpha);
  44. switch ($srcinfo[2]) {
  45. case 1: imagegif($srcImgObj, $savefile); break;
  46. case 2: imagejpeg($srcImgObj, $savefile); break;
  47. case 3: imagepng($srcImgObj, $savefile); break;
  48. default: return -5 ; //Save failed
  49. }
  50. imagedestroy($srcImgObj);
  51. imagedestroy($waterImgObj);
  52. return $savefile; } function image_create_from_ext($imgfile) {
  53. $info = getimagesize($imgfile);
  54. $im = null; switch ($info[2]) {
  55. case 1: $im=imagecreatefromgif($imgfile); break;
  56. case 2: $im=imagecreatefromjpeg($imgfile); break;
  57. case 3: $im=imagecreatefrompng($imgfile ); break;
  58. }
  59. return $im;
  60. } ?>
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