前言
今天在别人群里有人提的需求,然后我觉得他说的对,但是他用的不是WordPress,只好自己搞一下,由于本站用的腾讯云的对象存储COS,就先以腾讯云为例,其他平台以后再说
效果
腾讯云
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');
已开启创作声明
作者已开启创作声明,代表内容为独立创作
允许规范转载
可对作品内容进行复制和转载,但需注明作品作者、出处
禁止转载或摘编
不得对作品内容进行复制和转载
没有又拍云的吗?
给个文档
大大目前不支持b2的圈子吗
在搞了在搞了
已烂尾
应该展示一张图展示效果的