我得到这个脚本生成了一个目录中随机的JPG图像,从数据库中添加一个随机的口号:环绕文本中imagettftext()
<?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()中使用wordwrap,所以我需要以某种方式爆炸它。
我发现网络上的一些功能,但他们没有按预期工作。也许我只是不知道如何将它们与我现有的代码结合起来!
也许有人从自己的项目中得到一个工作示例?
感谢到目前为止!