Watermark Images

turt2live 1 Tallied Votes 521 Views Share

Simply enough:

Call the function, get the returned filename.

Function Variables:

$file : The input file (source/background)
$destination : Where you want the new watermarked image to go
$overlay : The overlay image (foreground), this is default set (if not passed in) to "images/watermark.png"
$X : X-Position of overlay (default is 10)
$Y : Y-Position of overlay (default is 10)


The return is the destination file (this allows for dynamic watermarks)

Example Usage:

<img src='<?php echo watermark_image("sample.png", "temp/".time().".png"); ?>'>
madCoder commented: Nice. Bookmarked this for future reference. +5
<?php
function watermark_image($file, $destination, $overlay = "images/watermark.png", $X = 10, $Y = 10){
	$watermark =		imagecreatefrompng($overlay);
	$source = 		getimagesize($file);
	$source_mime = 		$source['mime'];
	$source_x = 		$source['width'];
	$source_y = 		$source['height'];
	
	if($source_mime == "image/png"){
		$image = imagecreatefrompng($file);
	}else if($source_mime == "image/jpeg"){
		$image = imagecreatefromjpeg($file);
	}else if($source_mime == "image/gif"){
		$image = imagecreatefromgif($file);
	}
	
	imagecopy($image, $watermark, $X, $Y, 0, 0, imagesx($watermark), imagesy($watermark));
	imagepng($image, $destination);
	
	return $destination;
}
?>
turt2live 5 Junior Poster in Training

If you would like a sample: Click here

webatz 0 Newbie Poster

zz thank very much
i need it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.