使用 imagettftext() 右对齐图像中的文本,PHP

发布于 2024-08-24 22:27:20 字数 354 浏览 8 评论 0原文

我正在为我的用户设置动态论坛签名图像,我希望能够将他们的用户名放在图像上。我可以很好地做到这一点,但是由于用户名长度不同并且我想右对齐用户名,当我必须设置 x & 时我该如何做呢? y 坐标。

$im = imagecreatefromjpeg("/path/to/base/image.jpg");
$text = "Username";
$font = "Font.ttf";
$black = imagecolorallocate($im, 0, 0, 0);

imagettftext($im, 10, 0, 217, 15, $black, $font, $text);
imagejpeg($im, null, 90);

I am setting up dynamic forum signature images for my users and I want to be able to put their username on the image. I am able to do this just fine, but since usernames are different lengths and I want to right align the username, how can I go about doing this when I have to set x & y coordinates.

$im = imagecreatefromjpeg("/path/to/base/image.jpg");
$text = "Username";
$font = "Font.ttf";
$black = imagecolorallocate($im, 0, 0, 0);

imagettftext($im, 10, 0, 217, 15, $black, $font, $text);
imagejpeg($im, null, 90);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

血之狂魔 2024-08-31 22:27:21

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

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

$im = imagecreatefromjpeg("/path/to/base/image.jpg");

$textbox = new Box($im);
$textbox->setFontSize(12);
$textbox->setFontFace("Font.ttf");
$textbox->setFontColor(new Color(0, 0, 0)); // black
$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 right horizontally and to top vertically
$textbox->setTextAlign('right', 'top');

$textbox->draw("Username");

您还可以绘制多行文本。只需在传递给 draw() 方法的字符串中使用 \n 即可。
使用此类生成的示例:

右对齐文本演示

You can use stil/gd-text class. Disclaimer: I am the author.

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

$im = imagecreatefromjpeg("/path/to/base/image.jpg");

$textbox = new Box($im);
$textbox->setFontSize(12);
$textbox->setFontFace("Font.ttf");
$textbox->setFontColor(new Color(0, 0, 0)); // black
$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 right horizontally and to top vertically
$textbox->setTextAlign('right', 'top');

$textbox->draw("Username");

You can also draw multilined text. Just use \n in the string passed to draw() method.
Example generated with this class:

right aligned text demo

┊风居住的梦幻卍 2024-08-31 22:27:21

使用 imagettfbbox() 预先计算用户名的大小。

从那里获得的宽度,您可以扣除文本需要开始的 x 位置。

Pre-calculate the size of the user's name using imagettfbbox().

From the width you get from there, you can then deduct the x position at which your text needs to start.

梦与时光遇 2024-08-31 22:27:21

