Location: PHPKode > scripts > fen2Diagram > fen2diagram/fen2Diagram.class.php
<?php
/**
 * @author Can Ince <hide@address.com>
 * @copyright copyright @ 2007 by Can Ince
 * @license GNU/LGPL
 * @version 0.0.1
 * @package fen2Diagram
 */

class fen2Diagram {
var
$b = 'chessset/black_bishop.gif',
$r = 'chessset/black_rook.gif',
$n = 'chessset/black_knight.gif',
$p = 'chessset/black_pawn.gif',
$q = 'chessset/black_queen.gif',
$k = 'chessset/black_king.gif',
$B = 'chessset/white_bishop.gif',
$R = 'chessset/white_rook.gif',
$N = 'chessset/white_knight.gif',
$P = 'chessset/white_pawn.gif',
$Q = 'chessset/white_queen.gif',
$K = 'chessset/white_king.gif',
$fen,
$outputImageType="jpeg"; // default is jpeg. it can also be gif or png.
	
	/**
	Constructor function
	*/
	function fen2Diagram() {
	//dummy constructor function
	}
	
	/**
	Get the image file name relative to the actual piece.
	@param string $name : FEN notation of the actual piece. For a black rook, it would be
	r, for a white Queen, it would be Q 
	*/
	function getPiece($name) {
	return $this->$name;
	}
	
	/**render the chess diagram out of the parsed FEN */
	function printDiagram() {
	$fenArray=explode(" ",$this->fen);
	$fenNew=$fenArray[0];
	$lines=explode("/",$fenNew);
	$src_file = 'emptyBoard.jpg';
	list($src_w, $src_h, $src_t, $src_a) = getimagesize($src_file);
	// destination image dimensions:
	$dst_w = 360;
	$dst_h = 360;

	$srcImage = imageCreateFromJpeg($src_file) or die ('failed imageCreateFromJpg');
	$dstImage = imageCreateTrueColor($dst_w, $dst_h) or die ('failed imageCreateTrueColor');

	imageCopyResampled($dstImage, $srcImage, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h) or die ('failed imageCopyResampled');
	foreach($lines as $key=>$newLine) {
		while($z<=8) {
		$pos=$newLine{$z};
			if(is_numeric($pos)) {
			//we've got some empty squares
			$ptr_x=$ptr_x+(45*$pos);
			} else {
			$ptr_file =$this->getPiece($pos); 
			list($ptr_w, $ptr_h, $ptr_t, $ptr_a) = getimagesize($ptr_file);
				if(is_file($ptr_file)) {
					$ptrImage = imageCreateFromgif($ptr_file) or die ('failed imageCreatePiece');
					imageCopyMerge($dstImage, $ptrImage, $ptr_x, $ptr_y, 0, 0, $ptr_w, $ptr_h, 100) or die ('failed imageCopyMerge');
					$ptr_x=$ptr_x+45;
					imageDestroy($ptrImage) or die ('failed imageDestroy(3)');
					} 
				}
				$z++;
			} //end while
		$ptr_y=$ptr_y+45;$ptr_x=0;$z=0;
		} //end foreach
	switch($this->outputImageType) {

		case "jpeg":
		header("Content-type: image/jpeg");
		imageJpeg($dstImage,'',100) or die ('failed imageJpeg');
		break;
		case "png":
		header("Content-type: image/png");
		imagePng($dstImage,'',100) or die ('failed imagePng');
		break;
		case "gif":
		header("Content-type: image/gif");
		imageGif($dstImage,'',100) or die ('failed imageGif');
		break;

		default:
		header("Content-type: image/jpeg");
		imageJpeg($dstImage,'',100) or die ('failed imageJpeg');
		break;
		}
	imageDestroy($srcImage) or die ('failed imageDestroy(1)');
	imageDestroy($dstImage) or die ('failed imageDestroy(2)');
	} //end function printDiagram

}//end class fen2png
?>
Return current item: fen2Diagram