Forum Moderators: coopster
<?php
class waterMarker extends genericOptions {var $imagePath;
var $waterMarkText;
var $signature = "version 1.0";
var $font = 'fonts/arial.ttf';
var $size;
var $angle;
var $hSpacing;
var $vSpacing;
var $imageResource;
var $imageType = "jpg";
var $shadow = false;
var $antialiased = true;
var $red;
var $green;
var $blue;//===================================
// FUNCTION: MARK
// Desc: Draws watermark over image
// Parameters: 0
//===================================function mark()
{
list($width, $height, $type, $attr) = getimagesize($this->imagePath);switch ($this->imageType)
{
case "png":
$createProc = "imagecreatefrompng";
$outputProc = "imagepng";
break;case "gif":
$createProc = "imagecreatefromgif";
$outputProc = "imagepng";
break;case "bmp":
$createProc = "imagecreatefrombmp";
$outputProc = "imagebmp";
break;case "jpeg":
case "jpg":
$createProc = "imagecreatefromjpeg";
$outputProc = "imagejpeg";
break;
}$im = $createProc($this->imagePath);
$grey = imagecolorallocate($im, $this->red, $this->green, $this->blue);
$shadowColor = imagecolorallocate($im, 130, 130, 130);if (!$this->antialiased)
{
$grey *= -1;
$shadowColor *= -1;
}for ($x=20; $x<$width; $x+=$this->hSpacing)
{
for ($y=20; $y<$height; $y+=$this->vSpacing)
{
if ($this->shadow)
{
imagettftext($im, $this->size, $this->angle, $x+1, $y+1, $shadowColor, $this->font, $this->waterMarkText);
}imagettftext($im, $this->size, $this->angle, $x, $y, $grey, $this->font, $this->waterMarkText);
}
}$this->imageResource= $outputProc($im);
}
}
?>
I achieved getting a preset image into the bottom right corner with the following, but that was on a different project. I would appreciate it if anyone might be able to assist with getting the text located in the bottom in the top class script. Thank you
<?php
$imagesource = $_SERVER['DOCUMENT_ROOT'] . "/" . $_GET['path'];
if (!file_exists($imagesource)) die();
$filetype = strtolower(substr($imagesource,strlen($imagesource)-4,4));
if($filetype == ".gif") $image = @imagecreatefromgif($imagesource);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource);
if($filetype == ".png") $image = @imagecreatefrompng($imagesource);
if (empty($image)) die();
$watermark = @imagecreatefromgif('watermark.gif');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth));
$startheight = (($imageheight - $watermarkheight));
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
Do you still want to use this class for watermarks? There is really no point in adding a watermark to just the lower right corner of an image; adding the image on the entire thing as this script apparently does it what you should be looking for in a watermark otherwise it's almost pointless as pieces of the image can still be taken without a watermark on it.
If this is still what you want then I'm sure we can help you get it there, but just note that you are significantly making the watermark less effective in doing what it is meant to do.
Since everything is managed through the control panel, and the class controls the watermarks I pretty much need to use the existing class so it works with the current system.
I can use the bottom script, but the image links have to run through that script like viewer.php?path=/image/path/image.jpg which is fine but there is no way for me to specify if a photo should have a watermark as that will add them to all.
[edited by: php4U at 2:18 am (utc) on July 20, 2008]
for ($x=20; $x<$width; $x+=$this->hSpacing)
{
for ($y=20; $y<$height; $y+=$this->vSpacing)
{
if ($this->shadow)
{
imagettftext($im, $this->size, $this->angle, $x+1, $y+1, $shadowColor, $this->font, $this->waterMarkText);
}
#
imagettftext($im, $this->size, $this->angle, $x, $y, $grey, $this->font, $this->waterMarkText);
}
}
And you just want to write the watermark once in the lower right, so you have to find those coordinates and then just write it. So mayeb something like this:
$x = imagesx($im) - ($this->size * strlen($this->waterMarkText));
$y = imagesy($im) - $this-size;
if ($this->shadow)
{
imagettftext($im, $this->size, $this->angle, $x+1, $y+1, $shadowColor, $this->font, $this->waterMarkText);
}
imagettftext($im, $this->size, $this->angle, $x, $y, $grey, $this->font, $this->waterMarkText);
}
}
I have no idea if this is going to place the watermark where you want it to and I haven't tested this, but that's the general idea. You might have to play around with getting the x and y coodinates in the right spot, but other than that, it's pretty simple.
Good luck
$this->imageResource= $outputProc($im);
Your code really gave me a nice base to work with, so I will just have to correct the function call to see how it displays. Thank you again.