
功能介绍:不要第三方插件,用通过修改WordPress主题代码来实现自动加水印功能,你需要将代码放置在主题的functions.php文件中就可以了。
具体操作步骤如下:
1、先上传水印图片,最好是透明的,上传到主题目录下面的images文件里。例如,如果你的主题目录是wp-content/themes/your-theme,则水印图片应该放在wp-content/themes/your-theme/images/目录下,并命名为watermark.png。
2、找到主题下面的functions.php,将下面代码复制粘贴到最后!
add_filter('wp_generate_attachment_metadata', 'add_watermark_to_image');
function add_watermark_to_image($metadata) {
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['basedir'] . '/' . $metadata['file'];
// 只对主图添加水印(不处理缩略图)
$image = imagecreatefromjpeg($file_path);
$watermark = imagecreatefrompng(get_template_directory() . '/images/watermark.png');
$img_width = imagesx($image);
$img_height = imagesy($image);
$wm_width = imagesx($watermark);
$wm_height = imagesy($watermark);
// 水印位置:右下角(根据自身需求修改间距)
$dest_x = $img_width - $wm_width - 10;
$dest_y = $img_height - $wm_height - 10;
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $wm_width, $wm_height);
imagejpeg($image, $file_path);
imagedestroy($image);
imagedestroy($watermark);
return $metadata;
}
3、保存functions.php文件,在文章页面上传图片就可以看到水印了!
wordpress建站,如何实现多个网站调用的文章图片来自同一个文件夹
标签: js水印, wordpress入门
上面是“wordpress建站,不要第三方插件实现图片自动添加水印(亲测有效)”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_21386.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

网站外链过多会被百度降权吗
推荐一款可免费商用的音效素材下载网站——Freesound(音频片段、样本、录音等)
小冰冰传奇
一款来自巴黎的基于人工智能的照片编辑应用——Photoroom
化妆品类网站模板(html+css+jquery)
推荐一款免费可商用英文字体——Atkinson Hyperlegible