<?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?src=imagepath"> where path is a relative path such as subdirectory/image.jpg
$imagesource = $_GET['src'];
// this is where your sql login details go.
$dbserver = 'localhost';
$dbuser = '';
$dbpass = '';
$dbname = '';
/*
##### CREATE THIS TABLE FIRST: #####
CREATE TABLE IF NOT EXISTS `HotlinkTracking` (
`date` datetime NOT NULL,
`referrer` text NOT NULL,
`image` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
*/
$link = mysql_connect($dbserver, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$mysqldate = date( 'Y-m-d H:i:s' );
$referrer = $_SERVER['HTTP_REFERER'];
mysql_query("INSERT INTO HotlinkTracking
(referrer, date, image) VALUES('{$referrer}', '{$mysqldate}','{$imagesource}' ) ")
or die(mysql_error());
$watermarkPath = 'watermark.png';
$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);
$startwidth = 0;
$startheight = 0;
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>