watermark.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Watermarking functions
  4. *
  5. * @package TidypicsWatermark
  6. */
  7. /**
  8. * Make replacements in watermark text
  9. *
  10. * @param string $text
  11. * @param ElggUser $owner
  12. * @return string
  13. */
  14. function tp_process_watermark_text($text, $owner) {
  15. $text = str_replace("%name%", $owner->name, $text);
  16. $text = str_replace("%sitename%", elgg_get_config('sitename'), $text);
  17. return $text;
  18. }
  19. /**
  20. * Create the watermark image filename
  21. *
  22. * @param string $text
  23. * @param ElggUser $owner
  24. * @return string
  25. */
  26. function tp_get_watermark_filename($text, $owner) {
  27. $base = elgg_strtolower($text);
  28. $base = preg_replace("/[^\w-]+/", "-", $base);
  29. $base = trim($base, '-');
  30. $filename = tp_get_img_dir();
  31. $filename .= elgg_strtolower($owner->username . "_" . $base . "_stamp");
  32. return $filename;
  33. }
  34. /**
  35. * Use GD to apply watermark to image
  36. *
  37. * @param resource $image GD image resource
  38. */
  39. function tp_gd_watermark($image) {
  40. $watermark_text = elgg_get_plugin_setting('watermark_text', 'tidypics');
  41. if (!$watermark_text) {
  42. return;
  43. }
  44. // plugins can do their own watermark and return false to prevent this function from running
  45. if (elgg_trigger_plugin_hook('tp_watermark', 'gd', $image, true) === false) {
  46. return;
  47. }
  48. $owner = elgg_get_logged_in_user_entity();
  49. $watermark_text = tp_process_watermark_text($watermark_text, $owner);
  50. // transparent gray
  51. imagealphablending($image, true);
  52. $textcolor = imagecolorallocatealpha($image, 50, 50, 50, 60);
  53. // font and location
  54. $font = elgg_get_plugins_path() . "tidypics/fonts/LiberationSerif-Regular.ttf";
  55. $bbox = imagettfbbox(20, 0, $font, $watermark_text);
  56. $text_width = $bbox[2] - $bbox[0];
  57. $text_height = $bbox[1] - $bbox[7];
  58. $image_width = imagesx($image);
  59. $image_height = imagesy($image);
  60. $left = $image_width / 2 - $text_width / 2;
  61. $top = $image_height - 20;
  62. // write the text on the image
  63. imagettftext($image, 20, 0, $left, $top, $textcolor, $font, $watermark_text);
  64. }
  65. /**
  66. * imagick watermarking
  67. *
  68. * @param string $filename
  69. * @return bool
  70. */
  71. function tp_imagick_watermark($filename) {
  72. $watermark_text = elgg_get_plugin_setting('watermark_text', 'tidypics');
  73. if (!$watermark_text) {
  74. return false;
  75. }
  76. // plugins can do their own watermark and return false to prevent this function from running
  77. if (elgg_trigger_plugin_hook('tp_watermark', 'imagick', $filename, true) === false) {
  78. return true;
  79. }
  80. $owner = elgg_get_logged_in_user_entity();
  81. $watermark_text = tp_process_watermark_text($watermark_text, $owner);
  82. $img = new Imagick($filename);
  83. $img->readImage($image);
  84. $draw = new ImagickDraw();
  85. //$draw->setFont("");
  86. $draw->setFontSize(28);
  87. $draw->setFillOpacity(0.5);
  88. $draw->setGravity(Imagick::GRAVITY_SOUTH);
  89. $img->annotateImage($draw, 0, 0, 0, $watermark_text);
  90. if ($img->writeImage($filename) != true) {
  91. $img->destroy();
  92. return false;
  93. }
  94. $img->destroy();
  95. return true;
  96. }
  97. /**
  98. * ImageMagick watermarking
  99. *
  100. * @param string $filename
  101. */
  102. function tp_im_cmdline_watermark($filename) {
  103. $watermark_text = elgg_get_plugin_setting('watermark_text', 'tidypics');
  104. if (!$watermark_text) {
  105. return;
  106. }
  107. // plugins can do their own watermark and return false to prevent this function from running
  108. if (elgg_trigger_plugin_hook('tp_watermark', 'imagemagick', $filename, true) === false) {
  109. return;
  110. }
  111. $im_path = elgg_get_plugin_setting('im_path', 'tidypics');
  112. if (!$im_path) {
  113. $im_path = "/usr/bin/";
  114. }
  115. // make sure end of path is /
  116. if (substr($im_path, strlen($im_path)-1, 1) != "/") {
  117. $im_path .= "/";
  118. }
  119. $owner = elgg_get_logged_in_user_entity();
  120. $watermark_text = tp_process_watermark_text($watermark_text, $owner);
  121. $ext = ".png";
  122. $user_stamp_base = tp_get_watermark_filename($watermark_text, $owner);
  123. if ( !file_exists( $user_stamp_base . $ext )) {
  124. //create the watermark image if it doesn't exist
  125. $commands = array();
  126. $commands[] = $im_path . 'convert -size 300x50 xc:grey30 -pointsize 20 -gravity center -draw "fill grey70 text 0,0 \''. $watermark_text . '\'" "'. $user_stamp_base . '_fgnd' . $ext . '"';
  127. $commands[] = $im_path . 'convert -size 300x50 xc:black -pointsize 20 -gravity center -draw "fill white text 1,1 \''. $watermark_text . '\' text 0,0 \''. $watermark_text . '\' fill black text -1,-1 \''. $watermark_text . '\'" +matte ' . $user_stamp_base . '_mask' . $ext;
  128. $commands[] = $im_path . 'composite -compose CopyOpacity "' . $user_stamp_base . "_mask" . $ext . '" "' . $user_stamp_base . '_fgnd' . $ext . '" "' . $user_stamp_base . $ext . '"';
  129. $commands[] = $im_path . 'mogrify -trim +repage "' . $user_stamp_base . $ext . '"';
  130. $commands[] = 'rm "' . $user_stamp_base . '_mask' . $ext . '"';
  131. $commands[] = 'rm "' . $user_stamp_base . '_fgnd' . $ext . '"';
  132. foreach( $commands as $command ) {
  133. exec( $command );
  134. }
  135. }
  136. //apply the watermark
  137. $commands = array();
  138. $commands[] = $im_path . 'composite -gravity south -geometry +0+10 "' . $user_stamp_base . $ext . '" "' . $filename . '" "' . $filename . '_watermarked"';
  139. $commands[] = "mv \"$filename" . "_watermarked\" \"$filename\"";
  140. foreach( $commands as $command ) {
  141. exec( $command );
  142. }
  143. }