doulei8475 2013-04-13 17:32
浏览 44
已采纳

使用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.

  • 写回答

2条回答 默认 最新

  • doumicheng6732 2013-04-13 17:37
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示