【问题标题】:PHP GD - Align text center-horizontally and decrease font size to keep it inside the imagePHP GD - 水平居中对齐文本并减小字体大小以将其保留在图像内
【发布时间】:2013-04-05 15:39:01
【问题描述】:

希望你做得很好。

我还是 php 的新手,所以在阅读了一些文章并检查了这里的一些帖子后,我能够使用 PHP GD 使用 imagecreatefrompng() 函数在图像上放置一些文本,用户会来到一个表单和他们将能够输入他们的名字并且名字将被写在图像上,不幸的是我无法水平对齐文本中心,我尝试了所有可能的方法(我的方法显然是错误的),但我失败了我所有的尝试,请你们帮我把弦中心水平对齐吗?另外,由于我使用的是另一种大字体,因此如果输入的名称有点长,我需要减小尺寸,这样它就不会超过图像限制并保持在中心。我正在从表单中获取文本的值,您可以在我的代码开头查看:

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

  ?>

您的帮助将不胜感激,稍后我将通过单击提交按钮来保存图像,因为我不希望用户通过右键单击图像来保存图像。

谢谢朋友!

【问题讨论】:

    标签: php gd centering alignment


    【解决方案1】:

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

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

    【讨论】:

    • 伟大的米奇!!!,它工作得很好,非常感谢!!!你无法想象我尝试了多少次,你是一个天才的朋友,我真的很感激。将分析和记住每一行代码并牢记在心以备将来参考。谢谢!
    • 这就像单行文本的魅力。这不适用于多行文本。谢谢
    • @JayKandari 感谢您的赞美,作为说明,OP 的目标是将一个人的名字放在他们的照片上,这是一个单一的衬里......至少对于大多数名字来说 :-)
    【解决方案2】:

    您可以使用stil/gd-text 类来对齐文本。免责声明:我是作者。

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

    但这不是一个完整的答案,因为它不能自动减小字体大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-03
      • 2015-01-16
      • 2017-09-09
      • 2013-03-19
      • 2013-07-24
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多