Watermark

Soldato
Joined
29 Dec 2004
Posts
5,653
Location
Chatham, Kent
So i've added a watermark script to my website that will add a watermark to any image unless it's a thumbnail (150x150) as it will look stupid.

As you can see here though (Link) there are 2 images for bacata and boogie bug and the star rating down the bottom. These end up being watermarked and look terrible.

The Code:

<?
$src = $_GET['src'];

header('Content-type: image/jpeg');

//this will prevent the watermark from showing up in the thumbnail images
if (eregi("150x150", $src)) {
$watermark = imagecreatefrompng('empty.png');
} else {
$watermark = imagecreatefrompng('watermark.png');
}
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
if(eregi('.gif',$src)) {
$image = imagecreatefromgif($src);
}
elseif(eregi('.jpeg',$src)||eregi('.jpg',$src)) {
$image = imagecreatefromjpeg($src);
}
elseif(eregi('.png',$src)) {
$image = imagecreatefrompng($src);
}
else {
exit("Your image is not a gif, jpeg or png image. Sorry.");
}
$size = getimagesize($src);
$dest_x = $size[0] - $watermark_width - 0;
$dest_y = $size[1] - $watermark_height - 0;
imagecolortransparent($watermark,imagecolorat($watermark,0,0));
imagecopyresampled($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $watermark_width, $watermark_height);

imagejpeg($image, "", 95);
imagedestroy($image);
imagedestroy($watermark);
?>
As you can see from this bit of code:

//this will prevent the watermark from showing up in the thumbnail images
if (eregi("150x150", $src)) {
It prevents it from being in 150x150 images. Instead of excluding the size of the other images, all my images that i want watermarked are 960x720, so how would i make the rule so that the only images that get watermarked are 960x720?

Thanks,

Andy
 
Last edited:
Im not sure how your watermark script works but can you use getimagesize() to get the size of the image and then use an if statement to create the watermark if the image fits the criteria?
 
Off topic, your site seems messed up in Firefox 3.0.11 (right hand column with the search box at the top) is displaying at the bottom of the page near the comment box - assume it should be to the right of the content :)

EDIT: same in FF 3.5 too :(
 
That was me playing about earlier, and forgot a </div> lol Should be ok now though.

What i want with the watermark now is (if it's possible)

Is it possible to have any image that is anythingx400 or bigger, gets watermarked?

So 100x400, 248x400, anything really as long as it's at least 400px in height.

Andy
 
You are already using $size = getimagesize($src); in your code. I'd just move it a little to ease confusion.

It returns as an array $size[1] being your height.

The use a simple if statement to check $size[1] is greater than 400

PHP:
//this will prevent the watermark from showing up in the thumbnail images
$size = getimagesize($src);

if ($size[1] < 400) {
$watermark = imagecreatefrompng('empty.png');
} else {
$watermark = imagecreatefrompng('watermark.png');
}

Also just noticed you have a typo about 6 lines from the bottom of your code, not sure if its having an adverse effect on your script ot just an error from cutting and pasting to the forums.

imagecolortransparent($watermark,imagecolorat($wat ermark,0,0));
imagecopyresampled($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $watermark_width, $watermark_height);
 
Hi Nathan,

Could you possibly paste me the whole code so that i can see that in there, knowing me i'd put it in the wrong place, and that typo is due from pasting.

Many thanks,

Andy
 
PHP:
<?
$src = $_GET['src'];

header('Content-type: image/jpeg');

//this will prevent the watermark from showing up in the thumbnail images

$size = getimagesize($src);
if ($size[1] < 400) {
$watermark = imagecreatefrompng('empty.png');
} else {
$watermark = imagecreatefrompng('watermark.png');
}

$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);

if(eregi('.gif',$src)) {
$image = imagecreatefromgif($src);
}
elseif(eregi('.jpeg',$src)||eregi('.jpg',$src)) {
$image = imagecreatefromjpeg($src);
}
elseif(eregi('.png',$src)) {
$image = imagecreatefrompng($src);
}
else {
exit("Your image is not a gif, jpeg or png image. Sorry.");
}

$dest_x = $size[0] - $watermark_width - 0;
$dest_y = $size[1] - $watermark_height - 0;
imagecolortransparent($watermark,imagecolorat($watermark,0,0));
imagecopyresampled($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $watermark_width, $watermark_height);

imagejpeg($image, "", 95);
imagedestroy($image);
imagedestroy($watermark);
?>

That should do what you need.
 
Would it not be better to cache the watermarked images on the server? I imagine a script like that could kill the CPU of the server with enough visitors.
 
Will try it now.

RobH, to get to the images of that size, you have to click on the thumbnails so i expect it won't get raped as it's not loading on every page until you click an image.

Andy
 
Back
Top Bottom