Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP GD-水平对齐文本中心并减小字体大小,使其保持在图像内部_Php_Gd_Centering_Alignment - Fatal编程技术网

PHP GD-水平对齐文本中心并减小字体大小,使其保持在图像内部

PHP GD-水平对齐文本中心并减小字体大小,使其保持在图像内部,php,gd,centering,alignment,Php,Gd,Centering,Alignment,希望你做得很好 我还是一个php新手,所以在阅读了一些文章并检查了一些帖子后,我能够使用php GD使用imagecreatefrompng()函数在图像上放置一些文本,用户将进入一个表单,他们可以输入自己的姓名,姓名将写在图像上,不幸的是,我一直无法水平对齐文本中心,我用imagettfbbox尝试了所有可能的方法(我的方法显然是错误的),但我所有的尝试都失败了,你们能帮我水平对齐字符串中心吗?另外,由于我使用的是一种替代的大字体,我需要在输入的名称有点长的情况下减小大小,这样它就不会超过图像

希望你做得很好

我还是一个php新手,所以在阅读了一些文章并检查了一些帖子后,我能够使用php GD使用imagecreatefrompng()函数在图像上放置一些文本,用户将进入一个表单,他们可以输入自己的姓名,姓名将写在图像上,不幸的是,我一直无法水平对齐文本中心,我用imagettfbbox尝试了所有可能的方法(我的方法显然是错误的),但我所有的尝试都失败了,你们能帮我水平对齐字符串中心吗?另外,由于我使用的是一种替代的大字体,我需要在输入的名称有点长的情况下减小大小,这样它就不会超过图像的限制,并保持在中间。我从表单中获取文本值,您可以在我的代码开头检查:

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

  // Create Image From Existing File
  $jpg_image = imagecreatefromjpeg('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 , I set it to uppercase
  $text =strtoupper($nombre);

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



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

  // Clear Memory
  imagedestroy($jpg_image);

  ?>

我们将非常感谢您的帮助,稍后我将打破我的头,试图通过单击提交按钮保存图像,因为我不希望用户通过右键单击来保存图像


谢谢朋友们

您需要图像的宽度和文本的宽度来关联这两者

// get image dimensions
list($img_width, $img_height,,) = getimagesize("fabian.jpg");

// 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 the 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);
//获取图像尺寸
列表($img_宽度,$img_高度,,)=getimagesize(“fabian.jpg”);
//查找$txt\u宽度的字体大小=$img\u宽度的80%。。。
$font_size=1;
$txt_max_width=intval(0.8*$img_width);
做{
$font_size++;
$p=imagettfbbox($font\u size,0,$font\u path,$text);
$txt_宽度=$p[2]-$p[0];
//$txt_height=$p[1]-$p[7];//以防万一
}虽然($txt_width您可以使用类来对齐文本。免责声明:我是作者

<?php
use GDText\Box;
use GDText\Color;

$jpg_image = imagecreatefromjpeg('fabian.jpg');

$textbox = new Box($jpg_image);
$textbox->setFontSize(75);
$textbox->setFontFace('fabian.TTF');
$textbox->setFontColor(new Color(255, 255, 255));
$textbox->setBox(
    50,  // distance from left edge
    50,  // distance from top edge
    200, // textbox width
    100  // textbox height
);

// text will be aligned inside textbox to center horizontally and to top vertically
$textbox->setTextAlign('center', 'top');
$textbox->draw(strtoupper($nombre));

Great michi!!!,非常感谢!!!你无法想象我尝试了多少次,你是一个天才朋友,我真的很感激你。我会分析并记住每一行代码,以备将来参考。谢谢!这就像一行文字的魅力一样。这对多行文字不起作用一系列的文本。Thanks@JayKandari谢谢你的夸奖,作为一个注释,OP的目标是将一个人的名字集中在他们的照片上,这是一行…至少大多数人的名字是:-)