I'm currently rebuilding my website after my server room got flooded, or I would just link you to my site. But I'll copy paste below a tutorial I made on watermarking. I wrote this a long time ago, so feel free to ask questions if it makes no sense. Also, this assumes the watermark is a premade image, however it could be a dynamically generated image just as easily. Hope it helps.
I find when opening files it generally works better if you use full paths instead of relative paths as it makes it easier to update your folder structure. Of course this is personal preference, however, the way I choose to do it is dirname() with the FILE constant as the argument. I then set the path with filename for the image and the watermark.
$folder = dirname(__FILE__) . DIRECTORY_SEPARATOR;
$base = $folder . 'base.jpg';
$watermark = $folder . 'watermark.png';
Next we need to get the dimensions of the image and watermark to determine how big the watermark should be. We get the dimensions via getimagesize, then assign them to their own variables using the list function.
list($basew,$baseh) = getimagesize($base);
list($wmw,$wmh) = getimagesize($watermark);
For this tutorial, I want the watermark to be no bigger than half the width or height of the base image. So I determine which is less and then create a scale based on that. I then multiply the scale by the watermark’s measurements to determine what the size of the watermark should be on the image itself.
if( $basew > $baseh ) {
$wmscale = ($basew/2)/$wmw;
} else {
$wmscale = ($baseh/2)/$wmh;
}
$newwmw = $wmw*$wmscale;
$newwmh = $wmh*$wmscale;
Next we need to resize the watermark to the proper scale to put it on the image. We start by creating two images, one from the original watermark, and one the size of the new watermark. We open the original watermark using imagecreatefrompng and the new watermark using imagecreate and the new dimensions. We then use imagecopyresized to copy the watermark and scale it into the new watermark and destroy the opened original watermark. Note: it merely destroys the resources used by opening the file it does NOT remove it from the file system.
$wmimg = imagecreatefrompng($watermark);
$newwmimg = imagecreate($newwmw,$newwmh);
imagecopyresized($newwmimg,$wmimg,0,0,0,0,$newwmw,$newwmh,$wmw,$wmh);
imagedestroy($wmimg);
For this tutorial we are placing the watermark in the center of the image, so we need to determine where watermark will be placed. The offset is determined as the distance along the x-axis and y-axis with the point of origin being the top left corner. So we will open the base image using imagecreatefromjpeg, calculate the x and y offsets to use, copy the watermark to the proper spot on the image, and destroy the watermark.
$baseimg = imagecreatefromjpeg($base);
$dst_x = ($basew/2) - ($newwmw/2);
$dst_y = ($baseh/2) - ($newwmh/2);
imagecopy($baseimg,$newwmimg,$dst_x,$dst_y,0,0,$newwmw,$newwmh);
imagedestroy($newwmimg);
Finally, we output the image header, the image itself and then destroy the image to free up the final resources. It is important to note that we always destroy the resources we open so they don’t get left behind.
header('Content-type: image/png');
imagepng($baseimg);
imagedestroy($baseimg);
Now we can obviously alter this so that it works for more than just one picture. For example if you want to store filenames in a database, you could pass an image ID via a query string, and then look up that particular image in the script. Also this would be called within an html tag, and would be the target src IE: <img src=”watermark.php” /> or when choosing an image via query string: <img src=”watermark.php?image=123” />.