dourangdz750379 2015-01-21 10:10
浏览 388
已采纳

在imagettftext()中换行文本

i got this script to generate an image out of a random jpg in a directory, adding a random slogan from a database:

<?php

header('Content-type: text/html; charset=utf-8'); 

include '../connect.php';
require_once 'random.php';

$timestamp = time();
$date = date("d.m.Y_G", $timestamp);


$slogan = $mysqli->query("SELECT `text` FROM `slogans` ORDER BY RAND() LIMIT 1");

    $slogan_txt = $slogan->fetch_assoc();



    $bg = get_rand_img('../../images/');
    $imgPath = '../../images/' .$bg;

$text = $slogan_txt[text];
$image = $imgPath; 
$font = "font.TTF";
$font_size = "25";

$image_2 = imagecreatefromjpeg($image);
$black = imagecolorallocate($image_2,0,0,0);

$image_width = imagesx($image_2);  
$image_height = imagesy($image_2);

$text_box = imagettfbbox($font_size,$angle,$font,$text);
$text_width = $text_box[2]-$text_box[0]; // lower right corner - lower left corner
$text_height = $text_box[3]-$text_box[1];

$x = ($image_width/2) - ($text_width/2);
$y = ($image_height/2) - ($text_height/2);

imagettftext($image_2,$font_size,0,$x,$y,$black,$font,$text );

header ("Content-type: image/png");
imagejpeg($image_2);



?>

works fine so far.

Now there are some slogans with to much words for one row. i need them to be wrapped and also be centered!

can't use wordwrap in imagettftext(), so i need to explode it somehow.

i found some functions on the web, but they dont work as expected. maybe i just dont know how to combine them with my existing code!

maybe someone got a workign example from own projects?

thanks so far!

  • 写回答

2条回答

  • douyabu1528 2015-01-22 08:06
    关注

    Well, it worked now.

    Found something interesting here and thats it! Just needed to add the calculation for where to start with the text (responding to imagesize).

    GDtext by stil

    Thanks!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)