我想知道是否可以通过使用.htaccess添加水印来保护主机中从外部加载的图像
也就是说,如果另一个网站在他们自己的网站中的img标签中使用了我的图片URL http://example.com/1.jpg。
我的计划是,当外部请求到达我的主机时,我会添加一个水印,并将其发送给浏览该外部站点的用户。
发布于 2012-01-28 17:16:17
你基本上想要做的是,从这个教程开始:
http://www.alistapart.com/articles/hotlinking/
这将向您展示如何将来自外部站点的图像重定向到PHP页面。然后,您可以使用该PHP页面为您的图像添加水印,如下所示:
<?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['pic']);
$size = getimagesize($_GET['pic']);
$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);
?>发布于 2012-01-28 17:13:13
.htaccess不能向您的图像添加水印。但是,它可能会限制对您的图像的访问。
但是,您可以使用PHP GD Library添加水印。下面的代码展示了如何通过PHP GD向您的图像添加水印。
<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>$stamp是你想要在水印上的内容(例如:版权保留),$im是你的实际图像,它必须受到保护。
发布于 2014-03-04 06:51:10
您必须使用HTTP_REFERER来确定用户是否来自哪个链接,然后将其与您自己的域进行比较。如果请求域不同,则使用php gd lib在您的图像上添加水印。您应该检查是否设置了HTTP_REFERER。默认情况下,如果用户不是从另一个页面直接访问您的站点,它将是空的,并且不会进行设置。
if(isset($_SERVER['HTTP_REFERER'])) {
#check if its from external domin
# do something here
}https://stackoverflow.com/questions/9044054
复制相似问题