[英].htaccess Rewriterule Regex to apply only to files with certain words in it
我的.htaccess
文件中包含以下代码,该代码在所有WordPress上传文件上都添加了水印。 我正在尝试找出如何更改正则表达式以使其仅适用于以特定文件名结尾的文件的方法。
RewriteRule (.*)wp-content/uploads/(.*\.(jpe?g|gif|png))$ $1watermark.php?p=br&q=90&src=wp-content/uploads/$2
如果/ uploads /目录中有以下文件结尾的文件:ORIGINAL.jpg(完整文件名sunset-ORIGINAL.jpg)或ORIGINAL.gif,我只希望那些文件应用水印。
我可以通过修改.htaccess
重写规则来做这件事吗,还是应该尝试在watermark.php
文件中实现它?
//we tell the server to treat this file as if it wore an image
header('Content-type: image/jpeg');
//image file path
$img = $_GET['src'];
//watermark position
$p = $_GET['p']; if(!$p) $p = 'br';
/*
p can be anything from the following list:
tl = top left
tc = top center
tr = top right
cl = center left
c = center of the image
cr = center right
bl = bottom left
bc = bottom center
br = bottom right
*/
//watermarked image quality
$q = $_GET['q'];
//if the quality field is missing or is not on the 0 to 100 scale then we set the quality to 93
if(!$q || $q<0 || $q>100) $q = '93';
$filetype = substr($img,strlen($img)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif") $image = @imagecreatefromgif($img);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($img);
if($filetype == ".png") $image = @imagecreatefrompng($img);
if (!$image) die();
//getting the image size for the original image
$img_w = imagesx($image);
$img_h = imagesy($image);
//if the filename has 150x150 in it's name then we don't apply the watermark
if (eregi("150x150", $img)) {
imagejpeg($image, null, $q); die();
} else {
$watermark = @imagecreatefrompng('watermark.png');
}
/*
//if you want to use the watermark only on bigger images then use this instead of the condition above
if ($img_w < "150") {//if image width is less then 150 pixels
imagejpeg($image, null, $q); die();
} else {
$watermark = @imagecreatefrompng('watermark.png');
}
*/
//getting the image size for the watermark
$w_w = imagesx($watermark);
$w_h = imagesy($watermark);
if($p == "tl") {
$dest_x = 0;
$dest_y = 0;
} elseif ($p == "tc") {
$dest_x = ($img_w - $w_w)/2;
$dest_y = 0;
} elseif ($p == "tr") {
$dest_x = $img_w - $w_w;
$dest_y = 0;
} elseif ($p == "cl") {
$dest_x = 0;
$dest_y = ($img_h - $w_h)/2;
} elseif ($p == "c") {
$dest_x = ($img_w - $w_w)/2;
$dest_y = ($img_h - $w_h)/2;
} elseif ($p == "cr") {
$dest_x = $img_w - $w_w;
$dest_y = ($img_h - $w_h)/2;
} elseif ($p == "bl") {
$dest_x = 0;
$dest_y = $img_h - $w_h;
} elseif ($p == "bc") {
$dest_x = ($img_w - $w_w)/2;
$dest_y = $img_h - $w_h;
} elseif ($p == "br") {
$dest_x = $img_w - $w_w;
$dest_y = $img_h - $w_h;
}
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $w_w, $w_h);
imagejpeg($image, null, $q);
imagedestroy($image);
imagedestroy($watermark);
我可以通过修改
.htaccess
重写规则来做这件事吗,还是应该尝试在watermark.php
文件中实现它?
从体系结构的角度来看,最好让RewriteRule
调用watermark.php
,然后将健壮性添加到watermark.php
以区分应该对哪些文件进行操作或忽略。
之所以像.htaccess
和相关的RewriteRule
逻辑一样酷,是因为它实际上仅应用于将Web请求从一件事快速定向到另一件事。 添加一层“此文件是JPEG吗? 好吧,这样做……”逻辑有点太多。 可以做到,但是现实是,所有这些都可以在一个PHP脚本中得到更好的处理。
然后,在PHP中,您可以添加您最需要确定的鲁棒逻辑级别,以决定是否可以采取措施。
好的,看一下代码,看下面几行:
$filetype = substr($img,strlen($img)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif") $image = @imagecreatefromgif($img);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($img);
if($filetype == ".png") $image = @imagecreatefrompng($img);
if (!$image) die();
$filetype
基本上是文件扩展名,对吗? 而且您只想对.gif
, .jpg
和.png
文件起作用,对吗? 好看看这一行:
if (!$image) die();
处理该问题的逻辑已经存在。 该脚本仅适用于.gif
, .jpg
和.png
文件的文件,因此无需在该侧进行任何其他工作。
但是我会说脚本中的逻辑可以改进,但这是另一个主题。 就目前而言,您可以将非图像文件发送到此脚本,因为如果该文件不是.gif
, .jpg
和.png
文件,它将只是die()
。
编辑:好的,我在您的评论中看到您希望watermark.php
脚本仅对名称末尾且扩展名前具有名称ORIGINAL
文件起作用,对吗? 然后,您可以像这样修改watermark.php
。 首先,找到以下行:
//image file path
$img = $_GET['src'];
然后添加一个正则表达式来检查.gif
, .jpg
和.png
文件末尾的ORIGINAL
,如下所示:
//image file path
$img = $_GET['src'];
// Check if the image has a '.gif', '.jpg' or '.png' extension.
preg_match('/(?:ORIGINAL)\.(gif|jpg|png)$/', $img, $matches);
if (empty($matches)) die();
就是说,如果您问我,这个watermark.php
有一个主要问题:虽然它作用于GIF,JPEG和PNG文件,但最终产品都是JPEG文件,如顶部此行所示:
header('Content-type: image/jpeg');
这行位于底部:
imagejpeg($image, null, $q);
您确定要吗? 如果我是您,我将对该代码进行重做,以使PNG保留为PNG,GIF保留为GIF,而JPEG保留为JPEG。 但这超出了此问题的范围,对于您来说,这可能是一个不错的项目,而且您觉得需要重新处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.