Jump to content
xisto Community
BuffaloHelp

Watermark Your Image With Simple Php Script found it on the net

Recommended Posts

This script was found on the net http://forums.xisto.com/no_longer_exists/ B&T's Tips & Scripts site. Just in case the site may not show, I will include the code here:

List of things needed:
1. your image in any format
2. watermark image--in gif format with transparent background
3. script below with name (i.e. watermark.php)

<?php // this script creates a watermarked image from an image file - can be a .jpg .gif or .png file // where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory as this script // where this script is named watermark.php // call this script with an image tag // <img src="watermark.php?path=imagepath"> where path is a relative path such as subdirectory/image.jpg $imagesource =  $_GET['path']; $filetype = substr($imagesource,strlen($imagesource)-4,4); $filetype = strtolower($filetype); if($filetype == ".gif")  $image = @imagecreatefromgif($imagesource);  if($filetype == ".jpg")  $image = @imagecreatefromjpeg($imagesource);  if($filetype == ".png")  $image = @imagecreatefrompng($imagesource);  if (!$image) die(); $watermark = @imagecreatefromgif('watermark.gif'); $imagewidth = imagesx($image); $imageheight = imagesy($image);  $watermarkwidth =  imagesx($watermark); $watermarkheight =  imagesy($watermark); $startwidth = (($imagewidth - $watermarkwidth)/2); $startheight = (($imageheight - $watermarkheight)/2); imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); ?>

Name this script, i.e. watermark.php and call this script as following:
<img src="watermark.php?path=image_name.filetype">


the only thing you need to chage is "image_name.filetype" and of course you can have the relative path such as:

<img src="folder/watermark.php?path=folder/imagename">



The caution here is the script watermark.php and watermark.gif should be in the same location. watermark.gif should have transparent background.

As you can read it from the site, the location where the watermark.gif appears can be modified by adjusting this line of the code:

$startwidth = (($imagewidth - $watermarkwidth)/2); $startheight = (($imageheight - $watermarkheight)/2);
To have it appear on the bottom right corner, try this:
$startwidth = (($imagewidth - $watermarkwidth) ); $startheight = (($imageheight - $watermarkheight) );

Personal note: I have installed Apache2 and PHP5 in my computer and couldn't get this working at first. Later I found out I had to edit php.ini under extension to enable php_gd2.dll in order to make it work under LOCALHOST/

I hope you find a good usage out of this :lol:

Share this post


Link to post
Share on other sites

Hey this can be really useful when it comes to using images. You no longer have to make a watermark for every image that you make in image editing software! Now, does this work with hotlinks? It would be cool if I can put a watermark on an imageshack uploaded image or a hotlinked banner etc...

Share this post


Link to post
Share on other sites

Do you mean like this?

<img src="watermark.php?path=http://site/image.type">

I tested it out and what do you know, it works... wow good question and what a find! But remember that watermark.gif should be with watermark.php in the same location. I wonder if the script can be modified so that the watermark.gif can be located elsewhere...? For those PHP gurus out there, see if you can modify this script so that you can use different watermark.gif images. So that we can use one line command such that you can use multiple or alternate watermark.gif images:

<img src="watermark.php?mark=watermark.gif_location&path=image_location">

Again, since I am very new to PHP programming, I'm assuming two different var can be called in one command. So basically, the command would be like: "watermark script, location of watermark.gif, location of image"

Share this post


Link to post
Share on other sites

Here's one with a variable path as requested. It works on my Xisto account. I cleaned up the code, added some idiot proofing, and made it so that the watermark could be a .png which also supports alpha channels (transparency) and which won't dither like gifs do. Like the original, it only supports images with .gif/.jpg/.jpeg/.png extensions. I left the original author's (bad) naming scheme.

