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 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决