首页 > 解决方案 > 如何在图像字符串中添加换行符(
)(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>

标签: php

解决方案


创建图像中没有行 分隔,您可以使用 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;
} 

推荐阅读