这是我在watermark.php
文件:
<?php
// loads a png, jpeg or gif image from the given file name
function imagecreatefromfile($image_path) {
// retrieve the type of the provided image file
list($width, $height, $image_type) = getimagesize($image_path);
// select the appropriate imagecreatefrom* function based on the determined
// image type
switch ($image_type)
{
case IMAGETYPE_GIF: return imagecreatefromgif($image_path); break;
case IMAGETYPE_JPEG: return imagecreatefromjpeg($image_path); break;
case IMAGETYPE_PNG: return imagecreatefrompng($image_path); break;
default: return \'\'; break;
}
}
// load source image to memory
$image = imagecreatefromfile($_GET[\'image\']);
if (!$image) die(\'Unable to open image\');
// load watermark to memory
$watermark = imagecreatefromfile($_GET[\'watermark\']);
if (!$image) die(\'Unable to open watermark\');
// calculate the position of the watermark in the output image (the
// watermark shall be placed in the lower right corner)
$watermark_pos_x = imagesx($image) - imagesx($watermark) - 8;
$watermark_pos_y = imagesy($image) - imagesy($watermark) - 10;
// merge the source image and the watermark
imagecopy($image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0,
imagesx($watermark), imagesy($watermark));
// output watermarked image to browser
header(\'Content-Type: image/jpeg\');
imagejpeg($image, \'\', 100); // use best image quality (100)
// remove the images from memory
imagedestroy($image);
imagedestroy($watermark);
?>
然后变成.htaccess
文件:
RewriteEngine on
RewriteCond %{REQUEST_URI} (800x...|...x800)\\.(png|jpe?g|gif)
RewriteRule ^([^tn].*\\.(gif|jpg|png))$ /wp-content/uploads/watermark.php?image=$1&watermark=watermark.png [NC]
放置
watermark.php
+
.htaccess
+
watermark.png
进入
wp-content/uploads
文件夹(或任何其他位置,如果您调整
.htaccess
行)。
这里是。htaccess获取名称中包含800x或x800的jpg、png或gif请求(仅在大图像上应用水印),将其重定向到watermark.php
在图像上添加水印的脚本。
这是一个初学者,您应该通过定义要在中应用的规则来适应您的需要.htaccess
文件对于我们的案例,我们在媒体选项中定义了800x800大小。