<?php

	
/**************/
/*recuperation*/
/*des emblemes*/
/**************/
	
	//globfiles  : return an array with the files in the folder $dir, 
	//except the ones beginning with '_' or finishing by '.small.png'
	function globfiles($dir)
	{
		$files=Array();
		//if(!($res=opendir($dir))) exit("$dir doesn't exist!");
		$handle=opendir($dir);
		
		while (false !== ($file = readdir($handle)))
			if(!is_dir("$dir/$file") && (strpos($file,'.png') || strpos($file,'.PNG') || strpos($file,'.gif') || strpos($file,'.GIF')))
				array_push($files,"$dir/$file");
	
		closedir($handle);
		sort($files);
		return $files;
	}
	

	function emblemize ($dir)
	{
		$emblemes = globfiles($dir);//'../emblemes_preexistants'
		$string = '<table><tr><td>';
		
		$i=0;
		foreach ($emblemes as $embleme) {
			$i++;
			$string .= '<input type="radio" name="embleme" value="'.$embleme.'" />';
			$string .= '<img src="'.$embleme.'" alt="'.$embleme.'" />';
			if ($i>9) {$string .= '</td></tr>'."\n".'<tr><td>';$i=0;}
			else {$string .= '</td><td>';}
		}
		$string .= '</td></tr></table>';

		return $string;
	}	

	function uploadOrGetEmblem ($fileUploaded)
	{
		$DEFAULT_EMBLEM = '../emblemes_preexistants/2xup.gif';

		$imageEmblemFile = '';
		$stringResult = '';
		
		if ($_POST['embleme']=='') {
		
			if(isset($fileUploaded)) {
				$dest_dossier = '../emblemes/';
				$dest_fichier = $guilde.'.png';//basename($_FILES['monfichier']['name']);
				
				$imageEmblemFile = $dest_dossier.$dest_fichier;
				
				if (!move_uploaded_file($_FILES['monfichier']['tmp_name'], $imageEmblemFile)) {
					$imageEmblemFile = $DEFAULT_EMBLEM;
					$stringResult = 'La recuperation de l\'image de votre embl&egrave;me n\'a pas march&eacute;. Un embl&egrave;me par d&eacute;faut a &eacute;t&eacute; affect&eacute;.';
				}
				else {
					//modifie la taille de l'image si c'est une image uploadee
					//redimensionne l'image pour creer le petit symbole de guilde
					$im = @imagecreatefrompng($imageEmblemFile);
					list($width, $height, $type, $attr) = getimagesize($imageEmblemFile);
					$im2 = imagecreate( 16, 16);
					imagealphablending($im2, false);
					imagecopyresized ( $im2, $im, 0, 0, 0, 0, 16, 16, $width, $height );
					imagesavealpha($im2, true);
					imagepng ($im2,$imageEmblemFile);
					$stringResult = 'Vos modifications ont bien &eacute;t&eacute; prises en compte.';
				}
			}
			else {
				$imageEmblemFile = $DEFAULT_EMBLEM;
				$stringResult = '( '.$_FILES.' est vide)';
				$stringResult .= 'La recuperation de l\'image de votre embl&egrave;me n\'a pas march&eacute;. Un embl&egrave;me par d&eacute;faut a &eacute;t&eacute; affect&eacute.';
			}
		}
		else {
			$imageEmblemFile = $_POST['embleme'];
			$stringResult = 'Vos modifications ont bien &eacute;t&eacute; prises en compte.';
		}
		
		return array($imageEmblemFile,$stringResult);
	}

?>
