WordPress上传图片添加水印免插件方法

1.在主题目录里面新建class文件夹,然后把 image.php 文件放进去

2.打开 functions.php 文件,添加下面的代码:

/**
* WordPress上传图片添加水印
*/
function uimoban_watermark($attachment_ID)
{
$attachment = get_post($attachment_ID);
switch($attachment->post_mime_type){
case 'image/jpeg':
case 'image/png':
case 'image/gif':
require THEME_FILES . '/class/image.php';
$image = new image(true);
$image->set('watermark.png', 1);
$image->watermark($attachment->guid);
break;
default:return ;
}
}
add_action('add_attachment', 'uimoban_watermark');

说明:$image->set(‘watermark.png’, 1); 中watermark.png指定的是水印图片,和第一步代码中的data/watermark/目录下的watermark.png应该保持一致,当然你也可以自行修改。“1”代表水印位置。