<?php
!function_exists('readover') && exit('Forbidden');

function ImgWaterMark($source,$w_pos=0,$w_img='',$w_text='',$w_font=12,$w_color='#FF0000',$w_pct,$w_quality){
	global $imgdir,$db_waterfonts;
	$sourcedb = $waterdb = array();
	if (!($sourcedb = GetImgInfo($source))) {
		return false;
	}
	$ifWaterImage = 1;
	if (!($waterdb = GetImgInfo("$imgdir/water/$w_img"))) {
		if (!$w_text) return false;
		$ifWaterImage = 0;
		empty($db_waterfonts) && $db_waterfonts = 'en/PilsenPlakat';
		empty($w_font) && $w_font = 12;
		$fontsfile = "$imgdir/fonts/$db_waterfonts.ttf";
		$temp = imagettfbbox($w_font,0,$fontsfile,$w_text);//取得使用 TrueType 字体的文本的范围
		$waterdb['width'] = $temp[2] - $temp[6];
		$waterdb['height'] = $temp[3] - $temp[7];
		unset($temp);
	}
	if ($w_pos == 0) {
		$wX = rand(0,($sourcedb['width'] - $waterdb['width']));
		$wY = $ifWaterImage ? rand(0,($sourcedb['height'] - $waterdb['height'])) : rand($waterdb['height'],$sourcedb['height']);
	} elseif ($w_pos == 1) {
		$wX = 5;
		$wY = $ifWaterImage ? 5 : $waterdb['height'];
	} elseif ($w_pos == 2) {
		$wX = ($sourcedb['width'] - $waterdb['width']) / 2;
		$wY = $ifWaterImage ? 5 : $waterdb['height'];
	} elseif ($w_pos == 3) {
		$wX = $sourcedb['width'] - $waterdb['width']-5;
		$wY = $ifWaterImage ? 5 : $waterdb['height'];
	} elseif ($w_pos == 4) {
		$wX = 5;
		$wY = $ifWaterImage ? $sourcedb['height'] - $waterdb['height']-5 : $sourcedb['height']-5;
	} elseif ($w_pos == 5) {
		$wX = ($sourcedb['width'] - $waterdb['width']) / 2;
		$wY = $ifWaterImage ? $sourcedb['height'] - $waterdb['height']-5 : $sourcedb['height']-5;
	} elseif ($w_pos == 6) {
		$wX = $sourcedb['width'] - $waterdb['width']-5;
		$wY = $ifWaterImage ? $sourcedb['height'] - $waterdb['height']-5 : $sourcedb['height']-5;
	} else {
		$wX = ($sourcedb['width'] - $waterdb['width'])/2;
		$wY = $ifWaterImage ? ($sourcedb['height'] - $waterdb['height'])/2 : ($sourcedb['height'] + $waterdb['height'])/2;
	}
	imagealphablending($sourcedb['source'],true);
	if ($ifWaterImage) {
		if ($waterdb['type']=='png') {
			imagecopy($sourcedb['source'],$waterdb['source'],$wX,$wY,0,0,$waterdb['width'],$waterdb['height']);
		} else {
			imagecopymerge($sourcedb['source'],$waterdb['source'],$wX,$wY,0,0,$waterdb['width'],$waterdb['height'],$w_pct);
		}
	} else {
		if (strlen($w_color)!=7) return false;
		$R = hexdec(substr($w_color,1,2));
		$G = hexdec(substr($w_color,3,2));
		$B = hexdec(substr($w_color,5));
		//imagestring($sourcedb['source'],$w_font,$wX,$wY,$w_text,imagecolorallocate($sourcedb['source'],$R,$G,$B));
		if (strpos($db_waterfonts,'ch/') !== false && strtoupper($GLOBALS['db_charset']) != 'UTF-8') {
			$w_text = pwConvert($w_text,'UTF-8',$GLOBALS['db_charset']);
		}
		imagettftext($sourcedb['source'],$w_font,0,$wX,$wY,imagecolorallocate($sourcedb['source'],$R,$G,$B),$fontsfile,$w_text);
	}
//	P_unlink($source);
	MakeImage($sourcedb['type'],$sourcedb['source'],$source,$w_quality);
	isset($waterdb['source']) && imagedestroy($waterdb['source']);
	imagedestroy($sourcedb['source']);
}
function MakeThumb($srcFile,&$dstFile,$dstW,$dstH,$cenTer = null,$sameFile = null,$fixWH=null){
	if (!$dstW && $dstH) {
		$dstW = $dstH;
	} elseif (!$dstH && $dstW) {
		$dstH = $dstW;
	} elseif (!$dstH && !$dstW) {
		return false;
	}
	$minitemp = GetThumbInfo($srcFile,$dstW,$dstH,$cenTer);
	list($imagecreate,$imagecopyre) = GetImagecreate($minitemp['type']);
	if ((empty($sameFile) && $dstFile === $srcFile) || empty($minitemp) || !$imagecreate) return false;
	!empty($sameFile) && $dstFile = $srcFile;
	$imgwidth  = $minitemp['width'];
	$imgheight = $minitemp['height'];
	$srcX = $srcY = 0;
	if (!empty($cenTer)) {
		if ($imgwidth < $imgheight) {
			$srcY = round(($imgheight - $imgwidth)/2);
			$imgheight = $imgwidth;
		} else {
			$srcX = round(($imgwidth - $imgheight)/2);
			$imgwidth = $imgheight;
		}
	}
	$dstX = $dstY = 0;
	$thumb = $imagecreate($minitemp['dstW'],$minitemp['dstH']);
	$imagecopyre($thumb,$minitemp['source'],$dstX,$dstY,$srcX,$srcY,$minitemp['dstW'],$minitemp['dstH'],$imgwidth,$imgheight);
	MakeImage($minitemp['type'],$thumb,$dstFile);
	imagedestroy($thumb);
	return array($minitemp['dstW'],$minitemp['dstH']);
}
function GetThumbInfo($srcFile,$dstW,$dstH,$cenTer = null){
	$imgdata = array();
	$imgdata = GetImgInfo($srcFile);
	if (empty($imgdata) || ($imgdata['width']<=$dstW && $imgdata['height']<=$dstH)) return false;
	if (($imgdata['width']/$dstW) < ($imgdata['height']/$dstH)) {
		if (!empty($cenTer)) {
			$imgdata['dstW'] = $imgdata['dstH'] = $dstH;
		} else {
			$imgdata['dstW'] = round($dstH/$imgdata['height']*$imgdata['width']);
			$imgdata['dstH'] = $dstH;
		}
	} else {
		if (!empty($cenTer)) {
			$imgdata['dstW'] = $imgdata['dstH'] = $dstW;
		} else {
			$imgdata['dstW'] = $dstW;
			$imgdata['dstH'] = round($dstW/$imgdata['width']*$imgdata['height']);
		}
	}
	return $imgdata;
}
function GetImgInfo($srcFile){
	$imgdata = (array)GetImgSize($srcFile);
	if ($imgdata['type']==1) {
		$imgdata['type'] = 'gif';
	} elseif ($imgdata['type']==2) {
		$imgdata['type'] = 'jpeg';
	} elseif ($imgdata['type']==3) {
		$imgdata['type'] = 'png';
	} elseif ($imgdata['type']==6) {
		$imgdata['type'] = 'bmp';
	} else {
		return false;
	}
	if (empty($imgdata) || !function_exists('imagecreatefrom'.$imgdata['type'])) {
		return false;
	}
	$imagecreatefromtype = 'imagecreatefrom'.$imgdata['type'];
	$imgdata['source']	 = $imagecreatefromtype($srcFile);
	!$imgdata['width'] && $imgdata['width'] = imagesx($imgdata['source']);
	!$imgdata['height'] && $imgdata['height'] = imagesy($imgdata['source']);
	return $imgdata;
}
function MakeImage($type,$image,$filename,$quality='75'){
	$makeimage = 'image'.$type;
	if (!function_exists($makeimage)) {
		return false;
	}
	if ($type == 'jpeg') {
		$makeimage($image,$filename,$quality);
	} else {
		$makeimage($image,$filename);
	}
}
function GetImgSize($srcFile,$srcExt = null){
	empty($srcExt) && $srcExt = strtolower(substr(strrchr($srcFile,'.'),1));
	$srcdata = array();
	if (function_exists('read_exif_data') && in_array($srcExt,array('jpg','jpeg','jpe','jfif'))) {
		$datatemp = @read_exif_data($srcFile);
		$srcdata['width'] = $datatemp['COMPUTED']['Width'];
		$srcdata['height'] = $datatemp['COMPUTED']['Height'];
		$srcdata['type'] = 2;
		unset($datatemp);
	}
	!$srcdata['width'] && list($srcdata['width'],$srcdata['height'],$srcdata['type']) = @getimagesize($srcFile);
	if (empty($srcdata) || !$srcdata['type']) {
		return false;
	}
	return $srcdata;
}
function GetImagecreate($imagetype){
	if ($imagetype!='gif' && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled')) {
		return array('imagecreatetruecolor','imagecopyresampled');
	} elseif (function_exists('imagecreate') && function_exists('imagecopyresized')) {
		return array('imagecreate','imagecopyresized');
	} else {
		return array();
	}
}

