<?

define("WM","watermark.png"); // soubor s vodoznakem (png)
define("QUALITY", 90); // kvalita výsledného JPEGu
define("IMG",$_GET["img"]); // soubor, na který se aplikuje vodoznak




ob_start();

($img = imagecreatefromjpeg(IMG)) || error("image not found");


($foreground = imagecreatefrompng(WM)) || error("watermark not found");

$imageWidth = imagesx($img);
$imageHeight = imagesy($img);

$insertWidth = imagesx($foreground);
$insertHeight = imagesy($foreground);

$overlapX = $imageWidth-$insertWidth-0;
$overlapY = $imageHeight-$insertHeight-0;

if(!imagecopy( 
			$img,
			$foreground,
			$overlapX,
			$overlapY,
			0,
			0,
			$insertWidth,
			$insertHeight)) error("cannot imagecopy()");


imagejpeg(
		$img, 
		NULL,
		QUALITY
		) || error("cannot imagejpeg()");

header("Content-Type: image/jpeg");
header('Content-Disposition: inline; filename="'.basename(IMG).'"');
ob_flush();
ob_end_clean();


function error($err=null){
//	header("HTTP/1.0 404 Not Found");
	die("<h1>404 not found</h1>$err");
}

?>
