php - 如何在图像字符串中添加换行符(
)(PHP中的文本到图像)
问题描述
我正在制作名片。因此,我创建了视图来获取用户输入,例如姓名、电子邮件、网络。当有人将这些数据输入视图时,所有数据都将显示为图像。现在我把所有的都集中在一条线上。但是,我需要 name , email 和 web 像这样在单独的行中,
我怎样才能解决这个问题 ??
姓名
电子邮件
网络
这是PHP代码。
<?php
if(isset($_GET['submit'])){
$name = $_GET['name'];
$email = $_GET['email'];
$web = $_GET['web'];
$message = "<h1>$name</h1> <br> <h2>$email</h2> <br> <h3>$web</h3>";
$length = strlen($message) * 9.3;
$image = imagecreate($length,20);
$back = imagecolorallocate($image, 0,0,0);
$for = imagecolorallocate($image, 255,255,255);
imagestring($image,5,5,1,$message,$for);
header("Content-Type: image/jpeg");
imagejpeg($image);
}
?>
这是表格。
<form action="" method="" class="formsize">
Your Name : <input type="text" name="name" id="name" class="form-control"> <br><br>
Your Email : <input type="email" name="email" id="email" class="form-control"> <br><br>
Your Web Address : <input type="text" name="web" id="web" class="form-control"> <br><br>
<label>Upload Photo : </label>
<input type="file" class="form-control-file" name="file_img" aria-describedby="fileHelp"> <br><br>
<input type="submit" name="submit" value="Submit" class="btn btn-primary"> <br><br>
</form>
解决方案
创建图像中没有行 分隔,您可以使用 image_width:
$text = "Your Message";
$image_width = 200; // pixels
text_to_image($text, $image_width);
function text_to_image($text, $image_width, $colour = array(0,244,34), $background = array(0,0,0))
{
$font = 5;
$line_height = 15;
$padding = 5;
$text = wordwrap($text, ($image_width/10));
$lines = explode("\n", $text);
$image = imagecreate($image_width,((count($lines) * $line_height)) + ($padding * 2));
$background = imagecolorallocate($image, $background[0], $background[1], $background[2]);
$colour = imagecolorallocate($image,$colour[0],$colour[1],$colour[2]);
imagefill($image, 0, 0, $background);
$i = $padding;
foreach($lines as $line){
imagestring($image, $font, $padding, $i, trim($line), $colour);
$i += $line_height;
}
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
exit;
}
推荐阅读
- ftp - 如何将 DPDK 与 GridFTP 一起使用?
- javascript - 通过 NPM 安装时出现漏洞错误,( npm 审计修复)也无法正常工作
- html - 为什么调整大小:两者都不适用于字段集?
- javascript - 页面加载后发生的jQuery .get函数(HAML)
- typescript - TypeORM:如何在由一对多支持的实体上拥有字符串数组属性?
- bash - 为什么循环只运行 1 次而不是到达文件末尾?
- javascript - 将新源设置为 Audio Element 后如何中止下载 .mp3 文件?
- python - 如何引发坐标超出栅格边界(rasterio.sample)的错误?
- load-balancing - 如何实现具有持久连接的有状态应用程序的负载平衡 - Envoy 和 Rsocket
- c - 从文件中填充 C 中的嵌套结构的正确方法