在imagettftext()中换行

问题描述 投票:0回答:3

我使用此脚本从目录中的随机jpg生成图像,并从数据库中添加了随机标语:

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



?>

到目前为止效果很好。>

现在有一些口号一行最多的口号。我需要将它们包裹起来并且也要居中!

不能在imagettftext()中使用自动换行,所以我需要以某种方式使其爆炸。

我在网络上找到了一些功能,但是它们没有按预期工作。也许我只是不知道如何将它们与我现有的代码结合起来!

也许有人从自己的项目中得到了一个可行的例子?

到目前为止谢谢!

我使用此脚本从目录中的随机jpg生成图像,并从数据库中添加随机标语:

php text explode word-wrap imagettftext
3个回答
0
投票

如果第一个字符串长于14,则它将爆炸字符串,并将其文本放入第二个字符串,我想您可以在此基础上进行构建。


0
投票

嗯,现在可以了。


-1
投票
$text = ucfirst("THE TEXT");
header("Content-Type: image/png");
$im = imagecreatefrompng("image_path_png");
$color = imagecolorallocatealpha($im, 255, 255, 255, "100");
imagefillalpha($im, $color);
$text_colour = imagecolorallocate( $im, 0, 125, 125 );
$fontfile= DIR.'/Walkway_SemiBold.ttf';
$black = imagecolorallocate($im, 0, 0, 0);
imagettftext($im, 70, 0, 60, 460, $black, $fontfile, wordwrap($text,16,"\n"));
imagepng($im);
imagedestroy($im);


function ImageFillAlpha($image, $color) {
  imagefilledrectangle($image, 0, 0, imagesx($image), imagesy($image), $color);
}
© www.soinside.com 2019 - 2024. All rights reserved.