I've been working on a script for the past month that allows a user to create a custom headstone with their own text. Everything works great except for one thing: I cant figure out how to center the text!!! I then tried to make a calculation to center the text and now the result wont show! Here is the code to my page. I've never worked with images before in php so it's very new and difficult to me. If someone could please look the following code over and tell me what's wrong as well as the changes needed to make it work, I would greatyly appreciate it.
<?php
if (!$_POST) {
//show form
echo "
<html>
<head>
<title>Tombstone Creation Form</title>
</head>
<body>
<h1>Create Tombstone</h1>
<form method=\"POST\" action=\"".$_SERVER["PHP_SELF"]."\">
<p><strong>Full Name:</strong><br/>
<input type=\"text\" name=\"name\" size=35>
<br/>
<br/>
<strong>Date of Birth - Date of Death (e.g. 01/01/1950 - "; echo date("m/d/Y"); echo ")</strong><br/>
<input type=\"text\" name=\"dob\" size=35>
<br/>
<br/>
<strong>Last Name, First Initial (e.g. John Smith = smithj)</strong><br/>
<input type=\"text\" name=\"lnfi\" size=35>
<br/>
<br/>
<strong>Message Line 1 (e.g. Beloved Husband and Devoted Father)</strong><br/>
<input type=\"text\" name=\"message1\" size=35 maxlength=24>
<br/>
<br/>
<strong>Message Line 2 (e.g. Beloved Husband and Devoted Father)</strong><br/>
<input type=\"text\" name=\"message2\" size=35 maxlength=24>
<br/>
<br/>
<p><input type=\"submit\" name=\"submit\" value=\"Create Tombstone\">
</form>
</body>
</html>";
} else {
// Start With Image
$myImage = ImageCreateFromPNG ("tombstone.png");
// Black Text Color
$text = ImageColorAllocate ($myImage, 0, 0, 0);
// Get Width Of Image
$width = imagesx($myImage);
// Static Coordinates For Each Line
$y1 = 140;
$y2 = 180;
$y3 = 220;
$y4 = 260;
// System Font ID
$font = 4;
// Imagefontwidth Variable
$fontwidth = imagefontwidth($font);
// Center Each Line With A Calculation
$x1 = ($width - ($fontwidth*strlen($_POST["name"])))/2;
$x2 = ($width - ($fontwidth*strlen($_POST["dob"])))/2;
$x3 = ($width - ($fontwidth*strlen($_POST["message1"])))/2;
$x4 = ($width - ($fontwidth*strlen($_POST["message2"])))/2;
// Write The Text!
ImageString($myImage, $font, $x1, $y1, $_POST["name"], $text);
ImageString($myImage, $font, $x2, $y2, $_POST["dob"], $text);
ImageString($myImage, $font, $x3, $y3, $_POST["message1"], $text);
ImageString($myImage, $font, $x4, $y4, $_POST["message2"], $text);
// Get Ready For Preview
header ("Content-type: image/png");
// Save The Image
ImagePNG($myImage, $_POST["lnfi"].'.png');
// Display The Image
ImagePNG($myImage);
//Clean Up!
ImageDestroy($myImage);
}
?>
Thanks,
Saint Isaiah