繁体   English   中英

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

[英]Image generated with PHP GD -How to align to the center of browser?

希望你们都做得很好。

我使用PHP GD创建并使用文本编辑图像,我使用以下代码将其发送到浏览器,但它显示在浏览器左侧完全对齐,我尝试将css行为直接添加到img标记但没有任何内容发生了所以我想这将永远不会工作,因为它是一个显示图像的PHP标题,是否有任何方法将图像与同一代码中的浏览器中心对齐让我们说使用margin 0 auto对齐div的方式? 正如你所看到的,我仍然是GD的新手,所以这里是我的代码,我真的很感激好友:

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

?>

非常感谢您的建议! 祝你有美好的一天。

看起来您只是在您提供的代码中输出图像文件。 无法在此代码中指定任何浏览器应如何显示图像。

如果某个地方有单独的HTML代码显示此图像,则应指定与外观和定位相关的任何内容。 因此,它成为关于如何使图像居中的HTML / CSS问题,并且与PHP或GD没有任何关系。

你需要添加display: block; 到您的CSS规则,以使其集中。

但是,如果您只是在浏览器中请求图像,那么就没有办法解决它,因为您需要知道浏览器窗口尺寸,这是服务器端不可用的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM