使用gd库在php上居中图像

hey guys i have been doing some code on php ..but its not giving me the output which i expected ..The code is

html code

<html>
    <body>
        <form action="images.php" method="post">
            Enter image Name :<input type="text" name="imagename">
            <br></br>
            <input type="submit" name="sumbit">
            <br></br>
        </form>
    </body>
</html>

In this html code user is allowded to type his name so that the image is generated according to the name..

the php code

<?php
    $name = $_POST['imagename'];
    if($_POST['imagename'] != ''){
        header("Content-Type: image/png");
        $name = $_POST['imagename'];
        $im = @imagecreate(800,600);
        $background_color = imagecolorallocate($im, 0xFF, 0xCC, 0xDD);
        $text_color = imagecolorallocate($im, 133, 14, 91);
        imagestring($im, 5, 300, 300,  "$name", $text_color);
        imagepng($im);
        imagedestroy($im);
    }
    else echo "no image created";
?>

This php code generates the image accrding to the name given..

The image is generated fine with this piece of codes but what i need is that the image created must appear on the center of the browser ...i have tried it using <img src="file.php> on the html file..but it didnt worked ..

It will be really helpful if you guys find me a solution..Thanks in advance

Here you'll find an example of how to center text in a handmade image. Citing that page, you should do:

 <?php
     $fw = imagefontwidth(5);     // width of a character
     $l = strlen("$name");        // number of characters
     $tw = $l * $fw;              // text width
     $iw = imagesx($im);          // image width, 800 for your case

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

     //...
 ?>

If you want to center your image in the browser, you need html and css.

I would recommend that instead of displaying the image directly using header("Content-Type: image/png");, you save the image to a file.

Then your script can output html and include the image in an image tag or as a background image. That image can be centered in the browser easily using css.