<?
@ini_set('memory_limit', '-1');
@set_time_limit(0);
!defined("MONKEYMAO") && die("Accessed Defined!");

function imageresize($srcfile,$tow,$toh,$tofile="") {
   if($tofile==""){ $tofile = $srcfile; }
   $info = "";
   $srcinfo = getimagesize($srcfile);
	if(!$im = createfromimage($srcfile,$srcinfo)) {
		return false;
	}
	$srcw=imagesx($im);
	$srch=imagesy($im);
	$towh=$tow/$toh;
	$srcwh=$srcw/$srch;
	if($towh<=$srcwh){
		$ftow=$tow;
		$ftoh=$ftow*($srch/$srcw);
	}else{
		$ftoh=$toh;
		$ftow=$ftoh*($srcw/$srch);
	}    
	if($srcw>$tow || $srch>$toh){
		if(function_exists("imagecreatetruecolor")){
			@$ni = imagecreatetruecolor($ftow,$ftoh);
			if($ni){
				@imagecopyresampled($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
			}else{
				$ni=imagecreate($ftow,$ftoh);
				@imagecopyresized($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
			}
		}else{
			$ni=@imagecreate($ftow,$ftoh);
			@imagecopyresized($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
		}

	
		if(!saveimage($tofile,$ni,$srcinfo)) {
			return false;
		}
		@imagedestroy($ni);
	}else{
		@copy($srcfile,$tofile);
	}
	return true;
}



function createfromimage($file,$info = ''){
	if(!$info && file_exists($file)) {
		$info = getimagesize($file);	
	}
	switch($info['2']){
		case 1 : 
			return function_exists("imagecreatefromgif") ? imagecreatefromgif($file) : false;
		break;
		case 2 :
			return function_exists("imagecreatefromjpeg") ? imagecreatefromjpeg($file) : false;
		break;
		case 3 :
			return function_exists("imagecreatefrompng") ? imagecreatefrompng($file) : false;
		break;
		case 6:
			return function_exists("imagewbmp") ? imagewbmp($file) : false;
		break;
		default:
			return false;
		break;
	}
}


function saveimage($file,$source, $info = ''){
	if(!$info && file_exists($file)) {
		$info = getimagesize($file);	
	}
	
	switch($info[2]){
		case 1 :
			function_exists("imagegif") && imagegif($source,$file,100);
		break;
		case 2 :
			function_exists("imagejpeg") && imagejpeg($source,$file,100);
		break;
		case 3 :
			function_exists("imagepng") && imagepng($source,$file,100);
		break;
		case 6 :
			function_exists("imagewbmp") && imagewbmp($source,$file,100);
		break;
		default :
			return false;
		break;
	}

	return true;
}

function imgwatermark($srcfile,$w_pos=0,$w_img="",$w_pct=100){
	//read water image
    if(empty($srcfile) || !file_exists($srcfile)) {
		return false;
    }
    $srcinfo = getimagesize($srcfile);
    $srcfile_w    = $srcinfo[0];
    $srcfile_h    = $srcinfo[1];	 
	 if(!$srcfile_img = createfromimage($srcfile,$srcinfo)) {
		 return false;
	 }	   
    //read watermark image
    if(!file_exists($w_img)){
		 return false;
	}	
	$water_info = getimagesize($w_img);
	$width      = $water_info[0];
	$height     = $water_info[1];	
	if(!$water_img = createfromimage($w_img,$water_info)) {
		return false;
	}
     //watermark Location
$wx = $wy = 0;
	switch($w_pos){
		case 0:  //auto
			$wx = rand(0,($srcfile_w - $width));
			$wy = rand(0,($srcfile_h - $height));
		break;
		case 1:  //top left
			$wx = 5;
		break;
		case 2://left center
		  $wx = 5;
		  $wy = ($srcfile_h - $height) / 2;
		break;
		case 3: //left bottom
			$wx = 5;
			$wy = $srcfile_h - $height - 5;
		break;
		case 4://top center
			$wx = ($srcfile_w - $width) / 2;
		break;
		case 5: //middle
			$wx = ($srcfile_w - $width) / 2;
			$wy = ($srcfile_h - $height) / 2;
		break;
		case 6://down center
			$wx = ($srcfile_w - $width) / 2;
			$wy = $srcfile_h - $height - 5;
		break;
		case 7://right top
			$wx = $srcfile_w - $width - 5;
		break;
		case 8://right center
			$wx = $srcfile_w - $width - 5;
			$wy = ($srcfile_h - $height) / 2;
		break;
		case 9://right bottom
			$wx = $srcfile_w - $width - 5;
			$wy = $srcfile_h - $height - 5;
		break;
		default:
			$wx = ($srcfile_w - $width) / 2;
			$wy = ($srcfile_h - $height) / 2;
		break;
	}
    //write water mark
    @imagealphablending($srcfile_img, true);
	@imagecopymerge($srcfile_img, $water_img, $wx, $wy, 0, 0, $width,$height,$w_pct);

    //save image
	if(!saveimage($srcfile,$srcfile_img,$srcinfo)) {
		return false;
	}
    if(isset($water_info)){
		unset($water_info);
	}
    if(isset($water_img)){
		imagedestroy($water_img);
	}
    unset($srcinfo);
	imagedestroy($srcfile_img);
}


?>