这会起作用............

                  $s = split("[\n]+", $text);
                  $top=20;
                  $left=30;
                  $font_file="yourfont.ttf";
                  $fontsize=20;
               $__H=$top;
               foreach($s as $key=>$val){
                    $_b = imageTTFBbox($fontsize,0,$font_file,$val);
                    $_W = abs($_b[2]-$_b[0]); 
                    $_X = ($left+$text_box_width)-$_W;
                    $_H = abs($_b[5]-$_b[3]); 
                    $_H +=1;  
                    $__H += $_H;              
                    $res=imagettftext($image, $this->_fontsize, 0, $_X, $__H, $color, $font_file, $val);
                    $__H += 1;

This will work...............

                  $s = split("[\n]+", $text);
                  $top=20;
                  $left=30;
                  $font_file="yourfont.ttf";
                  $fontsize=20;
               $__H=$top;
               foreach($s as $key=>$val){
                    $_b = imageTTFBbox($fontsize,0,$font_file,$val);
                    $_W = abs($_b[2]-$_b[0]); 
                    $_X = ($left+$text_box_width)-$_W;
                    $_H = abs($_b[5]-$_b[3]); 
                    $_H +=1;  
                    $__H += $_H;              
                    $res=imagettftext($image, $this->_fontsize, 0, $_X, $__H, $color, $font_file, $val);
                    $__H += 1;
冰之心 2024-08-31 22:27:21

我增强了 sujithayur 代码,并创建了允许所有对齐(左、中、右)和对齐的函数。 (顶部、中心、中间)及其组合。它还使用文本阴影。

// $x is margin from left, in case of left align, and margin from right, in case of right horizontal align
// $alignHorizontal values can be 'left', 'center', 'right'
// $alignVertical values can be 'top', 'center', 'bottom'
function imagettftext_aligned($image, $fontSize, $x, $y, $color, $colorShadow, $fontPath, $text, $alignHorizontal, $alignVertical) {

      $s = explode("\n", $text);
      $imageWidth = imagesx($image);
      $imageHeight = imagesy($image);

      $top=$y;
      $left=$imageWidth - $x;
      $__H=$top; // default - top
      $lineHeight = $fontSize + 14;

      if ($alignVertical == 'bottom')
        $__H = $imageHeight - $y - (count($s) * $lineHeight);
      elseif ($alignVertical == 'center')
        $__H = $imageHeight/2 - (count($s) * $lineHeight)/2;

      foreach($s as $key=>$val){
            $_b = imageTTFBbox($fontSize,0,$fontPath,$val);
            $_W = abs($_b[2]-$_b[0]); 
            $_H = abs($_b[5]-$_b[3]); 
            $_H +=1;  

            if ($alignHorizontal == 'right')
              $_X = $left - $_W;
            elseif ($alignHorizontal == 'center')
              $_X = $imageWidth/2 - $_W/2;
            else // default - left
              $_X = $x;

            imagettftextblur($image, $fontSize, 0, $_X + 2, $__H + 2, $colorShadow, $fontPath, $val, 4); // 1 can be higher to increase blurriness of the shadow
            imagettftextblur($image, $fontSize, 0, $_X, $__H, $color, $fontPath, $val);

            $__H += $lineHeight + 1;   
       } 

  return ['bottom' => $__H];

}

// https://github.com/andrewgjohnson/imagettftextblur
if (!function_exists('imagettftextblur'))
{
    function imagettftextblur(&$image,$size,$angle,$x,$y,$color,$fontfile,$text,$blur_intensity = null)
    {
        $blur_intensity = !is_null($blur_intensity) && is_numeric($blur_intensity) ? (int)$blur_intensity : 0;
        if ($blur_intensity > 0)
        {
            $text_shadow_image = imagecreatetruecolor(imagesx($image),imagesy($image));
            imagefill($text_shadow_image,0,0,imagecolorallocate($text_shadow_image,0x00,0x00,0x00));
            imagettftext($text_shadow_image,$size,$angle,$x,$y,imagecolorallocate($text_shadow_image,0xFF,0xFF,0xFF),$fontfile,$text);
            for ($blur = 1;$blur <= $blur_intensity;$blur++)
                imagefilter($text_shadow_image,IMG_FILTER_GAUSSIAN_BLUR);
            for ($x_offset = 0;$x_offset < imagesx($text_shadow_image);$x_offset++)
            {
                for ($y_offset = 0;$y_offset < imagesy($text_shadow_image);$y_offset++)
                {
                    $visibility = (imagecolorat($text_shadow_image,$x_offset,$y_offset) & 0xFF) / 255;
                    if ($visibility > 0)
                        imagesetpixel($image,$x_offset,$y_offset,imagecolorallocatealpha($image,($color >> 16) & 0xFF,($color >> 8) & 0xFF,$color & 0xFF,(1 - $visibility) * 127));
                }
            }
            imagedestroy($text_shadow_image);
        }
        else
            return imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text);
    }
}

I enhanced sujithayur code, and created function which allows all aligns (left, center, right) & (top, center, middle) and its combinations. It also uses text shadow.

// $x is margin from left, in case of left align, and margin from right, in case of right horizontal align
// $alignHorizontal values can be 'left', 'center', 'right'
// $alignVertical values can be 'top', 'center', 'bottom'
function imagettftext_aligned($image, $fontSize, $x, $y, $color, $colorShadow, $fontPath, $text, $alignHorizontal, $alignVertical) {

      $s = explode("\n", $text);
      $imageWidth = imagesx($image);
      $imageHeight = imagesy($image);

      $top=$y;
      $left=$imageWidth - $x;
      $__H=$top; // default - top
      $lineHeight = $fontSize + 14;

      if ($alignVertical == 'bottom')
        $__H = $imageHeight - $y - (count($s) * $lineHeight);
      elseif ($alignVertical == 'center')
        $__H = $imageHeight/2 - (count($s) * $lineHeight)/2;

      foreach($s as $key=>$val){
            $_b = imageTTFBbox($fontSize,0,$fontPath,$val);
            $_W = abs($_b[2]-$_b[0]); 
            $_H = abs($_b[5]-$_b[3]); 
            $_H +=1;  

            if ($alignHorizontal == 'right')
              $_X = $left - $_W;
            elseif ($alignHorizontal == 'center')
              $_X = $imageWidth/2 - $_W/2;
            else // default - left
              $_X = $x;

            imagettftextblur($image, $fontSize, 0, $_X + 2, $__H + 2, $colorShadow, $fontPath, $val, 4); // 1 can be higher to increase blurriness of the shadow
            imagettftextblur($image, $fontSize, 0, $_X, $__H, $color, $fontPath, $val);

            $__H += $lineHeight + 1;   
       } 

  return ['bottom' => $__H];

}

// https://github.com/andrewgjohnson/imagettftextblur
if (!function_exists('imagettftextblur'))
{
    function imagettftextblur(&$image,$size,$angle,$x,$y,$color,$fontfile,$text,$blur_intensity = null)
    {
        $blur_intensity = !is_null($blur_intensity) && is_numeric($blur_intensity) ? (int)$blur_intensity : 0;
        if ($blur_intensity > 0)
        {
            $text_shadow_image = imagecreatetruecolor(imagesx($image),imagesy($image));
            imagefill($text_shadow_image,0,0,imagecolorallocate($text_shadow_image,0x00,0x00,0x00));
            imagettftext($text_shadow_image,$size,$angle,$x,$y,imagecolorallocate($text_shadow_image,0xFF,0xFF,0xFF),$fontfile,$text);
            for ($blur = 1;$blur <= $blur_intensity;$blur++)
                imagefilter($text_shadow_image,IMG_FILTER_GAUSSIAN_BLUR);
            for ($x_offset = 0;$x_offset < imagesx($text_shadow_image);$x_offset++)
            {
                for ($y_offset = 0;$y_offset < imagesy($text_shadow_image);$y_offset++)
                {
                    $visibility = (imagecolorat($text_shadow_image,$x_offset,$y_offset) & 0xFF) / 255;
                    if ($visibility > 0)
                        imagesetpixel($image,$x_offset,$y_offset,imagecolorallocatealpha($image,($color >> 16) & 0xFF,($color >> 8) & 0xFF,$color & 0xFF,(1 - $visibility) * 127));
                }
            }
            imagedestroy($text_shadow_image);
        }
        else
            return imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text);
    }
}
终陌 2024-08-31 22:27:20

使用 imagettfbbox 函数获取字符串的宽度,然后从图像的宽度中减去该宽度以获得起始 x 坐标。

$dimensions = imagettfbbox($fontSize, $angle, $font, $text);
$textWidth = abs($dimensions[4] - $dimensions[0]);
$x = imagesx($im) - $textWidth;

Use the imagettfbbox function to get the width of the string and then subtract that from the width of the image to get the starting x-coordinate.

$dimensions = imagettfbbox($fontSize, $angle, $font, $text);
$textWidth = abs($dimensions[4] - $dimensions[0]);
$x = imagesx($im) - $textWidth;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文