使用PHP GD生成的图像 - 如何与浏览器中心对齐?

Hope you all are doing great.

I have an image created and edited with text using PHP GD, I'm sending it to the browser with the following code but it is displayed aligned totally to the left of the browser, I tried adding css behavior directly to the img tag but nothing happened so I guess this will never work because it is a php header displaying the image, is there any way to align the image to the center of the browser within the same code lets say the way one align a div using margin 0 auto? As you can see I'm still a newbie with GD so here's my code, I really appreciate it pals:

<?php
$nombre=$_POST['nombre'];
  //Set the Content Type
header('Content-type: image/jpeg'); 


  // Create Image From Existing File
  $jpg_image = imagecreatefromjpeg('fabian.jpg');
      // get image dimensions
list($img_width, $img_height,,) = getimagesize("fabian.jpg");

  // Allocate A Color For The Text
  $white = imagecolorallocate($jpg_image, 255, 255, 255);

  // Set Path to Font File
  $font_path = 'fabian.TTF';

  // Set Text to Be Printed On Image
  $text =strtoupper($nombre);

  // Print Text On Image
  //imagettftext($jpg_image, 75, 0, 50, 400, $white, $font_path, $text);

 // find font-size for $txt_width = 80% of $img_width...
$font_size = 1; 
$txt_max_width = intval(0.8 * $img_width);    

do {

$font_size++;
$p = imagettfbbox($font_size,0,$font_path,$text);
$txt_width=$p[2]-$p[0];
// $txt_height=$p[1]-$p[7]; // just in case you need it

} while ($txt_width <= $txt_max_width);

// now center text...
$y = $img_height * 0.9 ;// baseline of text at 90% of $img_height
$x = ($img_width - $txt_width) / 2;

imagettftext($jpg_image, $font_size, 0, $x, $y, $white, $font_path, $text);



  // Send Image to Browser
  imagepng($jpg_image);


 // Clear Memory
  imagedestroy($jpg_image);

?>

Thanks a lot for your advice! Have a great day.

It looks like you're simply outputting the image file in the code you provided. There is no way to specify in this code how any browser should display the image.

If there is a separate HTML code somewhere that displays this image, you should specify anything relating appearance and positioning there. As such it becomes a HTML/CSS question about how to center an image and doesn't have anything to do with PHP or GD.

You will need to add display: block; to your CSS rules to get it to centralise.

If however you're just requesting the image in the browser, then there's not really a way around it, as you would need to know the browser window dimensions, which aren't available server side.