Advertisement
Guest User

Untitled

a guest
Oct 28th, 2017
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. <?php waterMark($_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'], "watermark.png", "bottom=5,right=5");
  2.  
  3. function waterMark($original, $watermark, $placement = 'bottom=5,right=5', $destination = null) {
  4. $original = urldecode($original);
  5. $info_o = @getImageSize($original);
  6. if (!$info_o)
  7. return false;
  8. $info_w = @getImageSize($watermark);
  9. if (!$info_w)
  10. return false;
  11.  
  12. list ($vertical, $horizontal) = explode(',', $placement,2);
  13. list($vertical, $sy) = explode('=', trim($vertical),2);
  14. list($horizontal, $sx) = explode('=', trim($horizontal),2);
  15.  
  16. switch (trim($vertical)) {
  17. case 'bottom':
  18. $y = $info_o[1] - $info_w[1] - (int)$sy;
  19. break;
  20. case 'middle':
  21. $y = ceil($info_o[1]/2) - ceil($info_w[1]/2) + (int)$sy;
  22. break;
  23. default:
  24. $y = (int)$sy;
  25. break;
  26. }
  27.  
  28. switch (trim($horizontal)) {
  29. case 'right':
  30. $x = $info_o[0] - $info_w[0] - (int)$sx;
  31. break;
  32. case 'center':
  33. $x = ceil($info_o[0]/2) - ceil($info_w[0]/2) + (int)$sx;
  34. break;
  35. default:
  36. $x = (int)$sx;
  37. break;
  38. }
  39.  
  40. header("Content-Type: ".$info_o['mime']);
  41. $original = @imageCreateFromString(file_get_contents($original));
  42. $watermark = @imageCreateFromString(file_get_contents($watermark));
  43. $out = imageCreateTrueColor($info_o[0],$info_o[1]);
  44.  
  45. if ($info_o[2] == 3) { //если картинка png, то сохраняем ей прозрачность
  46. $transparent = imagecolorallocatealpha($out, 0, 0, 0, 127);
  47. imagefill($out, 0, 0, $transparent);
  48. imagesavealpha($out, true);
  49. }
  50. imageCopy($out, $original, 0, 0, 0, 0, $info_o[0], $info_o[1]);
  51.  
  52. //Тут задаем размер изображения в которые можно добавлять Watermark
  53. // $info_o[0] > 250 - ширина изображения должна быть больше 250 px
  54. // $info_o[1] > 250 - высота изображения должна быть больше 250 px
  55.  
  56. if( ($info_o[0] > 250) && ($info_o[1] > 250) )
  57. {
  58. imageCopy($out, $watermark, $x, $y, 0, 0, $info_w[0], $info_w[1]);
  59. }
  60. //95 - качество картинки в процентах на выходе - можно менять на свои значения
  61. switch ($info_o[2]) {
  62. case 1:
  63. imageGIF($out);
  64. break;
  65. case 2:
  66. imageJPEG($out, NULL, 95);
  67. break;
  68. case 3:
  69. imagePNG($out);
  70. break;
  71. }
  72.  
  73. imageDestroy($out);
  74. imageDestroy($original);
  75. imageDestroy($watermark);
  76.  
  77. return true;
  78. }
  79.  
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement