如何为WordPress建站添加水印?
使用水印插件或自定义代码,您可以为WordPress建站中的图片自动添加水印,保护版权。
-
玖比肆 评论以下是添加水印的步骤:
1. 使用水印插件。 在WordPress中有许多免费和付费的水印插件。 您可以搜索并安装适合您需求的插件,然后按照插件的说明进行配置。
2. 自定义代码。 如果您熟悉代码,可以使用自定义代码实现添加水印。 首先,您需要将水印图像上传到您的WordPress媒体库中。 然后可以添加以下代码到您的主题函数文件(functions.php)中:
```php
function add_watermark( $attachment_ID ) {
$attachment = get_post( $attachment_ID );
$mime_type = $attachment->post_mime_type;// check if this attachment is an image
if ( strpos( $mime_type , 'image' ) === false )
return;// get the path to the original image
$file_path = get_attached_file( $attachment_ID );// load the original image
$image = wp_get_image_editor( $file_path );// add the watermark
$watermark_path = '/path/to/your/watermark/image.png';
$watermark = wp_get_image_editor( $watermark_path );$image_width = $image->get_size()['width'];
$image_height = $image->get_size()['height'];
$watermark_width = $watermark->get_size()['width'];
$watermark_height = $watermark->get_size()['height'];$offset_x = $image_width - $watermark_width - 10; // 10 is the margin
$offset_y = $image_height - $watermark_height - 10;$image->composite( $watermark, $offset_x, $offset_y, $watermark_width, $watermark_height );
// save the image
$image->save( $file_path );
}add_action( 'add_attachment', 'add_watermark' );
```这个代码将在每次上传图片时自动为图片添加水印。 您只需将代码中的 `$watermark_path` 更改为您水印图像的路径即可。
注意:使用自定义代码时,请始终备份并测试您的网站,以避免破坏现有功能。
2023年 4月 26日 上午10:31