<?php	// this script creates a watermarked image from an image file - can be a .jpg .gif or .png file	// where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory as this script	// where this script is named watermark.php	// call this script with an image tag	// <img src="watermark.php?path=imagepath"> where path is a relative path such as subdirectory/image.jpg	$imagesource =  $_GET['path'];	$watermarkPath = $_GET['watermark'];	$filetype = substr($imagesource,strlen($imagesource)-4,4);	$filetype = strtolower($filetype);	$watermarkType = substr($watermarkPath,strlen($watermarkPath)-4,4);	$watermarkType = strtolower($watermarkType);		if($filetype == ".gif")  		$image = @imagecreatefromgif($imagesource);	else  		if($filetype == ".jpg" || $filetype == "jpeg")  			$image = @imagecreatefromjpeg($imagesource);		else			if($filetype == ".png")  				$image = @imagecreatefrompng($imagesource);			else				die();  		if(!$image) 		die();		if($watermarkType == ".gif")		$watermark = @imagecreatefromgif($watermarkPath);	else		if($watermarkType == ".png")			$watermark = @imagecreatefrompng($watermarkPath);		else			die();			if(!$watermark)		die();			$imagewidth = imagesx($image);	$imageheight = imagesy($image);  	$watermarkwidth =  imagesx($watermark);	$watermarkheight =  imagesy($watermark);	$startwidth = (($imagewidth - $watermarkwidth)/2);	$startheight = (($imageheight - $watermarkheight)/2);	imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);	imagejpeg($image);	imagedestroy($image);	imagedestroy($watermark);?>
It can be accessed by using
<img src="watermark.php?path=imagePath.ext&watermark=watermarkPath.gif> or <img src="watermark.php?path=imagePath.ext&watermark=watermarkPath.png>

edit: Removed a "." and now the entire script's correct.
Edited by WindAndWater (see edit history)

Share this post


Link to post
Share on other sites

WindAndWater,Oh good lord! That worked beautifully! Thank you. And you're right: although I would not use png at this moment (since my watermark will be simple and small) the need for making a script that can be adaptable is very crucial to a perfect script.Thanks again!

Share this post


Link to post
Share on other sites

hmmm interesting script but from what I search up on this type of script you can use gd support and also htaccess as well to make your water makr even more dynamic and what not.

here are some example links
htaccess version

gd support

Share this post


Link to post
Share on other sites

edit: Removed a "." and now the entire script's correct.

WindAndWater,

Where did you remove your "dot"? Because the first script worked just fine form me. If you are referring to JPEG, who actually uses JPEG extension today except for avi purpose?

Share this post


Link to post
Share on other sites

Yup I removed the . so that it reads "jpeg" as opposed to ".jpeg" which is 5 characters long, so it will never match a 4 character extension. Truthfully probably no-one uses .jpeg anymore, but it's an old habit that I haven't kicked yet. :-)

Share this post


Link to post
Share on other sites

Hi,

I am storing the images in database and the codes is following :

<?	include "connection.php";	$sql="select image from sportsevent where id='$_GET[id]'";	$rs=mysql_query($sql);	$row=mysql_fetch_object($rs);	header("(anti-spam-(anti-spam-content-type:)) image/jpg");	echo $row->image;	mysql_free_result($rs);?>

Could you tell me how to watermark images while outputting image in this way???

Thanks
Edited by jlhaslip
added code tags (see edit history)

Share this post


Link to post
Share on other sites

so this basically covers your image with a watermark using php? cool. might want to try it with the pictures i would use later in my account. thanks a lot!

Share this post


Link to post
Share on other sites

Ah good old GD library saves the day again, but I think I'm about to bring up a point that nobody has thought of yet... If you are watermarking your image so nobody else can use the original, all somebody would have to do is look at the url of the image, and tadah, they have the path of the original picture! So much for protecting your images :)I'd suggest something i bit more secure. But other than that its great!

Share this post


Link to post
Share on other sites

Oh haha, my bad! :) I saw the example of the path of the image in the url and I thought that it meant it was actually accessing that url to get the image.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.