首页 » 未分类 » 正文

让Watermark RELOADED支持使用图片做水印(原生只能使用文字)

发布者:站点默认
2012/01/12 浏览数(1,733) 分类:未分类 让Watermark RELOADED支持使用图片做水印(原生只能使用文字)已关闭评论

说明:

Watermark RELOADED 可以在写文章上传图片后为图片添加文字版的水印。为了让其支持图片版的水印,所以有了此文。下边效果预览部分的红框即为添加的功能(有待完善,但功能正常)。

效果预览:

支持图片水印的的Watermark RELOADED

支持图片水印的的Watermark RELOADED

注意:

此非正式版,有不少细节需要修改,比如图片不能预览。但整体功能可以正常使用。

后台设置中的水印文件位置相对于插件根目录,不支持上传、不支持以“http”开头的文件,可以输入是“watermark/1.png”或“watermark.png”,开头不需要“/”。

具体操作:

需要修改“watermark-reloaded.php”文件中的代码、水印需要入到插件的根目录可根目录的某个文件夹中,名称可自定。

打开“watermark-reloaded.php”共五处修改(修改一个属性、三个方法、添加一个方法)。三个需要修改的方法中一个是后台设置、一个为图片水印的开关、一个为图片水印时修正水印位置,添加的一个方法为生成图片版的水印。

第〇处:为数组属性“$_options”添加两个元素,修改后代码为:

protected $_options             = array(
	'watermark_type'     => 1, // 0:text,1:image 这个是新加的
	'watermark_file'     => 'watermark.png', // 这个是新加的
	'watermark_donated'  => 0,
	'watermark_hide_nag' => 0,
	'watermark_on'       => array(),
	'watermark_position' => 'bottom_right',
	'watermark_offset'   => array(
		'x' => 5,
		'y' => 5
	),

第一处:找到“doWatermark”方法中的“$this->imageAddText($image, $opt);”修改为(“createPreview”方法中也有一处,不需要修改):

// And finaly write text to image
if ($this->get_option('watermark_type')){
	$this->imageAddImg($image, $options);
}else{
	$this->imageAddText($image, $opt);
}

第二处:在方法“calculateBBox”开头添加以下代码:

private function calculateBBox(array $opt) { // 这行不需要添加,主要是方便大家定位
	if (!empty($opt['watermark_type'])){
		$src = dirname(__FILE__).'/'.$this->get_option('watermark_file');
		list($width, $height) = getimagesize($src);
		$bbox = array(
			'bottom_left'  => array(
				'x' => 0,
				'y' => $height
			),
			'bottom_right' => array(
				'x' => $width,
				'y' => $height
			),
			'top_right'    => array(
				'x' => $width,
				'y' => 0
			),
			'top_left'     => array(
				'x' => 0,
				'y' => 0
			)
		);
		
		$bbox['width']  = $width;
		$bbox['height'] = $height;
		return $bbox;
	}

第三处:随便在类“Watermark_Reloaded”里找个位置添加方法“imageAddImg”,代码:

private function imageAddImg($image, array $opt) {
	$src = dirname(__FILE__).'/'.$this->get_option('watermark_file');
	$info = getimagesize($src);
	$width = $info[0];
	$height = $info[1];
	switch ($info['mime']){
		case "image/jpeg" :
			$src = imagecreatefromjpeg($src);
			$ext_name = 'jpg';
			break;
		case 'image/gif' :
			$src = imagecreatefromgif($src);
			$ext_name = 'gif';
			break;
		case 'image/png' :
			$src = imagecreatefrompng($src);
			$ext_name = 'png';
			break;
		default :
			$src = imagecreate($width,$height);
			imagecolorallocate($src,0xff,0xff,0xff);
	}
	$offset = $this->calculateOffset($image,$opt);
	imagealphablending($image, true);
	imagecopy($image, $src, $offset['x'], $offset['y'], 0, 0, $width, $height);
	return $image;
}

第四处:找到“optionsPage”方法中HTML部分的代码,在table标签后添加以下代码(用于WP后台设置):

<h3>图片水印</h3>
<table class="form-table">
	<tr valign="top">
		<th scope="row">使用图片水印代替文字水印</th>
		<td class="wr_width">
			<fieldset class="wr_width">
			<legend class="screen-reader-text"><span>启用图片水印?</span></legend>
			<?php $watermark_type = $this->get_option('watermark_type');?>
			<input name="watermark_type" type="radio" value="1" <?php if ($watermark_type) echo ' checked="checked" ';?> />是
			<input name="watermark_type" type="radio" value="0" <?php if (empty($watermark_type)) echo ' checked="checked" ';?> />否
			
			</fieldset>
		</td>
	</tr>
	<tr valign="top">
		<th scope="row">图片网址</th>
		<td class="wr_width">
			<fieldset class="wr_width">
			<legend class="screen-reader-text"><span>图片网址</span></legend>
			<input style="width:100%;" name="watermark_file" type="text" value="<?php echo $this->get_option('watermark_file');?>" />
			</fieldset>
		</td>
	</tr>
	<!-- 这里先不要了,图片预览还不完善
	<tr valign="top">
		<th scope="row">图片预览</th>
		<td class="wr_width">
			<fieldset class="wr_width">
			<legend class="screen-reader-text"><span>图片预览</span></legend>
			<img src="<?php echo dirname(__FILE__).'/'.$this->get_option('watermark_file');?>" />
			</fieldset>
		</td>
	</tr>
	-->
</table>

<完>

点击返回顶部
  1. 留言
  2. 联系方式