La logique de petit bout est très simple, je participe à un jeu en-ligne qui se nomme The Mafia Underground. Un groupe de modérateurs organise fréquemment des quiz, le problème était qu’ils écrivaient les questions dans le forum et les gens fesaient un copy/paste dans google et obtenait une réponse presque automatiquement.

J’ai donc décidé de rendre ça plus intéressant en offrant la chance aux modérateurs de générer des images à partir de leurs questions … voici donc le code sans plus tard (vous n’êtes pas ici pour mes yeux non?)

<?php
 
function sendimagetext($text,$uniqueid) {
 
  // Set font size
  $font_size = 4;
 
  if(strlen($text)>60)
  {
  	$arText = str_split($text,60);
  	$text = "";
  	foreach($arText as $key => $value)
  	{
  		$text .= $value."\n";
  	}
  }
 
  $ts=explode("\n",$text);
  $width=0;
  foreach ($ts as $k=>$string) { //compute width
    $width=max($width,strlen($string));
  }
 
  // Create image width dependant on width of the string
  $width  = imagefontwidth($font_size)*$width;
  // Set height to that of the font
  $height = imagefontheight($font_size)*count($ts);
  $el=imagefontheight($font_size);
  $em=imagefontwidth($font_size);
  // Create the image pallette
  $img = imagecreatetruecolor($width,$height);
  // Dark red background
  $bg = imagecolorallocate($img, 0,0,0);
  imagefilledrectangle($img, 0, 0,$width ,$height , $bg);
  // White font color
  $color = imagecolorallocate($img, 255, 255, 255);
 
  foreach ($ts as $k=>$string) {
 
    // Length of the string
    $len = strlen($string);
    // Y-coordinate of character, X changes, Y is static
    $ypos = 0;
    // Loop through the string
    for($i=0;$i<$len;$i++){
      // Position of the character horizontally
      $xpos = $i * $em;
      $ypos = $k * $el;
      // Draw character
      imagechar($img, $font_size, $xpos, $ypos, $string, $color);
      // Remove character from string
      $string = substr($string, 1);     
    }
  }
 
  // Return the image
  imagepng($img,"../pix/trivia_pics/".$uniqueid.".png");
  // Remove image
  //imagedestroy($img);
}
 
 
function GenerateRandomFilename($string)
{
	$ipbits = explode(".", $_SERVER["REMOTE_ADDR"]); 
	$ar = explode(".",$string);
	list($usec,$sec) = explode(' ', microtime());
	$usec = (integer) ($usec * 65536);
	$sec = (integer) ($sec & 0xFFFF);
	$uid = sprintf("%08x-%04x-%04x",($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec, $usec);
	return $uid;
}
 
?>
 
<html>
<head>
</head>
<body>
<?php
	if(!isset($_GET['s']))
	{
		$html = "<form name='questions' method='POST' action='index.php?s=done'><table border=0>";
		for ($j = 1; $j < 21; $j++)
		{
			$html .= "<tr><td><small>Question #".$j.": </small></td><td><input type='text' name='q".$j."' id='q".$j."'></td></tr>";
		}
		$html .= "<tr><td colspan=2 align=center><input type='submit' value='Generate'></td></tr>";
		$html .= "</table></form>";
	} else {
		$html = "";
		$i = 1;
		foreach ($_POST as $key => $value)
		{
			if(!empty($value))
			{
				$uniquestring = GenerateRandomFilename($value);
				//txttopng($value,$uniquestring);
				sendimagetext($value,$uniquestring);
				$html .= "Image for question #".$i."<br/>";
				$html .= "BBCODE: [img]http://pix.itsg33k.ca/trivia_pics/".$uniquestring.".png[/img]<br/>";
				$html .= "<a href='../pix/trivia_pics/".$uniquestring.".png'><img border=0 src='../pix/trivia_pics/".$uniquestring.".png'></a><br/><br/>";
			}
			$i++;
		}
	}
 
	echo $html;
?>
 
</body>
</html>

Vous remarquerez aux lignes 100 et 101 du script que j’ai dû rediriger les images vers un autre dossier, tout simplement parce que mon script est sécurisé dans un dossier spécial pour donner accès seulement aux modérateurs du site, mais je veux que les images soient disponibles à tous, donc j’écris les images dans un répertoire différent. Dans votre cas, il suffit d’utiliser votre génie pour modifier légèrement le script pour placer les images où vous le désirez.

On s’entend, c’est un script de base, rien de bien complexe, mais si vous cherchez comment faire, vous aurez le plus gros du travail de fait grâce à moi!

Si vous avez des questions ou commentaires, utilisez la section commentaires de ce post, il me fera plaisir de vous aider!