Forum Moderators: coopster

Message Too Old, No Replies

trying to rewrite a php class

         

php4U

1:33 am on Jul 19, 2008 (gmt 0)

10+ Year Member



I am running a script that allows me to add a watermark text to an image. Different parameters are conrtrolled in the adming control panel. (font, size, angle) This works but the only problem is that it writes the outputted text all over the image, and I prefer to have it located one time in the bottom right corner.


<?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);
?>

eelixduppy

9:01 pm on Jul 19, 2008 (gmt 0)



>> the outputted text all over the image, and I prefer to have it located one time in the bottom right corner.

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.

php4U

2:17 am on Jul 20, 2008 (gmt 0)

10+ Year Member



It is my fault for not clarifying that this is an image hosting script. There are free/paid account types, and the watermark will go on the free accounts. The watermark will be like a signature with the domain name on it, rather than the traditional watermark with "copyright" all throughout the image.

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]

php4U

12:34 am on Jul 24, 2008 (gmt 0)

10+ Year Member



or rather than trying to re-write the whole class...maybe at least include a way to set the opacity so the existing watermark doesn't take away from the image. any suggestions would help, thank you

eelixduppy

5:36 am on Jul 24, 2008 (gmt 0)



Sorry for the late response. Basically what you are going to do it remove the entire looping section of the script:

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

php4U

2:37 am on Jul 29, 2008 (gmt 0)

10+ Year Member



No worries about a late response, things have been a little hectic on this end recently anyway. I really appreciate the code example, and I will have to look deeper into it because upon execution I got call to undefined function on line 90 which is...

$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.

php4U

12:42 am on Jul 31, 2008 (gmt 0)

10+ Year Member



I went back and found 2 }'s missing in my file. After adding them, this worked and I changed "angle" to something else so it wouldn't print it at the specified angle in the control panel. I appreciate the code you supplied.