Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Watermark PHP
<img src="watermark.php?watermark=watermark.png&src=test.jpg" />
<?php
// check to see if the files exist
if(!file_exists($_GET['src'])){
die('Image does not exist.');
}
if(!file_exists($_GET['watermark'])){
die('Watermark does not exist.');
}
$src = $_GET['src'];
// create the proper image from the filetype
switch(end(explode('.', $src))){
case 'gif':
$image = imagecreatefromgif($src);
break;
case 'jpg':
case 'jpeg':
$image = imagecreatefromjpeg($src);
break;
case 'png':
$image = imagecreatefrompng($src);
break;
default:
die('Not a valid image type.');
}
$watermark_src = $_GET['watermark'];
// create the proper image from the watermark filetype
switch(end(explode('.', $watermark_src))){
case 'png':
$watermark = imagecreatefrompng($watermark_src);
break;
case 'gif':
$watermark = imagecreatefromgif($watermark_src);
default:
die('Not a valid watermark image.');
}
// image size
$image_width = imagesx($image);
$image_height = imagesy($image);
// watermark size
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
// position the watermark
// center center
$start_width = (($image_width - $watermark_width) / 2);
$start_height = (($image_height - $watermark_height) / 2);
// top left
//$start_width = 0;
//$start_height = 0;
// bottom right
//$start_width = ($image_width - $watermark_width);
//$start_height = ($image_height - $watermark_height);
// create the image
imagecopy($image, $watermark, $start_width, $start_height, 0, 0, $watermark_width, $watermark_height);
// and display it
header('content-type: image/jpeg');
imagejpeg($image);
// cleanup
imagedestroy($image);
imagedestroy($watermark);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.