суббота, 20 июля 2013 г.

Sign your hotlinks - Watermark on the Fly in PHP

Yon need PHP 4+ and GD 2.0+ on your server
<?php  

header('content-type: image/jpeg');  

$watermark = imagecreatefrompng('watermark.png');  

$watermark_width = imagesx($watermark);  

$watermark_height = imagesy($watermark);  

$image = imagecreatetruecolor($watermark_width, $watermark_height);  

$image = imagecreatefromjpeg($_GET['src']);  

$size = getimagesize($_GET['src']);  

$dest_x = $size[0] - $watermark_width - 5;  

$dest_y = $size[1] - $watermark_height - 5;  

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);  

imagejpeg($image);  

imagedestroy($image);  

imagedestroy($watermark);  

?>

“RewriteCond $1 ^.*.(jpg|jpeg|gif|png|bmp)$”

Or with no rewritecond the rewriterule should be:

“RewriteRule ^.*.(jpg|jpeg|gif|png|bmp)$ /scripts/watermark.php?src=%{REQUEST_URI} [L]”

Variant 2
 <?php
// this script creates a watermarked image from an image file - can be a .jpg .gif or .png file
// where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory as this script
// where this script is named watermark.php
// call this script with an image tag
// <img src="watermark.php?path=imagepath"> where path is a relative path from the document root - such as subdirectory/image.jpg
$imagesource = $_SERVER['DOCUMENT_ROOT']."/".$_GET['path'];
if (!file_exists($imagesource)) die();
$filetype = strtolower(substr($imagesource,strlen($imagesource)-4,4));
if($filetype == ".gif") $image = @imagecreatefromgif($imagesource); 
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource); 
if($filetype == ".png") $image = @imagecreatefrompng($imagesource); 
if (empty($image)) die();
$watermark = @imagecreatefromgif('watermark.gif');
$imagewidth = imagesx($image);
$imageheight = imagesy($image); 
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?> 

RewriteEngine On
RewriteCond %{REQUEST_URI} !error.gif$
RewriteRule \.(gif|jpg|png)$ /error.gif [L] 

Комментариев нет:

Отправить комментарий