douzhong1730 2016-03-09 14:56
浏览 268
已采纳

php imagettftext行高

I have successfully written text to my img and positioned it correctly, however the problem I have ran into is that increasing the font size makes the text go off the image.

So I know I can achieve the look I want by just simply increasing the line height of the text writing to the screen. I can't seem to find any documentation on if/how this can be done.

My php code

<?php  

  $font = 'font-type.ttf';

  $rImg = ImageCreateFromJPEG( "test.jpg" );

  $color = imagecolorallocate($rImg, 255, 255, 255);

  //create bounding box
  $bbox = imagettfbbox ( 38 , 0 , $font , urldecode($_GET['promo']) );

  //setting the cordinates
  $x = $bbox[0] + (imagesx($rImg) / 2) - ($bbox[4] / 2) + 200;
  $y = $bbox[1] + (imagesy($rImg) / 2) - ($bbox[5] / 2) + 25;

  //write it
  imagettftext($rImg, 38, 0, $x, $y, $color, $font, urldecode($_GET['promo']));

  header('Content-type: image/jpeg');
  imagejpeg($rImg, NULL,100);

  imagedestroy($rImg);

?>
  • 写回答

1条回答 默认 最新

  • drtwqc3744 2016-03-10 15:32
    关注

    I have achieved my desired look using this function

    function ImageTTFTextWithTracking($im, $size, $angle, $t, $x, $y, $color, $font, $text) {
    $numchar = strlen($text);
    for($i = 0; $i < $numchar; $i++) {
        # Assign character
        $char[$i] = substr($text, $i, 1);
    
        # Write character
        imagettftext($im, $size, $angle, ($x + $w + ($i * $t)), $y, $color, $font, $char[$i]);
    
        # Get width of character
        $width = imagettfbbox($size, $angle, $font, $char[$i]);
        $w = $w + $width[2];
    }
    }
    ImageTTFTextWithTracking($rImg, 48, 0, 10, $x, $y, $colorWhite, $font, urldecode($_GET['code']));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 51单片机使lcd显示器开机闪烁预设字符闪烁3次需要加什么代码
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题