HTACCESS 配合 PHP 给图片加上水印

利用apache的.htaccess、php做wrapper来给图片加上水印
首先在要加水印图片的目录,例如/var/www/html/pic目录下写一个.htacess文件

AddHandler watermarked .jpg
AddHandler watermarked .jpeg
AddHandler watermarked .gif
AddHandler watermarked .png
Action watermarked /watermark/wrapper.php
这样将所有jpg/gif/png文件请求forward到/watermark/wrapper.php。由于用到了AddHandle,需要在apache的配置文件httpd.conf里相应目录的AllowOverride一项,允许FileInfo,例如
<Directory "/var/www/html">
  Options Indexes FollowSymLinks
  AllowOverride FileInfo
  Order allow,deny
  Allow from all
</Directory>
然后在/var/www/html下建立watermark目录,写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);
   
$watermarkType = getFileType($watermark);

   if (empty(
$outputType)) $outputType = $sourceType;
   if (
$outputType == "gif") $outputType = "png"; // Okay to remove after July 2004
   
header("Content-type:image/$outputType");

   
// We're only looking at the file name, not the directory name
   
$fileName = basename($source);

   
// Match dots "."
   
preg_match_all("/(.)/", $fileName, $dots);

   
// We only care about match #1
   
$dots = $dots[1];

   if (
count($dots) > 1) {
      
// More than one dot, so don't do watermarking

      
$fp = fopen($source, "rb");
      
$contents = fread($fp, filesize($source));
      
fclose($fp);
      echo
$contents;
   }

   else {

      
// Continue with watermarking

      // Derive function names
      
$createSource = "ImageCreateFrom".strtoupper($sourceType);
      
$showImage = "Image".strtoupper($outputType);
      
$createWatermark = "ImageCreateFrom".strtoupper($watermarkType);

      
// Load original and watermark to memory
      
$output = $createSource($source);
      
$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);

      
// Display
      
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";
   return
$type;
}
?>
要加上去的图片 watermark.png也放在该目录下,也可以在别的目录,但是需要修改$watermark这个变量。然后用浏览器访问pic下的一个图片看看是不是加上了水印:),缺省是加在右下角,要改成别的方式得改改这个php文件。
这样原图可以没有水印的,只是在被请求的时候再加上水印,另外可以再wrapper.php进行一些判断,比如判断HTTP referer (php的变量是$_SERVER["HTTP_REFERER"])看请求是否来自本服务器。
参考连接:http://www.webpronews.com/webdevelopment/basicdevelopment/wpn-37-20040429HTACCESSWrapperswithPHP.html
这个里面还讲到用.htaccess和php来添加头/尾文件,提供个人主页服务的跳转之前输出的东西应该也是用类似的技术实现的吧
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值