<?php
function filename_safe($filename) {
  $temp = $filename;
  
  // Lower case
  $temp = strtolower($temp);
  
  // Replace spaces with a '_'
  $temp = str_replace(" ", "_", $temp);
  
  // Loop through string
  $result = '';
  for ($i=0; $i<strlen($temp); $i++) {
	if (preg_match('([0-9]|[a-z]|_)', $temp[$i])) {
	  $result = $result . $temp[$i];
	}
  }
  
  // Return filename
  return $result;
}

if(isset($_REQUEST['submit']))
{
  if($_REQUEST['submit'] == 'Create Image') 
  {
  
	$text = $_REQUEST['text'];
	
	$font = 'TrajanPro-Regular.otf';
	
	$size = 27.5; //font size in pt
	$angle = 0;
	
	$box = imageftbbox($size,$angle,$font,$text);
	
	$img_h = 40;
	$img_w_pad = 50;
	$img_w = ($img_w_pad * 2) + $box[2] - $box[6];
	
	$img = imagecreatetruecolor($img_w, $img_h);
	
	//$bg_color = imagecolorallocate($img, 255, 255, 255);
	$bg_color = imagecolorallocatealpha($img, 255, 255, 255, 127);	

	$font_color = imagecolorallocate($img, 42, 12, 90);
  
	//header('Content-type: image/png');
  
	header('Content-Description: File Transfer');
	header('Content-Type: application/octet-stream');
	header('Content-Disposition: attachment; filename=dept_header.png');
	header('Content-Transfer-Encoding: binary');
	header('Expires: 0');
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	header('Pragma: public');
	//header('Content-Length: ' . filesize($file));
  
	//imagefilledrectangle($img, 0, 0, $img_w, $img_h, $bg_color);
	
	imagecolortransparent($img,$bg_color);
	//imagecolorallocatealpha($img, 255, 255, 255, 127);

	imagealphablending( $img, true );
	imagesavealpha( $img, true );
	
	imagefill($img, 1, 1, $bg_color);
	//imagefilledrectangle($img, 0, 0, $img_w, $img_h, $bg_color);
	
	imagettftext($img, $size, $angle, $img_w_pad, $img_h, $font_color, $font, $text);
	
	imagepng($img);
	
	imagedestroy($img);
  }
} else {
?>	
<html>
<body>
<label for="text">Enter Text to Create Custom Header Image</label><br/>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="imageform">
<input id="text" type="text" name="text" maxlength="64" size="60" />
<input type="text" style="visibility:hidden; display:none;" />
<input type="submit" name="submit" value="Create Image" />
</form>
</body>
</html>
<?php	
}
?>