For CPG users who wants to add GD2 watermark function on their gallery.
This MOD uses GD2!
Here is how to mod (original) CPG 1.2.1 to be able to add watermark automatically on new pics and by choise on all pics in selected album.
This is quite long post, but it takes only about 10 minutes to add this mod
Files that needs to be modified:
Lang/english.php
include/picmgmt.inc.php
util.php
config.php
(and 4 SQL command to execute)
REMEMBER to take backup from all files that You are modifyingYou can download modified files in a zip file from
here, or You can do modifications by Your self.
1. Open file Lang/english.php
Find this:
'th_wd' => 'Width',
Add this after it
'wm_bottomright' => 'Bottom right',
'wm_bottomleft' => 'Bottom left',
'wm_topleftt' => 'Up left',
'wm_topright' => 'Up Right',
'wm_both' => 'Both',
'wm_original' => 'Original',
'wm_resized' => 'Resized',
Find this:
array('Enable debug mode', 'debug_mode', 1),
Add this after it
'Image watermarking',
array('Watermark Image on upload', 'enable_watermark', 1),
array('Where to place the watermark', 'where_put_watermark', 8),
array('which files to watermark', 'which_files_to_watermark', 9),
array('Which file to use for watermark', 'watermark_file', 0),
Find this:
'select_album' => 'Select album',
Add this after it
'watermarks' => 'Add watermaks',
'watermark_normal' => 'Resized images only',
'watermark_image' => 'Original sized only',
'watermark_both' => 'Original and resized images',
'watermark_wait' => 'Watermarking images...',
'watermark_continue_wait' => 'Continuing to watermarking originals and/or resized images...',
2.Open file include/picmgmt.inc.php
Find this:
$image_filesize = filesize($image);
Add this before it
//Adds watermarks to new images
if ($CONFIG['enable_watermark'] == '1' && $CONFIG['which_files_to_watermark'] == 'both' || $CONFIG['which_files_to_watermark'] == 'original') {
if (!watermark($image)) //this is for full size image
return false;
}
if ($CONFIG['enable_watermark'] == '1' && $CONFIG['which_files_to_watermark'] == 'both' || $CONFIG['which_files_to_watermark'] == 'resized') {
if (!watermark($normal)) //this is for intermediate size image
return false;
}
Find this:
?>
Add this before it
/**
* watermark_image()
*
* Create a file containing a watermarked image
*
* @param $src_file the source file
* @param $dest_file the destination file
* @param $new_size the size of the square within which the new image must fit
* @param $method the method used for image resizing //ainoastaan gd2
* @return 'true' in case of success
*/
function watermark($src_file)
{
global $CONFIG, $ERROR;
global $lang_errors;
$imginfo = getimagesize($src_file);
if ($imginfo == null)
return false;
// GD can only handle JPG & PNG images
if ($imginfo[2] != GIS_JPG && $imginfo[2] != GIS_PNG) {
$ERROR = $lang_errors['gd_file_type_err'];
return false;
}
// height/width
$srcWidth = $imginfo[0];
$srcHeight = $imginfo[1];
/* if ($thumb_use == 'ht') {
$ratio = $srcHeight / $new_size;
} elseif ($thumb_use == 'wd') {
$ratio = $srcWidth / $new_size;
} else {
$ratio = max($srcWidth, $srcHeight) / $new_size;
}
$ratio = max($ratio, 1.0);*/
$destWidth = $srcWidth; //(int)($srcWidth / $ratio);
$destHeight = $srcHeight; //(int)($srcHeight / $ratio);
$dest_file = $src_file;
// Method for thumbnails creation
// switch ($method) {
// case "gd2" :
if (!function_exists('imagecreatefromjpeg')) {
cpg_die(CRITICAL_ERROR, 'PHP running on your server does not support the GD image library, check with your webhost if ImageMagick is installed', __FILE__, __LINE__);
}
if (!function_exists('imagecreatetruecolor')) {
cpg_die(CRITICAL_ERROR, 'PHP running on your server does not support GD version 2.x, please switch to GD version 1.x on the config page', __FILE__, __LINE__);
}
if ($imginfo[2] == GIS_JPG)
$src_img = imagecreatefromjpeg($src_file);
else
$src_img = imagecreatefrompng($src_file);
if (!$src_img) {
$ERROR = $lang_errors['invalid_image'];
return false;
} //duunataan wesileima
$dst_img = imagecreatetruecolor($destWidth, $destHeight);
/*$dst_img =*/ ImageAlphaBlending($dst_img, true) or die ("Could not alpha blend"); // Enable when on GD 2+
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
$logoImage = ImageCreateFromPNG($CONFIG['watermark_file']);
$logoW = ImageSX($logoImage);
$logoH = ImageSY($logoImage);
//where is the watermark displayed...
$pos = $CONFIG['where_put_watermark'];
if ($pos == "topleft") {
$src_x = 5;
$src_y = 5;
} else if ($pos == "topright") {
$src_x = $srcWidth - ($logoW + 5);
$src_y = 5;
} else if ($pos == "bottomleft") {
$src_x = 5;
$src_y = $srcHeight - ($logoH + 5);
} else if ($pos == "bottomright") {
$src_x = $srcWidth - ($logoW + 5);
$src_y = $srcHeight - ($logoH + 5);
}
ImageCopy($dst_img,$logoImage,$src_x,$src_y,0,0,$logoW,$logoH); //$dst_x,$dst_y,0,0,$logoW,$logoH);
imagejpeg($dst_img, $src_file, $CONFIG['jpeg_qual']);
imagedestroy($src_img);
imagedestroy($dst_img);
// break;
//}
// Set mode of uploaded picture
// We check that the image is valid
$imginfo = getimagesize($src_file);
if ($imginfo == null) {
$ERROR = $lang_errors['resize_failed'];
@unlink($src_file);
return false;
} else {
return true;
}
}
3.Open File util.php
Find this:
function deleteorig()
Add this before it
function vesileimaathumbit() //watermarkthumbs
{
global $picturetbl, $CONFIG, $lang_util_php;
$phpself = $_SERVER['PHP_SELF'];
$albumid = $_POST['albumid'];
$updatetype = $_POST['updatetype'];
$numpics = $_POST['numpics'];
$startpic = 0;
$startpic = $_POST['startpic'];
$query = "SELECT * FROM $picturetbl WHERE aid = '$albumid'";
$result = MYSQL_QUERY($query);
$totalpics = mysql_numrows($result);
if ($startpic == 0) {
// 0 - numpics
$num = $totalpics;
// Over picture limit
if ($totalpics > $numpics) $num = $startpic + $numpics;
} else {
// startpic - numpics
$num = $startpic + $numpics;
if ($num > $totalpics) $num = $totalpics;
}
$i = $startpic;
while ($i < $num) {
$image = $CONFIG['fullpath'] . mysql_result($result, $i, "filepath") . mysql_result($result, $i, "filename");
if ($updatetype == 0 || $updatetype == 2) {
$imagesize = getimagesize($image);
if (max($imagesize[0], $imagesize[1]) > $CONFIG['picture_width'] && $CONFIG['make_intermediate']) {
$image = $CONFIG['fullpath'] . mysql_result($result, $i, "filepath") . $CONFIG['normal_pfx'] . mysql_result($result, $i, "filename");
} else {
$image = $CONFIG['fullpath'] . mysql_result($result, $i, "filepath") . mysql_result($result, $i, "filename");
}
if (watermark($image)) {
print $image .' '. $lang_util_php['updated_succesfully'] . '!<br />'; //$normal .' '. $lang_util_php['updated_succesfully'] . '!<br />';
my_flush();
} else {
print $lang_util_php['error_create'] . ':$image<br />'; //$lang_util_php['error_create'] . ':$normal<br />';
my_flush();
}
}
if ($updatetype == 1 || $updatetype == 2) {
$image = $CONFIG['fullpath'] . mysql_result($result, $i, "filepath") . mysql_result($result, $i, "filename");
if (watermark($image)) {
print $image .' '. $lang_util_php['updated_succesfully'] . '!<br />'; //$normal .' '. $lang_util_php['updated_succesfully'] . '!<br />';
my_flush();
} else {
print $lang_util_php['error_create'] . ':$image<br />'; //$lang_util_php['error_create'] . ':$normal<br />';
my_flush();
}
}
++$i;
}
$startpic = $i;
if ($startpic < $totalpics) {
?>
<form action=<?php echo $phpself;
?> method="post">
<input type="hidden" name="action" value="continuewatermarks" />
<input type="hidden" name="numpics" value="<?php echo $numpics?>" />
<input type="hidden" name="startpic" value="<?php echo $startpic?>" />
<input type="hidden" name="updatetype" value="<?php echo $updatetype?>" />
<input type="hidden" name="albumid" value="<?php echo $albumid?>" />
<input type="submit" value="<?php print $lang_util_php['continue'];
?>" class="submit" /></form>
<?php
}
}
Find this:
filenametotitle(0);
Add this before it
echo '<br /><a href="' . $phpself . '">' . $lang_util_php['back'] . '</a>';
} else if ($action == 'watermarks') {//tämä
global $picturetbl, $CONFIG;
print '<a href="' . $phpself . '">' . $lang_util_php['back'] . '</a><br />';
print '<h2>' . $lang_util_php['watermark_wait'] . '</h2>';
vesileimaathumbit(); //watermarkthumbs
echo '<br /><a href="' . $phpself . '">' . $lang_util_php['back'] . '</a>';
} else if ($action == 'continuewatermarks') {//tämä
print '<a href="' . $phpself . '">' . $lang_util_php['back'] . '</a><br />';
print '<h2>' . $lang_util_php['watermark_wait'] . '</h2>';
vesileimaathumbit(); //watermarkthumbs
Find this:
starttable('100%', '<input type="radio" name="action" value="title" id="title" class="nobg"
(only part of that line)
Add this before it
//This is for watermarking
starttable('100%', '<input type="radio" name="action" value="watermarks" id="watermarks" class="nobg" /><label for="watermarks" accesskey="w" class="labelradio">' . $lang_util_php['watermarks'] . '</label> (1)');
print '
<tr><td>
' . $lang_util_php['update_what'] . ' (2):<br />
<input type="radio" name="updatetype" value="0" id="thumb" class="nobg" /><label for="thumb" accesskey="w" class="labelradio">' . $lang_util_php['watermark_normal'] . '</label><br />
<input type="radio" name="updatetype" value="1" id="resized" class="nobg" /><label for="resized" accesskey="r" class="labelradio">' . $lang_util_php['watermark_image'] . '</label><br />
<input type="radio" name="updatetype" value="2" checked="checked" id="all" class="nobg" /><label for="all" accesskey="a" class="labelradio">' . $lang_util_php['watermark_both'] . '</label><br />
' . $lang_util_php['update_number'] . '
<input type="text" name="numpics" value="' . $defpicnum . '" size="5" /><br />
' . $lang_util_php['update_option'] . '<br /><br />
</td></tr>';
endtable();
print '<br />';
4.Open file config.php
Find this:
function create_form(&$data)
Add this above it
// Added for allowing user to select where to add watermarks on pics...
function form_watermark($text, $name)
{
global $CONFIG, $lang_config_php;
$value = $CONFIG[$name];
$bottomright_selected = ($value == 'bottomright') ? 'selected' : '';
$bottomleft_selected = ($value == 'bottomleft') ? 'selected' : '';
$topleftt_selected = ($value == 'topleftt') ? 'selected' : '';
$topright_selected = ($value == 'topright') ? 'selected' : '';
echo <<<EOT
<tr>
<td class="tableb">
$text
</td>
<td class="tableb" valign="top">
<select name="$name" class="listbox">
<option value="bottomright" $bottomright_selected>{$lang_config_php['wm_bottomright']}</option>
<option value="bottomleft" $bottomleft_selected>{$lang_config_php['wm_bottomleft']}</option>
<option value="topleftt" $topleftt_selected>{$lang_config_php['wm_topleftt']}</option>
<option value="topright" $topright_selected>{$lang_config_php['wm_topright']}</option>
</select>
</td>
</tr>
EOT;
}
// Added for allowing user to select which files to watermark...
function form_watermark2($text, $name)
{
global $CONFIG, $lang_config_php;
$value = $CONFIG[$name];
$both_selected = ($value == 'both') ? 'selected' : '';
$original_selected = ($value == 'original') ? 'selected' : '';
$resized_selected = ($value == 'resized') ? 'selected' : '';
echo <<<EOT
<tr>
<td class="tableb">
$text
</td>
<td class="tableb" valign="top">
<select name="$name" class="listbox">
<option value="both" $both_selected>{$lang_config_php['wm_both']}</option>
<option value="original" $original_selected>{$lang_config_php['wm_original']}</option>
<option value="resized" $resized_selected>{$lang_config_php['wm_resized']}</option>
</select>
</td>
</tr>
EOT;
}
Find this:
// Add triple dropdwon later - put back in for compatibility reasons
Few lines below is
die('Invalid action');
Now we need to paste this
//Watermark place
case 8 :
form_watermark($element[0], $element[1]);
break;
//Which filest to watermark
case 9 :
form_watermark2($element[0], $element[1]);
break;
case 10 :
// do nothing
break;
default:
between them.
After You have done it, it should look like this (I have put couple lines befor and after those lines in this example (hoppe You got it, becouse of My poor english :?
// Thumbnail scaling
case 7 :
form_scale($element[0], $element[1]);
break; // Add triple dropdwon later - put back in for compatibility reasons
//Watermark place
case 8 :
form_watermark($element[0], $element[1]);
break;
//Which filest to watermark
case 9 :
form_watermark2($element[0], $element[1]);
break;
case 10 :
// do nothing
break;
default:
die('Invalid action');
} // switch
} else {
form_label($element);
5.Then You need to execute theese SQL commands with phpMyAdmin or Webmin, etc.
insert into cpg11d_config (name, value) values ('enable_watermark', '1');
insert into cpg11d_config (name, value) values ('where_put_watermark', 'bottomright');
insert into cpg11d_config (name, value) values ('watermark_file', '/your/full/path/to/logo.png');
insert into cpg11d_config (name, value) values ('which_files_to_watermark', 'both');
Thats it. Now You can go to setting and set it up.
Actually You need to go there, before trying to add new pics.
You have to chage 'Which file to use for watermark' to point in Your ow transparen logo.png file that is used as watermark.
There is no check for Your intermediate sized pictures, so if Your pics are smaller that intermediate pic size, You'll get errors for no image found.[b/]
There are some solution to come for that problem.
This should contain every modification I have made.