aboutsummaryrefslogtreecommitdiff
path: root/lib/watermark.php
diff options
context:
space:
mode:
authorCash Costello <cash.costello@gmail.com>2010-05-01 12:24:16 +0000
committerCash Costello <cash.costello@gmail.com>2010-05-01 12:24:16 +0000
commitec7f055fd50b809ea50b7917b9c2e0cee5cd50d6 (patch)
tree40082090349123bfdda1b0b8ba444b9bfe5b91f8 /lib/watermark.php
parentfaeb704ed004777c57389f829519feeb2e235f11 (diff)
downloadelgg-ec7f055fd50b809ea50b7917b9c2e0cee5cd50d6.tar.gz
elgg-ec7f055fd50b809ea50b7917b9c2e0cee5cd50d6.tar.bz2
cleaned up library files
Diffstat (limited to 'lib/watermark.php')
-rw-r--r--lib/watermark.php146
1 files changed, 94 insertions, 52 deletions
diff --git a/lib/watermark.php b/lib/watermark.php
index c7b8503c5..6b16f0e4a 100644
--- a/lib/watermark.php
+++ b/lib/watermark.php
@@ -1,132 +1,175 @@
<?php
-
+/**
+ * Watermarking functions
+ *
+ * @package TidypicsWatermark
+ */
+
+/**
+ * Make replacements in watermark text
+ *
+ * @param string $text
+ * @param ElggUser $owner
+ * @return string
+ */
function tp_process_watermark_text($text, $owner) {
global $CONFIG;
$text = str_replace("%name%", $owner->name, $text);
$text = str_replace("%sitename%", $CONFIG->sitename, $text);
-
+
return $text;
}
+/**
+ * Create the watermark image filename
+ *
+ * @param string $text
+ * @param ElggUser $owner
+ * @return string
+ */
function tp_get_watermark_filename($text, $owner) {
- global $CONFIG;
$base = strtolower($text);
$base = preg_replace("/[^\w-]+/", "-", $base);
$base = trim($base, '-');
-
+
$filename = tp_get_img_dir();
$filename .= strtolower($owner->username . "_" . $base . "_stamp");
-
+
return $filename;
}
+/**
+ * Use GD to apply watermark to image
+ *
+ * @param resource $image GD image resource
+ */
function tp_gd_watermark($image) {
+ global $CONFIG;
+
$watermark_text = get_plugin_setting('watermark_text', 'tidypics');
- if (!$watermark_text)
+ if (!$watermark_text) {
return;
-
+ }
+
// plugins can do their own watermark and return false to prevent this function from running
- if (trigger_plugin_hook('tp_watermark', 'gd', $image, true) === false)
+ if (trigger_plugin_hook('tp_watermark', 'gd', $image, true) === false) {
return;
-
- global $CONFIG;
-
+ }
+
$owner = get_loggedin_user();
$watermark_text = tp_process_watermark_text($watermark_text, $owner);
-
+
// transparent gray
imagealphablending($image, true);
$textcolor = imagecolorallocatealpha($image, 50, 50, 50, 60);
-
+
// font and location
- $font = $CONFIG->pluginspath . "tidypics/fonts/LiberationSerif-Regular.ttf";
+ $font = $CONFIG->pluginspath . "tidypics/fonts/LiberationSerif-Regular.ttf";
$bbox = imagettfbbox(20, 0, $font, $watermark_text);
-
+
$text_width = $bbox[2] - $bbox[0];
$text_height = $bbox[1] - $bbox[7];
-
+
$image_width = imagesx($image);
$image_height = imagesy($image);
-
+
$left = $image_width / 2 - $text_width / 2;
$top = $image_height - 20;
-
+
// write the text on the image
imagettftext($image, 20, 0, $left, $top, $textcolor, $font, $watermark_text);
}
+/**
+ * imagick watermarking
+ *
+ * @param string $filename
+ * @return bool
+ */
function tp_imagick_watermark($filename) {
$watermark_text = get_plugin_setting('watermark_text', 'tidypics');
- if (!$watermark_text)
- return;
-
+ if (!$watermark_text) {
+ return false;
+ }
+
// plugins can do their own watermark and return false to prevent this function from running
- if (trigger_plugin_hook('tp_watermark', 'imagick', $filename, true) === false)
- return;
-
+ if (trigger_plugin_hook('tp_watermark', 'imagick', $filename, true) === false) {
+ return true;
+ }
+
$owner = get_loggedin_user();
$watermark_text = tp_process_watermark_text($watermark_text, $owner);
-
- $img = new Imagick($filename);
- $img->readImage($image);
+ $img = new Imagick($filename);
+
+ $img->readImage($image);
- $draw = new ImagickDraw();
+ $draw = new ImagickDraw();
- //$draw->setFont("");
+ //$draw->setFont("");
- $draw->setFontSize(28);
+ $draw->setFontSize(28);
- $draw->setFillOpacity(0.5);
+ $draw->setFillOpacity(0.5);
- $draw->setGravity(Imagick::GRAVITY_SOUTH);
+ $draw->setGravity(Imagick::GRAVITY_SOUTH);
+
+ $img->annotateImage($draw, 0, 0, 0, $watermark_text);
- $img->annotateImage($draw, 0, 0, 0, $watermark_text);
-
if ($img->writeImage($filename) != true) {
$img->destroy();
return false;
}
-
+
$img->destroy();
-
+
return true;
}
+/**
+ * ImageMagick watermarking
+ *
+ * @param string $filename
+ */
function tp_im_cmdline_watermark($filename) {
-
+
$watermark_text = get_plugin_setting('watermark_text', 'tidypics');
- if (!$watermark_text)
+ if (!$watermark_text) {
return;
-
+ }
+
// plugins can do their own watermark and return false to prevent this function from running
- if (trigger_plugin_hook('tp_watermark', 'imagemagick', $filename, true) === false)
+ if (trigger_plugin_hook('tp_watermark', 'imagemagick', $filename, true) === false) {
return;
-
+ }
+
$im_path = get_plugin_setting('im_path', 'tidypics');
if (!$im_path) {
$im_path = "/usr/bin/";
}
-
+
// make sure end of path is /
- if (substr($im_path, strlen($im_path)-1, 1) != "/") $im_path .= "/";
+ if (substr($im_path, strlen($im_path)-1, 1) != "/") {
+ $im_path .= "/";
+ }
+
-
$owner = get_loggedin_user();
$watermark_text = tp_process_watermark_text($watermark_text, $owner);
-
+
$ext = ".png";
-
+
$user_stamp_base = tp_get_watermark_filename($watermark_text, $owner);
-
-
- if ( !file_exists( $user_stamp_base . $ext )) { //create the watermark if it doesn't exist
+
+
+ if ( !file_exists( $user_stamp_base . $ext )) {
+ //create the watermark image if it doesn't exist
$commands = array();
$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 . '"';
$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;
@@ -134,12 +177,12 @@ function tp_im_cmdline_watermark($filename) {
$commands[] = $im_path . 'mogrify -trim +repage "' . $user_stamp_base . $ext . '"';
$commands[] = 'rm "' . $user_stamp_base . '_mask' . $ext . '"';
$commands[] = 'rm "' . $user_stamp_base . '_fgnd' . $ext . '"';
-
+
foreach( $commands as $command ) {
exec( $command );
}
}
-
+
//apply the watermark
$commands = array();
$commands[] = $im_path . 'composite -gravity south -geometry +0+10 "' . $user_stamp_base . $ext . '" "' . $filename . '" "' . $filename . '_watermarked"';
@@ -148,4 +191,3 @@ function tp_im_cmdline_watermark($filename) {
exec( $command );
}
}
-?> \ No newline at end of file