【问题标题】:Center text on image using PHP GD使用 PHP GD 在图像上居中文本
【发布时间】:2014-05-20 04:47:07
【问题描述】:

所以我正在创建一个横幅生成器。

我将在中间添加文本,但希望它正好在中心。 我知道imagettftext 可用于在横幅上写字,但这不会使其居中。

一个可能的解决方案是找到文本的宽度,然后使用从横幅宽度的一半中取出的一半,但我不知道如何做到这一点。

我正在使用 PHP-GD,不想使用我必须安装的其他任何东西。

imagettftext($img, 14, 0, (468 - ((strlen($_GET['description']) * imagefontwidth(imageloadfont('minecraft.ttf'))) / 1)), 85, imagecolorallocate($img, 0, 0, 0), 'minecraft.ttf', $_GET['description']);

上面的代码正在生成上面的结果。小字符串没问题,但肯定有问题,因为一旦它们变长,它就会失败。

【问题讨论】:

    标签: php gd php-gd


    【解决方案1】:

    您可以通过从imageftbbox 获取外边界的宽度来使文本居中,然后将其除以 2 以获得使图像中的文本居中的偏移量。

    // Get image dimensions
      $width = imagesx($image);
      $height = imagesy($image);
    // Get center coordinates of image
      $centerX = $width / 2;
      $centerY = $height / 2;
    // Get size of text
      list($left, $bottom, $right, , , $top) = imageftbbox($font_size, $angle, $font, $text);
    // Determine offset of text
      $left_offset = ($right - $left) / 2;
      $top_offset = ($bottom - $top) / 2;
    // Generate coordinates
      $x = $centerX - $left_offset;
      $y = $centerY + $top_offset;
    // Add text to image
      imagettftext($image, $font_size, $angle, $x, $y, $color, $font, $text);
    

    imageftbbox documentation

    【讨论】:

    • $y = $centerY - top_offset; 必须是 $y = $centerY + $top_offset;
    • 不,$top_offset 需要从 $centerY 坐标中减去以说明文本的高度。如果您改为添加$top_offset,则文本将显示在中心下方。
    • 恕我直言,由于某种原因,得到了我想要的结果。
    • 你的$top_offset是正面的还是负面的?
    【解决方案2】:

    查看imagettfbboxhttp://www.php.net/manual/en/function.imagettfbbox.php。它将为您提供您希望呈现的文本的范围。然后将其放在图像的中心是简单的算术。

    【讨论】:

    • 哦,我明白了。那么 PHP GD 是否使所有字体加载固定宽度?
    • 我不这么认为。我对 GD 的内部运作一无所知,但我想它会计算每个字符的宽度和高度,考虑任何填充或边距或其他任何东西,然后给你总数。
    【解决方案3】:

    我花了很长时间,但我想出了如何在图像上准确地居中文本。

    list($left,, $right) = imageftbbox(18, 0, 'minecraft.ttf', $_GET['description']);
    $dwidth = $right - $left;
    $pos = (HALF_OF_IMAGE_WIDTH - $nwidth / 2);
    

    【讨论】:

      【解决方案4】:

      您可以通过获取图像高度和宽度的一半以及文本高度和宽度的一半来使图像中的文本居中

      您可以分别使用imagesximagesy 获取图像宽度和高度。
      您可以使用 PHP GD 中的imagettfbbox 方法获取文本的宽度和高度。

      得到bound之后,就可以得到文字宽度和文字高度
      文本宽度 = x 上的右边界 - x 轴上的左边界
      文字高度 = y 轴下限 - y 轴上限

      然后使用图像的宽度和高度来获取允许图像居中的起始偏移量
      start_x_offset = (imagewidth - textwidth) / 2;
      start_y_offset = (imageheight - textheight) / 2;

      // Get image dimensions
      $image_width = imagesx($image);
      $image_height = imagesy($image);
      
      $text_bound = imageftbbox($font_size, $angle, $font, $text);
      
      //Get the text upper, lower, left and right corner bounds
      $lower_left_x =  $text_bound[0]; 
      $lower_left_y =  $text_bound[1];
      $lower_right_x = $text_bound[2];
      $lower_right_y = $text_bound[3];
      $upper_right_x = $text_bound[4];
      $upper_right_y = $text_bound[5];
      $upper_left_x =  $text_bound[6];
      $upper_left_y =  $text_bound[7];
      
      
      //Get Text Width and text height
      $text_width =  $lower_right_x - $lower_left_x; //or  $upper_right_x - $upper_left_x
      $text_height = $lower_right_y - $upper_right_y; //or  $lower_left_y - $upper_left_y
      
      //Get the starting position for centering
      $start_x_offset = ($image_width - $text_width) / 2;
      $start_y_offset = ($image_height - $text_height) / 2;
      
      // Add text to image
      imagettftext($image, $font_size, $angle, $start_x_offset, $start_y_offset, $color, $font, $text);
      

      【讨论】:

        猜你喜欢
        • 2012-05-27
        • 1970-01-01
        • 1970-01-01
        • 2019-03-23
        • 1970-01-01
        • 1970-01-01
        • 2016-11-16
        • 1970-01-01
        • 2016-05-14
        相关资源
        最近更新 更多