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
https://forums.phpfreaks.com/topic/102579-php-center-an-imagestring/
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);

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

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.

Always put GD image code in a file of its own and place on the page using img tag

 

<img src="tombstone.php?t1=$text1&t2=$text2" />

 

I'm trying to make the generator just like this http://jjchandler.com/tombstone/

 

I'm not trying to make the page work on php tags in the url, but on user input on the form

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.