Last active
June 12, 2020 16:22
-
-
Save joetm/f9e359ffedebf2ed757a to your computer and use it in GitHub Desktop.
Watermark images on-the-fly with htaccess
This file contains 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
/* | |
# add this to .htaccess | |
# AddHandler watermarked .thumb | |
# AddHandler watermarked .attach | |
AddHandler watermarked .jpg | |
Action watermarked /home/pornster/cpattachments/wrapper.php | |
*/ | |
<?php | |
$watermark = "watermark.png"; | |
$image = $_SERVER["PATH_TRANSLATED"]; | |
if (empty($image)) die(); | |
if (!file_exists($image)) { | |
header("404 Not Found"); | |
echo "File Not Found."; | |
die(); | |
} | |
$outputType = getFileType($image); | |
watermark($image, $watermark, $outputType); | |
/** | |
Outputs the image $source with $watermark in the lower right corner. | |
@param $source the source image | |
@param $watermark the watermark to apply | |
@param $outputType the type to output as (png, jpg, gif, etc.) | |
defaults to the image type of $source if left blank | |
*/ | |
function watermark($source, $watermark, $outputType="") { | |
$sourceType = getFileType($source); | |
if (empty($outputType)) $outputType = $sourceType; | |
header("Content-type:image/$outputType"); | |
// Derive function names | |
$createSource = "ImageCreateFrom".strtoupper($sourceType); | |
$showImage = "Image".strtoupper($outputType); | |
// Load original and watermark to memory | |
$output = $createSource($source); | |
$w = ImageSX($output); | |
$h = ImageSY($output); | |
//if ($w<388 || $h<500){ | |
// $showImage($output); | |
//} | |
//else { | |
$watermarkType = getFileType($watermark); | |
$createWatermark = "ImageCreateFrom".strtoupper($watermarkType); | |
$logo = $createWatermark($watermark); | |
ImageAlphaBlending($output, true); | |
// Find proper coordinates so watermark will be in the lower right corner | |
$x = ImageSX($output) - ImageSX($logo); | |
$y = ImageSY($output) - ImageSY($logo); | |
ImageCopy($output, $logo, $x, $y, 0, 0, ImageSX($logo), ImageSY($logo)); | |
$showImage($output); | |
//} | |
// Purge | |
ImageDestroy($output); | |
ImageDestroy($logo); | |
} | |
function getFileType($string) { | |
$type = strtolower(eregi_replace("^(.*)\.","",$string)); | |
if ($type == "jpg") $type = "jpeg"; | |
if ($type == "thumb") $type = "jpeg"; | |
if ($type == "attach") $type = "jpeg"; | |
return $type; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment