Jump to content

PHP Center an "Imagestring"


SaintIsaiah

Recommended Posts

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 greatly appreciate it. I started making php from scratch a month ago so if anyone wouldn't mind telling me how well it's put out and any suggestions, I'd appreciate that as well.

 

<?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

Link to comment
Share on other sites

<?php
$fw = imagefontwidth(5);     // width of a character
$l = strlen($text);          // number of characters
$tw = $l * $fw;              // text width
$iw = imagesx($im);          // image width

$xpos = ($iw - $tw)/2;
$ypos = 10;
imagestring ($im, 5, $xpos, $ypos, $text, $color);

Link to comment
Share on other sites

I was gonna edit this and also ask a question but I couldn't find an edit button. How do I make the template look like it's engraved on the image with php.

 

print text 3 times - text _ shadow and hilight

 

<?php 
$im = imagecreate(150,50);
$text = 'R.I.P';
$bgd = imagecolorallocate($im, 180,180,180);
$light = imagecolorallocate($im, 230,230,230);
$mid = imagecolorallocate($im, 160,160,160);
$dark = imagecolorallocate($im, 90,90,90);

$fw = imagefontwidth(5);     // width of a character
$l = strlen($text);          // number of characters
$tw = $l * $fw;              // text width
$iw = imagesx($im);          // image width

$xpos = ($iw - $tw)/2;
$ypos = 20;

imagestring ($im, 5, $xpos-1, $ypos-1, $text, $dark);      // dark shadow top left
imagestring ($im, 5, $xpos+1, $ypos+1, $text, $light);     // lighter shade bottom right
imagestring ($im, 5, $xpos, $ypos, $text, $mid);           // text in the middle

header("content-type: image/png");
imagepng($im);
imagedestroy($im);
?>

Link to comment
Share on other sites

Well that works but how would I go about implementing that into my already made script to where the text isn't based on an already specified variable, but the user's input on the form? I tried implementing the first reply with the code you provided into my script and on the result page it didn't display or save an image.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.