如何为WordPress建站添加水印?

玖比肆 WordPress 95

使用水印插件或自定义代码,您可以为WordPress建站中的图片自动添加水印,保护版权。

回复

共1条回复 我来回复
  • 玖比肆的头像
    玖比肆 评论

    以下是添加水印的步骤:

    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 0条评论
微信客服
分享本页
返回顶部
玖比肆(9Bi4)专注于帮助企业实现数字化转型,提供全方位的网站建设、网络运营、网络推广、网络营销和SEO优化等一系列数字化营销服务。