我得到了这个脚本,从一个目录中的随机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() 中使用自动换行,所以我需要以某种方式分解它。
我在网络上找到了一些功能,但它们没有按预期工作。 也许我只是不知道如何将它们与我现有的代码结合起来!
也许有人从自己的项目中得到了一个工作示例?
到目前为止谢谢!
这会分解一个字符串并将其文本放入第二个字符串中,如果第一个字符串长于 14,我想你可以在此基础上建立。
$string = "";
$string2 = "";
$name = explode(" ", $name);
foreach ($name as $n) {
if (strlen($string) + strlen($n) > 14) {
$string2 .= $n . " ";
} else {
$string .= $n . " ";
}
}
要使文本居中,您需要执行以下操作:(图像EX/2) - 数字 * 像素化