<?
	### Database functions version 1.0 7th march 2006
	$version_functions_images=1.0;
?>
<?
	function ImgMerge($fn1, $fn2) {
		global $imsize;
		
		$im1 = @imagecreatefromgif (Inc($fn1.".gif"));
		$im2 = @imagecreatefromgif (Inc($fn2.".gif"));
		imagecopy($im2,$im1,0,0,0,0,$imsize,$imsize);
		imagegif($im2, Inc("imgswap.gif"));
	}
	
	function ImgCreate($v, $verbose=false, $bg=1) {
		global $let2, $imgname, $imsize;
		global $filelistnb, $filelist;
		global $dirtiles, $DOCUMENT_ROOT;
		
		if ($verbose) echo "ImgCreate<br>";
		if ($verbose) echo $v."<br>";
		$fn="img_".$v.".gif";
		if ($verbose) echo $fn."<br>";
		$found=false;
		for ($i=1; $i<=$filelistnb; $i++) {
			if ($filelist[$i]==$fn) {
				$found=true;
			}
		}
		
		if (!$found) {
			if ($bg==1) {
				$im2=@imagecreatefromgif (Inc("empty.gif"));
			}
			if ($bg==2) {
				$im2=@imagecreatefromgif (Inc("empty2.gif"));
			}

			for ($j=1; $j<=strlen($v); $j++) {			
				$vj=strpos($let2, $v[$j-1]);
				if ($verbose) echo $vj."<br>";
				$vj=decbin($vj)."";
				if ($verbose) echo $vj."<br>";
				do {
					if (strlen($vj)<6) { $vj="0".$vj; }
				} while (strlen($vj)<6);
				if ($verbose) echo $vj."<br>";
				for ($i=1; $i<=6; $i++) {
					if ($vj[$i-1]=="1") {
						$im1 = @imagecreatefromgif (Inc($imgname[$j][$i].".gif"));
						imagecopy($im2,$im1,0,0,0,0,$imsize,$imsize);
					}
				}
			}
			imagegif($im2, $DOCUMENT_ROOT.$dirtiles.$fn);
		}
		return $fn;
	}
	
	function ImgResize($filesource,$filedest,$size=500,$force=false) {
		list($width, $height, $type, $attr) = getimagesize($filesource);
		if ($width>$height) {
			$r=max($width, $height)/$size;
			$h=min($width, $height)/$r;
			$w=$size;
		} else {
			$r=max($width, $height)/$size;
			$w=min($width, $height)/$r;
			$h=$size;
		}
		if ((!file_exists($filedest)) || ($force)) {
			$im = @imagecreatefromjpeg ($filesource);
			$thumb = @imagecreatetruecolor($w, $h);
			imagecopyresized($thumb, $im, 0, 0, 0, 0, $w, $h, $width, $height);
			imagejpeg($thumb, $filedest);
		}
	}
	
	function ImgCrop($filesource,$filedest,$x1,$y1,$x2,$y2) {
		$im = @imagecreatefromjpeg ($filesource);
		$thumb = @imagecreatetruecolor($x2-$x1, $y2-$y1);
		imagecopyresized($thumb, $im, 0, 0, $x1, $y1, $x2-$x1, $y2-$y1, $x2-$x1, $y2-$y1);
		imagejpeg($thumb, $filedest);
	}
	
	function ImgCopy($filesource,$filedest) {
		list($width, $height, $type, $attr) = getimagesize($filesource);
		$im = @imagecreatefromjpeg ($filesource);
		$thumb = @imagecreatetruecolor($width, $height);
		imagecopyresized($thumb, $im, 0, 0, 0, 0, $width, $height, $width, $height);
		imagejpeg($thumb, $filedest);
	}
	
	function ImgRotate($filename,$angle) {
		$im = @imagecreatefromjpeg ($filename);
		$im=imagerotate($im, $angle, 0);
		imagejpeg($im, $filename);
	}
?>