Created
June 23, 2011 18:40
-
-
Save mloberg/1043237 to your computer and use it in GitHub Desktop.
Watermark PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<img src="watermark.php?watermark=watermark.png&src=test.jpg" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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