祝大家一切顺利。
我有一个图像创建和编辑文本使用PHP GD,我与下面的代码发送给浏览器,但这完全显示对齐左边的浏览器,我试着添加css行为直接img标签但没有工作所以我想这永远不会发生,因为它是一个PHP页眉显示图像,有什么方法可以使图像在相同的代码浏览器的中心假设的方式使一个div使用保证金0汽车?正如你所看到的,我仍然是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规则中以使其集中。
然而,如果你只是在浏览器中请求图像,那么就没有真正的方法绕过它,因为你需要知道浏览器窗口的尺寸,这是不可用的服务器端。