loader
Loading···

用对象存储为wordpress实现不同用户投稿图片不同水印

前言

今天在别人群里有人提的需求,然后我觉得他说的对,但是他用的不是WordPress,只好自己搞一下,由于本站用的腾讯云的对象存储COS,就先以腾讯云为例,其他平台以后再说

效果

用对象存储为wordpress实现不同用户投稿图片不同水印

腾讯云

function custom_modify_image_url($content) {
    global $post;

    // 获取文章作者的昵称
    $author_id = $post->post_author;
    $author_info = get_userdata($author_id);
    $author_nickname = '替换为你网站的名字@'.$author_info->display_name;
    //将$author_nickname进行base64编码
    $encoded_author_name = base64_encode($author_nickname);
    //因为对象存储使用的url进行处理,所以我们需要把编码后的字符中的+替换为-,以保证不会乱码
    $encoded_author_name = str_replace(['+', '/'], ['-', '_'],$encoded_author_name);

    // 找到文章中的图片URL并进行替换https:\/\/cos\.uocin\.com\/为你的对象存储地址/前面需要加\
    $pattern = '/<img([^>]*)src=["\']https:\/\/cos\.uocin\.com\/([^"\']*)["\']([^>]*)>/i';
    //https://cos.uocin.com/替换成你的域名不需要加\,字体和颜色大小位置,请根据对象存储的文档进行修改
    $replacement = '<img$1src="https://cos.uocin.com/$2?imageMogr2/format/webp/interlace/0/quality/80|watermark/2/text/' .$encoded_author_name . '/font/dGFob21hLnR0Zg/fontsize/48/fill/IzAwMDAwMA/dissolve/90/shadow/30/gravity/southeast/dx/20/dy/20"$3>';
    $content = preg_replace($pattern, $replacement, $content);

    return $content;
}

add_filter('the_content', 'custom_modify_image_url');

这边我试了一下,子比和B2都是正常的,b2的帖子页面没有,子比的帖子功能正常,将上面的代码放入functions.php即可

阿里云

function custom_modify_image_url($content) {
    global $post;

    // 获取文章作者的昵称
    $author_id = $post->post_author;
    $author_info = get_userdata($author_id);
    $author_nickname = '替换为你网站的名字@'.$author_info->display_name;
    //将$author_nickname进行base64编码
    $encoded_author_name = base64_encode($author_nickname);
    //因为对象存储使用的url进行处理,所以我们需要把编码后的字符中的+替换为-、/替换为_,以保证不会乱码
    $encoded_author_name = str_replace(['+', '/'], ['-', '_'],$encoded_author_name);

    // 找到文章中的图片URL并进行替换https:\/\/cos\.uocin\.com\/为你的对象存储地址/和.点前面需要加\
    $pattern = '/<img([^>]*)src=["\']https:\/\/xxxx\.aliyuncs\.com\/([^"\']*)["\']([^>]*)>/i';
    //https://cos.uocin.com/替换成你的域名不需要加\,字体和颜色大小位置,请根据对象存储的文档进行修改
    $replacement = '<img$1src="https://xxxx.aliyuncs.com/$2@watermark=2&text=' .$encoded_author_name . '其他参数请在此处添加"$3>';
    $content = preg_replace($pattern, $replacement, $content);

    return $content;
}

add_filter('the_content', 'custom_modify_image_url');
已开启创作声明,禁止转载或摘编

给TA买糖
共{{data.count}}人
人已买糖
6 条回复 UP文章作者
  • 小黄脸
  • AC娘
  • 米游姬
  • 蔚蓝档案
  1. 肆柒玖

    没有又拍云的吗? :g16:

  2. user76

    大大目前不支持b2的圈子吗 :a15:

  3. 米游资讯姬

    应该展示一张图展示效果的