function modeImageThumb($srcFile,$dstFile,$dstX,$dstY) {
	$imgdata = array();
	list($imgdata['width'],$imgdata['height'],$imgdata['type']) = getimagesize($srcFile);
	switch ($imgdata['type']) {
		case 1:
			$imgdata['type'] = 'gif';break;
		case 2:
			$imgdata['type'] = 'jpeg';break;
		case 3:
			$imgdata['type'] = 'png';break;
		default:
			return false;
	}
	if (!empty($imgdata) && function_exists('imagecreatefrom'.$imgdata['type'])) {
		$imagecreatefromtype = 'imagecreatefrom'.$imgdata['type'];
	} else {
		return false;
	}
	$imgdata['source'] = $imagecreatefromtype($srcFile);
	!$imgdata['width'] && $imgdata['width'] = imagesx($imgdata['source']);
	!$imgdata['height'] && $imgdata['height'] = imagesy($imgdata['source']);

	list($imagecreate,$imagecopyre) = GetImageCreate($imgdata['type']);

	$thumb = $imagecreate($dstX,$dstY);
	$color = @ImageColorAllocate($thumb,255,255,255);
	@imagefilledrectangle($thumb,0,0,$dstX,$dstY,$color);
	
	$pX = $pY = $pW = $pH = 0;
	
	if ($imgdata['width'] < $dstX && $imgdata['height'] < $dstY) {
		$pW = $imgdata['width'];
		$pH = $imgdata['height'];
	} else {
		if (($imgdata['width']/$imgdata['height']) > ($dstX/$dstY)) {
			$pW = $dstX;
			$pH = round($dstX*$imgdata['height']/$imgdata['width']);
		} else {
			$pH = $dstY;
			$pW = round($dstY*$imgdata['width']/$imgdata['height']);
		}
	}
	$pX = round(($dstX - $pW)/2);
	$pY = round(($dstY - $pH)/2);

	$imagecopyre($thumb,$imgdata['source'],$pX,$pY,0,0,$pW,$pH,$imgdata['width'],$imgdata['height']);
	MakeImage($imgdata['type'],$thumb,$dstFile);
	imagedestroy($thumb);
	return 1;
}
?>