Hello. I had 0 problems getting this mod to work. Thank you very kindly for taking the time to create it. I did however have a preference that I wanted to be able to watermark the image in the middle of the picture so it was hard to crop it out and retain a picture worth having. I also wanted to be able to move the watermark down towards the bottom. I added these parts into the watermark.class.php file.
I then needed to add the options to call where the placement of the watermark would be. I added these in.
elseif (strtoupper($this->watermark_position) == "MIDDLE")
{
$src_x = $this->width/2 - $logo_w/2;
$src_y = $this->height/2 - $logo_h/2;
}
elseif (strtoupper($this->watermark_position) == "BOTTOMMIDDLE")
{
$src_x = $this->width/2 - $logo_w/2;
$src_y = $this->height - $logo_h - 80;
}
I had to also call this new placement. Look for this line and change it to suit.
var $watermark_position = "BOTTOMMIDDLE";
If you want to change the vertical placment of "BOTTOMMIDDLE" just change the number to suit your needs. The number to modify is the #80 where it says
$logo_h - 80;
I also wanted to post the entire set of code incase you wanted to copy/past it into your own watermark.class.php file.
Be forewarned I also modified the size requirements for the watermark. Since I'm placing it in the middle I had to make sure the photo is bigger then the width of the watermark. Instead of 2 times bigger then the watermark, the photo only needs to be 1.01 times wider and 1.01 higher and the watermark will be placed.
This is my first time ever changing code and posting the changes for others to use. There may be a better way to do this and I welcome suggestions.
<?php
/**
* Custom Watermark Class for PNG or JPG watermarks with GD2
* Modified for use with Coppermine Image Gallery http://coppermine.sourceforge.net
*
* Usage Example:
Find in functions.inc.php:
return $url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);
Replace with:
if ($pic_row['user1']!="YES") {
return $url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);
} else {
require_once "include/watermark.class.php";
$watermark_file_location = 'images/watermark.png';
$picture_location = $url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);;
$img = new Watermark();
$img->watermark_file = $watermark_file_location;
$img->add_watermark($picture_location);
return $img->image_tag;
}
*
*
* @hacked Timothy Wood <info@skybax.com>
* @Idea from SmartThumb http://www.fullo.net/progetti/smartthumb/
* @authors to Philipp v. Criegern <criegep@criegern.com> & Francesco Fullone <ffullone@progettoaroma.com>
*
* @version 1.0 8/8/2004 (hacked)
*/
class Watermark
{
/**
* Directory where the created watermarked images are stored in (for PHP access)
* e.g. '/usr/local/apache/htdocs/images/watermark'
*/
var $thumbnail_dir_internal = 'albums/watermark/';
/**
* Path to watermarked directory for browser/coppermine access
* e.g. '/images/thumbnails'
*/
var $thumbnail_dir_external = 'albums/watermark/';
/**
* Watermarked image suffix for new watermarked image
* e.g. 'watermarksuffix_orginal.jpg'
* WARNING: Cannot be left blank
*/
var $file_suffix = 'watermark_';
/**
* Error message if creation fails, this will crash the gallery for the particular image if all fails...
*/
var $error;
/**
* where the watermark is inserted
* topleft, bottomleft, topright, bottomright, middle, bottommiddle
*/
var $watermark_position = "BOTTOMMIDDLE";
/**
* the watermark filename
* currently MUST be a PNG24 file! working on gif/jpg support
*/
var $watermark_file;
/**
* which kind of output is request
* valid value is JPG, PNG*
* * if using PNG - look @ www.skybax.com for more information on how to secure images
* (neat transparency effect that disables 'easy' harvesting of images)
*/
var $extension = 'JPG';
/**
* this method REQUIRES the GD 2.0+
* @param string $imagefile Filename of source image
*/
function add_watermark ( $imagefile )
{
if ((is_file($this->watermark_file)) && (function_exists('imagecreatetruecolor')))
{
$this->image_name = $this->file_suffix.basename($imagefile);
$this->image_tag = $imagefile;
if (!is_file($this->thumbnail_dir_internal . $this->image_name))
{
$old = $this->image_info($imagefile);
$logo = ImageCreateFromPNG($this->watermark_file) or die($this->watermark_file.' is not a valid PNG24 file!');
$logo_w = imagesX($logo);
$logo_h = imagesY($logo);
// Check that image to be modified is larger then the watermark. (image has to be 1.01 times the size of the watermark)f
$original = $this->image_info($imagefile);
$original_w = imagesX($old);
$original_h = imagesY($old);
if ($original_w > ($logo_w * 1.01) && $original_h > ($logo_h * 1.01)){
if (strtoupper($this->watermark_position) == "TOPLEFT")
{
$src_x = 0;
$src_y = 0;
}
elseif (strtoupper($this->watermark_position) == "TOPRIGHT")
{
$src_x = $this->width - $logo_w;
$src_y = 0;
}
elseif (strtoupper($this->watermark_position) == "BOTTOMLEFT")
{
$src_x = 0;
$src_y = $this->height - $logo_h;
}
elseif (strtoupper($this->watermark_position) == "BOTTOMRIGHT")
{
$src_x = $this->width - $logo_w;
$src_y = $this->height - $logo_h;
}
elseif (strtoupper($this->watermark_position) == "MIDDLE")
{
$src_x = $this->width/2 - $logo_w/2;
$src_y = $this->height/2 - $logo_h/2;
}
elseif (strtoupper($this->watermark_position) == "BOTTOMMIDDLE")
{
$src_x = $this->width/2 - $logo_w/2;
$src_y = $this->height - $logo_h - 80;
}
else
{
die ('the watermark position: ' . $this->watermark_position . ' is non recognized! try with: TOPLEFT, BOTTOMLEFT, TOPRIGHT, BOTTOMRIGHT, MIDDLE, BOTTOMMIDDLE');
}
$new = imagecreatetruecolor($this->width, $this->height);
ImageAlphaBlending($new, true) or die ("Could not alpha blend");
ImageCopy($new, $old, 0, 0, 0, 0, $this->width, $this->height);
ImageCopy($new, $logo, $src_x, $src_y, 0, 0, $logo_w, $logo_h);
$this->save_image($new);
ImageDestroy($new);
ImageDestroy($old);
ImageDestroy($logo);
}else{ return $img->image_tag; }
// $img->image_tag;
}
else
{
$arr = getimagesize($this->thumbnail_dir_internal . $this->image_name);
$this->width = $arr[0];
$this->height = $arr[1];
}
//place $this->image_tag in the SRC of <img>
$this->image_tag = $this->thumbnail_dir_external . $this->image_name ;
}
else { die('Watermarking file: '. $this->watermark_file.' did not work!'); }
}
/**
* check the directory and update the class var
*/
function check_conf()
{
if (strlen($this->thumbnail_dir_internal) && (substr($this->thumbnail_dir_internal, -1) != '/'))
{
$this->thumbnail_dir_internal .= '/';
}
if (strlen($this->thumbnail_dir_external) && (substr($this->thumbnail_dir_external, -1) != '/'))
{
$this->thumbnail_dir_external .= '/';
}
}
/**
* check the imagefile info to create the correct image stream
* @param $imagefile the image filename
* @return $img the image stream
*/
function image_info($imagefile)
{
$tmp_extension = strtoupper(substr(basename($imagefile), -3));
if ( $tmp_extension == 'JPG' )
{
$img = ImageCreateFromJPEG($imagefile) or die ("Could not create from JPEG");
}
elseif ( $tmp_extension == 'PNG' )
{
$img = ImageCreateFromPNG($imagefile) or die ("Could not create from PNG");
}
else
{
die('the extension '.$tmp_extension.' is not valid, try with JPG or PNG images');
}
$this->width = imagesX($img);
$this->height = imagesY($img);
return $img;
}
/**
* save the image to disk
* @param $img the image stream
* @return void
*/
function save_image($img)
{
if (strtoupper($this->extension) == 'JPG' )
{
$this->image_name = substr($this->image_name, 0, -4) . '.jpg';
ImageJPEG($img, $this->thumbnail_dir_internal . $this->image_name);
}
elseif (strtoupper($this->extension) == 'PNG' )
{
$this->image_name = substr($this->image_name, 0, -4) . '.png';
ImagePNG($img, $this->thumbnail_dir_internal . $this->image_name);
}
else
{
die('the extension '.$this->extension.' is not valid, try with JPG or PNG');
}
}
/**
* delete a thumbnail or watermark file image from the thumbnail_dir_internal
* @param $imagefile the image filename
* @return void
*/
function delete_image($imagefile)
{
$this->image_name = basename($imagefile);
if (is_file($this->thumbnail_dir_internal . $this->image_name))
{
unlink($this->thumbnail_dir_internal . $this->image_name);
}
else
{
die('file does not exist');
}
}
} //end watermark class
?>